Merge potential next release v0.4 (#187) Breaking Changes
* First draft of the new n-dimensional arrays + NB use case * Improves default implementation of multiple Array methods * Refactors tree methods * Adds matrix decomposition routines * Adds matrix decomposition methods to ndarray and nalgebra bindings * Refactoring + linear regression now uses array2 * Ridge & Linear regression * LBFGS optimizer & logistic regression * LBFGS optimizer & logistic regression * Changes linear methods, metrics and model selection methods to new n-dimensional arrays * Switches KNN and clustering algorithms to new n-d array layer * Refactors distance metrics * Optimizes knn and clustering methods * Refactors metrics module * Switches decomposition methods to n-dimensional arrays * Linalg refactoring - cleanup rng merge (#172) * Remove legacy DenseMatrix and BaseMatrix implementation. Port the new Number, FloatNumber and Array implementation into module structure. * Exclude AUC metrics. Needs reimplementation * Improve developers walkthrough New traits system in place at `src/numbers` and `src/linalg` Co-authored-by: Lorenzo <tunedconsulting@gmail.com> * Provide SupervisedEstimator with a constructor to avoid explicit dynamical box allocation in 'cross_validate' and 'cross_validate_predict' as required by the use of 'dyn' as per Rust 2021 * Implement getters to use as_ref() in src/neighbors * Implement getters to use as_ref() in src/naive_bayes * Implement getters to use as_ref() in src/linear * Add Clone to src/naive_bayes * Change signature for cross_validate and other model_selection functions to abide to use of dyn in Rust 2021 * Implement ndarray-bindings. Remove FloatNumber from implementations * Drop nalgebra-bindings support (as decided in conf-call to go for ndarray) * Remove benches. Benches will have their own repo at smartcore-benches * Implement SVC * Implement SVC serialization. Move search parameters in dedicated module * Implement SVR. Definitely too slow * Fix compilation issues for wasm (#202) Co-authored-by: Luis Moreno <morenol@users.noreply.github.com> * Fix tests (#203) * Port linalg/traits/stats.rs * Improve methods naming * Improve Display for DenseMatrix Co-authored-by: Montana Low <montanalow@users.noreply.github.com> Co-authored-by: VolodymyrOrlov <volodymyr.orlov@gmail.com>
This commit is contained in:
+189
-155
@@ -6,7 +6,7 @@
|
||||
//! Example:
|
||||
//!
|
||||
//! ```
|
||||
//! use smartcore::linalg::naive::dense_matrix::*;
|
||||
//! use smartcore::linalg::basic::matrix::DenseMatrix;
|
||||
//! use smartcore::naive_bayes::bernoulli::BernoulliNB;
|
||||
//!
|
||||
//! // Training data points are:
|
||||
@@ -14,56 +14,55 @@
|
||||
//! // Chinese Chinese Shanghai (class: China)
|
||||
//! // Chinese Macao (class: China)
|
||||
//! // Tokyo Japan Chinese (class: Japan)
|
||||
//! let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
//! &[1., 1., 0., 0., 0., 0.],
|
||||
//! &[0., 1., 0., 0., 1., 0.],
|
||||
//! &[0., 1., 0., 1., 0., 0.],
|
||||
//! &[0., 1., 1., 0., 0., 1.],
|
||||
//! let x = DenseMatrix::from_2d_array(&[
|
||||
//! &[1, 1, 0, 0, 0, 0],
|
||||
//! &[0, 1, 0, 0, 1, 0],
|
||||
//! &[0, 1, 0, 1, 0, 0],
|
||||
//! &[0, 1, 1, 0, 0, 1],
|
||||
//! ]);
|
||||
//! let y = vec![0., 0., 0., 1.];
|
||||
//! let y: Vec<u32> = vec![0, 0, 0, 1];
|
||||
//!
|
||||
//! let nb = BernoulliNB::fit(&x, &y, Default::default()).unwrap();
|
||||
//!
|
||||
//! // Testing data point is:
|
||||
//! // Chinese Chinese Chinese Tokyo Japan
|
||||
//! let x_test = DenseMatrix::<f64>::from_2d_array(&[&[0., 1., 1., 0., 0., 1.]]);
|
||||
//! let x_test = DenseMatrix::from_2d_array(&[&[0, 1, 1, 0, 0, 1]]);
|
||||
//! let y_hat = nb.predict(&x_test).unwrap();
|
||||
//! ```
|
||||
//!
|
||||
//! ## References:
|
||||
//!
|
||||
//! * ["Introduction to Information Retrieval", Manning C. D., Raghavan P., Schutze H., 2009, Chapter 13 ](https://nlp.stanford.edu/IR-book/information-retrieval-book.html)
|
||||
use num_traits::Unsigned;
|
||||
|
||||
use crate::api::{Predictor, SupervisedEstimator};
|
||||
use crate::error::Failed;
|
||||
use crate::linalg::row_iter;
|
||||
use crate::linalg::BaseVector;
|
||||
use crate::linalg::Matrix;
|
||||
use crate::math::num::RealNumber;
|
||||
use crate::math::vector::RealNumberVector;
|
||||
use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1};
|
||||
use crate::naive_bayes::{BaseNaiveBayes, NBDistribution};
|
||||
use crate::numbers::basenum::Number;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Naive Bayes classifier for Bearnoulli features
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug)]
|
||||
struct BernoulliNBDistribution<T: RealNumber> {
|
||||
#[derive(Debug, Clone)]
|
||||
struct BernoulliNBDistribution<T: Number + Ord + Unsigned> {
|
||||
/// class labels known to the classifier
|
||||
class_labels: Vec<T>,
|
||||
/// number of training samples observed in each class
|
||||
class_count: Vec<usize>,
|
||||
/// probability of each class
|
||||
class_priors: Vec<T>,
|
||||
class_priors: Vec<f64>,
|
||||
/// Number of samples encountered for each (class, feature)
|
||||
feature_count: Vec<Vec<usize>>,
|
||||
/// probability of features per class
|
||||
feature_log_prob: Vec<Vec<T>>,
|
||||
feature_log_prob: Vec<Vec<f64>>,
|
||||
/// Number of features of each sample
|
||||
n_features: usize,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> PartialEq for BernoulliNBDistribution<T> {
|
||||
impl<T: Number + Ord + Unsigned> PartialEq for BernoulliNBDistribution<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
if self.class_labels == other.class_labels
|
||||
&& self.class_count == other.class_count
|
||||
@@ -76,7 +75,7 @@ impl<T: RealNumber> PartialEq for BernoulliNBDistribution<T> {
|
||||
.iter()
|
||||
.zip(other.feature_log_prob.iter())
|
||||
{
|
||||
if !a.approximate_eq(b, T::epsilon()) {
|
||||
if !a.iter().zip(b.iter()).all(|(a, b)| (a - b).abs() < 1e-4) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -87,25 +86,27 @@ impl<T: RealNumber> PartialEq for BernoulliNBDistribution<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for BernoulliNBDistribution<T> {
|
||||
fn prior(&self, class_index: usize) -> T {
|
||||
impl<X: Number + PartialOrd, Y: Number + Ord + Unsigned> NBDistribution<X, Y>
|
||||
for BernoulliNBDistribution<Y>
|
||||
{
|
||||
fn prior(&self, class_index: usize) -> f64 {
|
||||
self.class_priors[class_index]
|
||||
}
|
||||
|
||||
fn log_likelihood(&self, class_index: usize, j: &M::RowVector) -> T {
|
||||
let mut likelihood = T::zero();
|
||||
for feature in 0..j.len() {
|
||||
let value = j.get(feature);
|
||||
if value == T::one() {
|
||||
fn log_likelihood<'a>(&'a self, class_index: usize, j: &'a Box<dyn ArrayView1<X> + 'a>) -> f64 {
|
||||
let mut likelihood = 0f64;
|
||||
for feature in 0..j.shape() {
|
||||
let value = *j.get(feature);
|
||||
if value == X::one() {
|
||||
likelihood += self.feature_log_prob[class_index][feature];
|
||||
} else {
|
||||
likelihood += (T::one() - self.feature_log_prob[class_index][feature].exp()).ln();
|
||||
likelihood += (1f64 - self.feature_log_prob[class_index][feature].exp()).ln();
|
||||
}
|
||||
}
|
||||
likelihood
|
||||
}
|
||||
|
||||
fn classes(&self) -> &Vec<T> {
|
||||
fn classes(&self) -> &Vec<Y> {
|
||||
&self.class_labels
|
||||
}
|
||||
}
|
||||
@@ -113,26 +114,26 @@ impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for BernoulliNBDistributi
|
||||
/// `BernoulliNB` parameters. Use `Default::default()` for default values.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BernoulliNBParameters<T: RealNumber> {
|
||||
pub struct BernoulliNBParameters<T: Number> {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub alpha: T,
|
||||
pub alpha: f64,
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub priors: Option<Vec<T>>,
|
||||
pub priors: Option<Vec<f64>>,
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors.
|
||||
pub binarize: Option<T>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> BernoulliNBParameters<T> {
|
||||
impl<T: Number + PartialOrd> BernoulliNBParameters<T> {
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub fn with_alpha(mut self, alpha: T) -> Self {
|
||||
pub fn with_alpha(mut self, alpha: f64) -> Self {
|
||||
self.alpha = alpha;
|
||||
self
|
||||
}
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub fn with_priors(mut self, priors: Vec<T>) -> Self {
|
||||
pub fn with_priors(mut self, priors: Vec<f64>) -> Self {
|
||||
self.priors = Some(priors);
|
||||
self
|
||||
}
|
||||
@@ -143,11 +144,11 @@ impl<T: RealNumber> BernoulliNBParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for BernoulliNBParameters<T> {
|
||||
impl<T: Number + PartialOrd> Default for BernoulliNBParameters<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
alpha: T::one(),
|
||||
priors: None,
|
||||
alpha: 1f64,
|
||||
priors: Option::None,
|
||||
binarize: Some(T::zero()),
|
||||
}
|
||||
}
|
||||
@@ -156,27 +157,27 @@ impl<T: RealNumber> Default for BernoulliNBParameters<T> {
|
||||
/// BernoulliNB grid search parameters
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BernoulliNBSearchParameters<T: RealNumber> {
|
||||
pub struct BernoulliNBSearchParameters<T: Number> {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub alpha: Vec<T>,
|
||||
pub alpha: Vec<f64>,
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub priors: Vec<Option<Vec<T>>>,
|
||||
pub priors: Vec<Option<Vec<f64>>>,
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Threshold for binarizing (mapping to booleans) of sample features. If None, input is presumed to already consist of binary vectors.
|
||||
pub binarize: Vec<Option<T>>,
|
||||
}
|
||||
|
||||
/// BernoulliNB grid search iterator
|
||||
pub struct BernoulliNBSearchParametersIterator<T: RealNumber> {
|
||||
pub struct BernoulliNBSearchParametersIterator<T: Number> {
|
||||
bernoulli_nb_search_parameters: BernoulliNBSearchParameters<T>,
|
||||
current_alpha: usize,
|
||||
current_priors: usize,
|
||||
current_binarize: usize,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> IntoIterator for BernoulliNBSearchParameters<T> {
|
||||
impl<T: Number> IntoIterator for BernoulliNBSearchParameters<T> {
|
||||
type Item = BernoulliNBParameters<T>;
|
||||
type IntoIter = BernoulliNBSearchParametersIterator<T>;
|
||||
|
||||
@@ -190,7 +191,7 @@ impl<T: RealNumber> IntoIterator for BernoulliNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Iterator for BernoulliNBSearchParametersIterator<T> {
|
||||
impl<T: Number> Iterator for BernoulliNBSearchParametersIterator<T> {
|
||||
type Item = BernoulliNBParameters<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
@@ -226,9 +227,9 @@ impl<T: RealNumber> Iterator for BernoulliNBSearchParametersIterator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for BernoulliNBSearchParameters<T> {
|
||||
impl<T: Number + std::cmp::PartialOrd> Default for BernoulliNBSearchParameters<T> {
|
||||
fn default() -> Self {
|
||||
let default_params = BernoulliNBParameters::default();
|
||||
let default_params = BernoulliNBParameters::<T>::default();
|
||||
|
||||
BernoulliNBSearchParameters {
|
||||
alpha: vec![default_params.alpha],
|
||||
@@ -238,7 +239,7 @@ impl<T: RealNumber> Default for BernoulliNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> BernoulliNBDistribution<T> {
|
||||
impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
|
||||
/// Fits the distribution to a NxM matrix where N is number of samples and M is number of features.
|
||||
/// * `x` - training data.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
@@ -246,14 +247,14 @@ impl<T: RealNumber> BernoulliNBDistribution<T> {
|
||||
/// priors are adjusted according to the data.
|
||||
/// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter.
|
||||
/// * `binarize` - Threshold for binarizing.
|
||||
pub fn fit<M: Matrix<T>>(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
alpha: T,
|
||||
priors: Option<Vec<T>>,
|
||||
fn fit<TX: Number + PartialOrd, X: Array2<TX>, Y: Array1<TY>>(
|
||||
x: &X,
|
||||
y: &Y,
|
||||
alpha: f64,
|
||||
priors: Option<Vec<f64>>,
|
||||
) -> Result<Self, Failed> {
|
||||
let (n_samples, n_features) = x.shape();
|
||||
let y_samples = y.len();
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
@@ -267,16 +268,15 @@ impl<T: RealNumber> BernoulliNBDistribution<T> {
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
if alpha < T::zero() {
|
||||
if alpha < 0f64 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Alpha should be greater than 0; |alpha|=[{}]",
|
||||
alpha
|
||||
)));
|
||||
}
|
||||
|
||||
let y = y.to_vec();
|
||||
let (class_labels, indices) = y.unique_with_indices();
|
||||
|
||||
let (class_labels, indices) = <Vec<T> as RealNumberVector<T>>::unique_with_indices(&y);
|
||||
let mut class_count = vec![0_usize; class_labels.len()];
|
||||
|
||||
for class_index in indices.iter() {
|
||||
@@ -293,14 +293,14 @@ impl<T: RealNumber> BernoulliNBDistribution<T> {
|
||||
} else {
|
||||
class_count
|
||||
.iter()
|
||||
.map(|&c| T::from(c).unwrap() / T::from(n_samples).unwrap())
|
||||
.map(|&c| c as f64 / (n_samples as f64))
|
||||
.collect()
|
||||
};
|
||||
|
||||
let mut feature_in_class_counter = vec![vec![0_usize; n_features]; class_labels.len()];
|
||||
|
||||
for (row, class_index) in row_iter(x).zip(indices) {
|
||||
for (idx, row_i) in row.iter().enumerate().take(n_features) {
|
||||
for (row, class_index) in x.row_iter().zip(indices) {
|
||||
for (idx, row_i) in row.iterator(0).enumerate().take(n_features) {
|
||||
feature_in_class_counter[class_index][idx] +=
|
||||
row_i.to_usize().ok_or_else(|| {
|
||||
Failed::fit(&format!(
|
||||
@@ -318,9 +318,8 @@ impl<T: RealNumber> BernoulliNBDistribution<T> {
|
||||
feature_count
|
||||
.iter()
|
||||
.map(|&count| {
|
||||
((T::from(count).unwrap() + alpha)
|
||||
/ (T::from(class_count[class_index]).unwrap() + alpha * T::two()))
|
||||
.ln()
|
||||
((count as f64 + alpha) / (class_count[class_index] as f64 + alpha * 2f64))
|
||||
.ln()
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
@@ -341,40 +340,52 @@ impl<T: RealNumber> BernoulliNBDistribution<T> {
|
||||
/// distribution.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct BernoulliNB<T: RealNumber, M: Matrix<T>> {
|
||||
inner: BaseNaiveBayes<T, M, BernoulliNBDistribution<T>>,
|
||||
binarize: Option<T>,
|
||||
pub struct BernoulliNB<
|
||||
TX: Number + PartialOrd,
|
||||
TY: Number + Ord + Unsigned,
|
||||
X: Array2<TX>,
|
||||
Y: Array1<TY>,
|
||||
> {
|
||||
inner: Option<BaseNaiveBayes<TX, TY, X, Y, BernoulliNBDistribution<TY>>>,
|
||||
binarize: Option<TX>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> SupervisedEstimator<M, M::RowVector, BernoulliNBParameters<T>>
|
||||
for BernoulliNB<T, M>
|
||||
impl<TX: Number + PartialOrd, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
SupervisedEstimator<X, Y, BernoulliNBParameters<TX>> for BernoulliNB<TX, TY, X, Y>
|
||||
{
|
||||
fn fit(x: &M, y: &M::RowVector, parameters: BernoulliNBParameters<T>) -> Result<Self, Failed> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
inner: Option::None,
|
||||
binarize: Option::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn fit(x: &X, y: &Y, parameters: BernoulliNBParameters<TX>) -> Result<Self, Failed> {
|
||||
BernoulliNB::fit(x, y, parameters)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> Predictor<M, M::RowVector> for BernoulliNB<T, M> {
|
||||
fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
impl<TX: Number + PartialOrd, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
Predictor<X, Y> for BernoulliNB<TX, TY, X, Y>
|
||||
{
|
||||
fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.predict(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> BernoulliNB<T, M> {
|
||||
impl<TX: Number + PartialOrd, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
BernoulliNB<TX, TY, X, Y>
|
||||
{
|
||||
/// Fits BernoulliNB with given data
|
||||
/// * `x` - training data of size NxM where N is the number of samples and M is the number of
|
||||
/// features.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `parameters` - additional parameters like class priors, alpha for smoothing and
|
||||
/// binarizing threshold.
|
||||
pub fn fit(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
parameters: BernoulliNBParameters<T>,
|
||||
) -> Result<Self, Failed> {
|
||||
pub fn fit(x: &X, y: &Y, parameters: BernoulliNBParameters<TX>) -> Result<Self, Failed> {
|
||||
let distribution = if let Some(threshold) = parameters.binarize {
|
||||
BernoulliNBDistribution::fit(
|
||||
&(x.binarize(threshold)),
|
||||
&Self::binarize(x, threshold),
|
||||
y,
|
||||
parameters.alpha,
|
||||
parameters.priors,
|
||||
@@ -385,7 +396,7 @@ impl<T: RealNumber, M: Matrix<T>> BernoulliNB<T, M> {
|
||||
|
||||
let inner = BaseNaiveBayes::fit(distribution)?;
|
||||
Ok(Self {
|
||||
inner,
|
||||
inner: Some(inner),
|
||||
binarize: parameters.binarize,
|
||||
})
|
||||
}
|
||||
@@ -393,51 +404,73 @@ impl<T: RealNumber, M: Matrix<T>> BernoulliNB<T, M> {
|
||||
/// Estimates the class labels for the provided data.
|
||||
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
|
||||
/// Returns a vector of size N with class estimates.
|
||||
pub fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
pub fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
if let Some(threshold) = self.binarize {
|
||||
self.inner.predict(&(x.binarize(threshold)))
|
||||
self.inner
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.predict(&Self::binarize(x, threshold))
|
||||
} else {
|
||||
self.inner.predict(x)
|
||||
self.inner.as_ref().unwrap().predict(x)
|
||||
}
|
||||
}
|
||||
|
||||
/// Class labels known to the classifier.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn classes(&self) -> &Vec<T> {
|
||||
&self.inner.distribution.class_labels
|
||||
pub fn classes(&self) -> &Vec<TY> {
|
||||
&self.inner.as_ref().unwrap().distribution.class_labels
|
||||
}
|
||||
|
||||
/// Number of training samples observed in each class.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn class_count(&self) -> &Vec<usize> {
|
||||
&self.inner.distribution.class_count
|
||||
&self.inner.as_ref().unwrap().distribution.class_count
|
||||
}
|
||||
|
||||
/// Number of features of each sample
|
||||
pub fn n_features(&self) -> usize {
|
||||
self.inner.distribution.n_features
|
||||
self.inner.as_ref().unwrap().distribution.n_features
|
||||
}
|
||||
|
||||
/// Number of samples encountered for each (class, feature)
|
||||
/// Returns a 2d vector of shape (n_classes, n_features)
|
||||
pub fn feature_count(&self) -> &Vec<Vec<usize>> {
|
||||
&self.inner.distribution.feature_count
|
||||
&self.inner.as_ref().unwrap().distribution.feature_count
|
||||
}
|
||||
|
||||
/// Empirical log probability of features given a class
|
||||
pub fn feature_log_prob(&self) -> &Vec<Vec<T>> {
|
||||
&self.inner.distribution.feature_log_prob
|
||||
pub fn feature_log_prob(&self) -> &Vec<Vec<f64>> {
|
||||
&self.inner.as_ref().unwrap().distribution.feature_log_prob
|
||||
}
|
||||
|
||||
fn binarize_mut(x: &mut X, threshold: TX) {
|
||||
let (nrows, ncols) = x.shape();
|
||||
for row in 0..nrows {
|
||||
for col in 0..ncols {
|
||||
if *x.get((row, col)) > threshold {
|
||||
x.set((row, col), TX::one());
|
||||
} else {
|
||||
x.set((row, col), TX::zero());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn binarize(x: &X, threshold: TX) -> X {
|
||||
let mut new_x = x.clone();
|
||||
Self::binarize_mut(&mut new_x, threshold);
|
||||
new_x
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::linalg::naive::dense_matrix::DenseMatrix;
|
||||
use crate::linalg::basic::matrix::DenseMatrix;
|
||||
|
||||
#[test]
|
||||
fn search_parameters() {
|
||||
let parameters = BernoulliNBSearchParameters {
|
||||
let parameters: BernoulliNBSearchParameters<f64> = BernoulliNBSearchParameters {
|
||||
alpha: vec![1., 2.],
|
||||
..Default::default()
|
||||
};
|
||||
@@ -462,16 +495,18 @@ mod tests {
|
||||
// Chinese Chinese Shanghai (class: China)
|
||||
// Chinese Macao (class: China)
|
||||
// Tokyo Japan Chinese (class: Japan)
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[1., 1., 0., 0., 0., 0.],
|
||||
&[0., 1., 0., 0., 1., 0.],
|
||||
&[0., 1., 0., 1., 0., 0.],
|
||||
&[0., 1., 1., 0., 0., 1.],
|
||||
let x = DenseMatrix::from_2d_array(&[
|
||||
&[1.0, 1.0, 0.0, 0.0, 0.0, 0.0],
|
||||
&[0.0, 1.0, 0.0, 0.0, 1.0, 0.0],
|
||||
&[0.0, 1.0, 0.0, 1.0, 0.0, 0.0],
|
||||
&[0.0, 1.0, 1.0, 0.0, 0.0, 1.0],
|
||||
]);
|
||||
let y = vec![0., 0., 0., 1.];
|
||||
let y: Vec<u32> = vec![0, 0, 0, 1];
|
||||
let bnb = BernoulliNB::fit(&x, &y, Default::default()).unwrap();
|
||||
|
||||
assert_eq!(bnb.inner.distribution.class_priors, &[0.75, 0.25]);
|
||||
let distribution = bnb.inner.clone().unwrap().distribution;
|
||||
|
||||
assert_eq!(&distribution.class_priors, &[0.75, 0.25]);
|
||||
assert_eq!(
|
||||
bnb.feature_log_prob(),
|
||||
&[
|
||||
@@ -496,38 +531,38 @@ mod tests {
|
||||
|
||||
// Testing data point is:
|
||||
// Chinese Chinese Chinese Tokyo Japan
|
||||
let x_test = DenseMatrix::<f64>::from_2d_array(&[&[0., 1., 1., 0., 0., 1.]]);
|
||||
let x_test = DenseMatrix::from_2d_array(&[&[0.0, 1.0, 1.0, 0.0, 0.0, 1.0]]);
|
||||
let y_hat = bnb.predict(&x_test).unwrap();
|
||||
|
||||
assert_eq!(y_hat, &[1.]);
|
||||
assert_eq!(y_hat, &[1]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn bernoulli_nb_scikit_parity() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[2., 4., 0., 0., 2., 1., 2., 4., 2., 0.],
|
||||
&[3., 4., 0., 2., 1., 0., 1., 4., 0., 3.],
|
||||
&[1., 4., 2., 4., 1., 0., 1., 2., 3., 2.],
|
||||
&[0., 3., 3., 4., 1., 0., 3., 1., 1., 1.],
|
||||
&[0., 2., 1., 4., 3., 4., 1., 2., 3., 1.],
|
||||
&[3., 2., 4., 1., 3., 0., 2., 4., 0., 2.],
|
||||
&[3., 1., 3., 0., 2., 0., 4., 4., 3., 4.],
|
||||
&[2., 2., 2., 0., 1., 1., 2., 1., 0., 1.],
|
||||
&[3., 3., 2., 2., 0., 2., 3., 2., 2., 3.],
|
||||
&[4., 3., 4., 4., 4., 2., 2., 0., 1., 4.],
|
||||
&[3., 4., 2., 2., 1., 4., 4., 4., 1., 3.],
|
||||
&[3., 0., 1., 4., 4., 0., 0., 3., 2., 4.],
|
||||
&[2., 0., 3., 3., 1., 2., 0., 2., 4., 1.],
|
||||
&[2., 4., 0., 4., 2., 4., 1., 3., 1., 4.],
|
||||
&[0., 2., 2., 3., 4., 0., 4., 4., 4., 4.],
|
||||
let x = DenseMatrix::from_2d_array(&[
|
||||
&[2, 4, 0, 0, 2, 1, 2, 4, 2, 0],
|
||||
&[3, 4, 0, 2, 1, 0, 1, 4, 0, 3],
|
||||
&[1, 4, 2, 4, 1, 0, 1, 2, 3, 2],
|
||||
&[0, 3, 3, 4, 1, 0, 3, 1, 1, 1],
|
||||
&[0, 2, 1, 4, 3, 4, 1, 2, 3, 1],
|
||||
&[3, 2, 4, 1, 3, 0, 2, 4, 0, 2],
|
||||
&[3, 1, 3, 0, 2, 0, 4, 4, 3, 4],
|
||||
&[2, 2, 2, 0, 1, 1, 2, 1, 0, 1],
|
||||
&[3, 3, 2, 2, 0, 2, 3, 2, 2, 3],
|
||||
&[4, 3, 4, 4, 4, 2, 2, 0, 1, 4],
|
||||
&[3, 4, 2, 2, 1, 4, 4, 4, 1, 3],
|
||||
&[3, 0, 1, 4, 4, 0, 0, 3, 2, 4],
|
||||
&[2, 0, 3, 3, 1, 2, 0, 2, 4, 1],
|
||||
&[2, 4, 0, 4, 2, 4, 1, 3, 1, 4],
|
||||
&[0, 2, 2, 3, 4, 0, 4, 4, 4, 4],
|
||||
]);
|
||||
let y = vec![2., 2., 0., 0., 0., 2., 1., 1., 0., 1., 0., 0., 2., 0., 2.];
|
||||
let y: Vec<u32> = vec![2, 2, 0, 0, 0, 2, 1, 1, 0, 1, 0, 0, 2, 0, 2];
|
||||
let bnb = BernoulliNB::fit(&x, &y, Default::default()).unwrap();
|
||||
|
||||
let y_hat = bnb.predict(&x).unwrap();
|
||||
|
||||
assert_eq!(bnb.classes(), &[0., 1., 2.]);
|
||||
assert_eq!(bnb.classes(), &[0, 1, 2]);
|
||||
assert_eq!(bnb.class_count(), &[7, 3, 5]);
|
||||
assert_eq!(bnb.n_features(), 10);
|
||||
assert_eq!(
|
||||
@@ -539,48 +574,47 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
assert!(bnb
|
||||
.inner
|
||||
.distribution
|
||||
.class_priors
|
||||
.approximate_eq(&vec!(0.46, 0.2, 0.33), 1e-2));
|
||||
assert!(bnb.feature_log_prob()[1].approximate_eq(
|
||||
let distribution = bnb.inner.clone().unwrap().distribution;
|
||||
|
||||
assert_eq!(
|
||||
&distribution.class_priors,
|
||||
&vec!(0.4666666666666667, 0.2, 0.3333333333333333)
|
||||
);
|
||||
assert_eq!(
|
||||
&bnb.feature_log_prob()[1],
|
||||
&vec![
|
||||
-0.22314355,
|
||||
-0.22314355,
|
||||
-0.22314355,
|
||||
-0.91629073,
|
||||
-0.22314355,
|
||||
-0.51082562,
|
||||
-0.22314355,
|
||||
-0.51082562,
|
||||
-0.51082562,
|
||||
-0.22314355
|
||||
],
|
||||
1e-1
|
||||
));
|
||||
assert!(y_hat.approximate_eq(
|
||||
&vec!(2.0, 2.0, 0.0, 0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
|
||||
1e-5
|
||||
));
|
||||
-0.2231435513142097,
|
||||
-0.2231435513142097,
|
||||
-0.2231435513142097,
|
||||
-0.916290731874155,
|
||||
-0.2231435513142097,
|
||||
-0.5108256237659907,
|
||||
-0.2231435513142097,
|
||||
-0.5108256237659907,
|
||||
-0.5108256237659907,
|
||||
-0.2231435513142097
|
||||
]
|
||||
);
|
||||
assert_eq!(y_hat, vec!(2, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0));
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
#[cfg(feature = "serde")]
|
||||
fn serde() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[1., 1., 0., 0., 0., 0.],
|
||||
&[0., 1., 0., 0., 1., 0.],
|
||||
&[0., 1., 0., 1., 0., 0.],
|
||||
&[0., 1., 1., 0., 0., 1.],
|
||||
]);
|
||||
let y = vec![0., 0., 0., 1.];
|
||||
// TODO: implement serialization
|
||||
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
// #[test]
|
||||
// #[cfg(feature = "serde")]
|
||||
// fn serde() {
|
||||
// let x = DenseMatrix::from_2d_array(&[
|
||||
// &[1, 1, 0, 0, 0, 0],
|
||||
// &[0, 1, 0, 0, 1, 0],
|
||||
// &[0, 1, 0, 1, 0, 0],
|
||||
// &[0, 1, 1, 0, 0, 1],
|
||||
// ]);
|
||||
// let y: Vec<u32> = vec![0, 0, 0, 1];
|
||||
|
||||
let bnb = BernoulliNB::fit(&x, &y, Default::default()).unwrap();
|
||||
let deserialized_bnb: BernoulliNB<f64, DenseMatrix<f64>> =
|
||||
serde_json::from_str(&serde_json::to_string(&bnb).unwrap()).unwrap();
|
||||
// let bnb = BernoulliNB::fit(&x, &y, Default::default()).unwrap();
|
||||
// let deserialized_bnb: BernoulliNB<i32, u32, DenseMatrix<i32>, Vec<u32>> =
|
||||
// serde_json::from_str(&serde_json::to_string(&bnb).unwrap()).unwrap();
|
||||
|
||||
assert_eq!(bnb, deserialized_bnb);
|
||||
}
|
||||
// assert_eq!(bnb, deserialized_bnb);
|
||||
// }
|
||||
}
|
||||
|
||||
+160
-169
@@ -6,50 +6,51 @@
|
||||
//! Example:
|
||||
//!
|
||||
//! ```
|
||||
//! use smartcore::linalg::naive::dense_matrix::*;
|
||||
//! use smartcore::linalg::basic::matrix::DenseMatrix;
|
||||
//! use smartcore::naive_bayes::categorical::CategoricalNB;
|
||||
//!
|
||||
//! let x = DenseMatrix::from_2d_array(&[
|
||||
//! &[3., 4., 0., 1.],
|
||||
//! &[3., 0., 0., 1.],
|
||||
//! &[4., 4., 1., 2.],
|
||||
//! &[4., 2., 4., 3.],
|
||||
//! &[4., 2., 4., 2.],
|
||||
//! &[4., 1., 1., 0.],
|
||||
//! &[1., 1., 1., 1.],
|
||||
//! &[0., 4., 1., 0.],
|
||||
//! &[0., 3., 2., 1.],
|
||||
//! &[0., 3., 1., 1.],
|
||||
//! &[3., 4., 0., 1.],
|
||||
//! &[3., 4., 2., 4.],
|
||||
//! &[0., 3., 1., 2.],
|
||||
//! &[0., 4., 1., 2.],
|
||||
//! &[3, 4, 0, 1],
|
||||
//! &[3, 0, 0, 1],
|
||||
//! &[4, 4, 1, 2],
|
||||
//! &[4, 2, 4, 3],
|
||||
//! &[4, 2, 4, 2],
|
||||
//! &[4, 1, 1, 0],
|
||||
//! &[1, 1, 1, 1],
|
||||
//! &[0, 4, 1, 0],
|
||||
//! &[0, 3, 2, 1],
|
||||
//! &[0, 3, 1, 1],
|
||||
//! &[3, 4, 0, 1],
|
||||
//! &[3, 4, 2, 4],
|
||||
//! &[0, 3, 1, 2],
|
||||
//! &[0, 4, 1, 2],
|
||||
//! ]);
|
||||
//! let y = vec![0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 1., 1., 1., 0.];
|
||||
//! let y: Vec<u32> = vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0];
|
||||
//!
|
||||
//! let nb = CategoricalNB::fit(&x, &y, Default::default()).unwrap();
|
||||
//! let y_hat = nb.predict(&x).unwrap();
|
||||
//! ```
|
||||
use num_traits::Unsigned;
|
||||
|
||||
use crate::api::{Predictor, SupervisedEstimator};
|
||||
use crate::error::Failed;
|
||||
use crate::linalg::BaseVector;
|
||||
use crate::linalg::Matrix;
|
||||
use crate::math::num::RealNumber;
|
||||
use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1};
|
||||
use crate::naive_bayes::{BaseNaiveBayes, NBDistribution};
|
||||
use crate::numbers::basenum::Number;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Naive Bayes classifier for categorical features
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug)]
|
||||
struct CategoricalNBDistribution<T: RealNumber> {
|
||||
#[derive(Debug, Clone)]
|
||||
struct CategoricalNBDistribution<T: Number + Unsigned> {
|
||||
/// number of training samples observed in each class
|
||||
class_count: Vec<usize>,
|
||||
/// class labels known to the classifier
|
||||
class_labels: Vec<T>,
|
||||
/// probability of each class
|
||||
class_priors: Vec<T>,
|
||||
coefficients: Vec<Vec<Vec<T>>>,
|
||||
class_priors: Vec<f64>,
|
||||
coefficients: Vec<Vec<Vec<f64>>>,
|
||||
/// Number of features of each sample
|
||||
n_features: usize,
|
||||
/// Number of categories for each feature
|
||||
@@ -60,7 +61,7 @@ struct CategoricalNBDistribution<T: RealNumber> {
|
||||
category_count: Vec<Vec<Vec<usize>>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> PartialEq for CategoricalNBDistribution<T> {
|
||||
impl<T: Number + Unsigned> PartialEq for CategoricalNBDistribution<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
if self.class_labels == other.class_labels
|
||||
&& self.class_priors == other.class_priors
|
||||
@@ -80,7 +81,7 @@ impl<T: RealNumber> PartialEq for CategoricalNBDistribution<T> {
|
||||
return false;
|
||||
}
|
||||
for (a_i_j, b_i_j) in a_i.iter().zip(b_i.iter()) {
|
||||
if (*a_i_j - *b_i_j).abs() > T::epsilon() {
|
||||
if (*a_i_j - *b_i_j).abs() > std::f64::EPSILON {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -93,29 +94,29 @@ impl<T: RealNumber> PartialEq for CategoricalNBDistribution<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for CategoricalNBDistribution<T> {
|
||||
fn prior(&self, class_index: usize) -> T {
|
||||
impl<T: Number + Unsigned> NBDistribution<T, T> for CategoricalNBDistribution<T> {
|
||||
fn prior(&self, class_index: usize) -> f64 {
|
||||
if class_index >= self.class_labels.len() {
|
||||
T::zero()
|
||||
0f64
|
||||
} else {
|
||||
self.class_priors[class_index]
|
||||
}
|
||||
}
|
||||
|
||||
fn log_likelihood(&self, class_index: usize, j: &M::RowVector) -> T {
|
||||
fn log_likelihood<'a>(&'a self, class_index: usize, j: &'a Box<dyn ArrayView1<T> + 'a>) -> f64 {
|
||||
if class_index < self.class_labels.len() {
|
||||
let mut likelihood = T::zero();
|
||||
for feature in 0..j.len() {
|
||||
let value = j.get(feature).floor().to_usize().unwrap();
|
||||
let mut likelihood = 0f64;
|
||||
for feature in 0..j.shape() {
|
||||
let value = j.get(feature).to_usize().unwrap();
|
||||
if self.coefficients[feature][class_index].len() > value {
|
||||
likelihood += self.coefficients[feature][class_index][value];
|
||||
} else {
|
||||
return T::zero();
|
||||
return 0f64;
|
||||
}
|
||||
}
|
||||
likelihood
|
||||
} else {
|
||||
T::zero()
|
||||
0f64
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,13 +125,13 @@ impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for CategoricalNBDistribu
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
||||
/// Fits the distribution to a NxM matrix where N is number of samples and M is number of features.
|
||||
/// * `x` - training data.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub fn fit<M: Matrix<T>>(x: &M, y: &M::RowVector, alpha: T) -> Result<Self, Failed> {
|
||||
if alpha < T::zero() {
|
||||
pub fn fit<X: Array2<T>, Y: Array1<T>>(x: &X, y: &Y, alpha: f64) -> Result<Self, Failed> {
|
||||
if alpha < 0f64 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"alpha should be >= 0, alpha=[{}]",
|
||||
alpha
|
||||
@@ -138,7 +139,7 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
}
|
||||
|
||||
let (n_samples, n_features) = x.shape();
|
||||
let y_samples = y.len();
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
@@ -152,11 +153,7 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
let y: Vec<usize> = y
|
||||
.to_vec()
|
||||
.iter()
|
||||
.map(|y_i| y_i.floor().to_usize().unwrap())
|
||||
.collect();
|
||||
let y: Vec<usize> = y.iterator(0).map(|y_i| y_i.to_usize().unwrap()).collect();
|
||||
|
||||
let y_max = y
|
||||
.iter()
|
||||
@@ -164,7 +161,7 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
.ok_or_else(|| Failed::fit("Failed to get the labels of y."))?;
|
||||
|
||||
let class_labels: Vec<T> = (0..*y_max + 1)
|
||||
.map(|label| T::from(label).unwrap())
|
||||
.map(|label| T::from_usize(label).unwrap())
|
||||
.collect();
|
||||
let mut class_count = vec![0_usize; class_labels.len()];
|
||||
for elem in y.iter() {
|
||||
@@ -174,9 +171,9 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
let mut n_categories: Vec<usize> = Vec::with_capacity(n_features);
|
||||
for feature in 0..n_features {
|
||||
let feature_max = x
|
||||
.get_col_as_vec(feature)
|
||||
.iter()
|
||||
.map(|f_i| f_i.floor().to_usize().unwrap())
|
||||
.get_col(feature)
|
||||
.iterator(0)
|
||||
.map(|f_i| f_i.to_usize().unwrap())
|
||||
.max()
|
||||
.ok_or_else(|| {
|
||||
Failed::fit(&format!(
|
||||
@@ -187,34 +184,32 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
n_categories.push(feature_max + 1);
|
||||
}
|
||||
|
||||
let mut coefficients: Vec<Vec<Vec<T>>> = Vec::with_capacity(class_labels.len());
|
||||
let mut coefficients: Vec<Vec<Vec<f64>>> = Vec::with_capacity(class_labels.len());
|
||||
let mut category_count: Vec<Vec<Vec<usize>>> = Vec::with_capacity(class_labels.len());
|
||||
for (feature_index, &n_categories_i) in n_categories.iter().enumerate().take(n_features) {
|
||||
let mut coef_i: Vec<Vec<T>> = Vec::with_capacity(n_features);
|
||||
let mut coef_i: Vec<Vec<f64>> = Vec::with_capacity(n_features);
|
||||
let mut category_count_i: Vec<Vec<usize>> = Vec::with_capacity(n_features);
|
||||
for (label, &label_count) in class_labels.iter().zip(class_count.iter()) {
|
||||
let col = x
|
||||
.get_col_as_vec(feature_index)
|
||||
.iter()
|
||||
.get_col(feature_index)
|
||||
.iterator(0)
|
||||
.enumerate()
|
||||
.filter(|(i, _j)| T::from(y[*i]).unwrap() == *label)
|
||||
.filter(|(i, _j)| T::from_usize(y[*i]).unwrap() == *label)
|
||||
.map(|(_, j)| *j)
|
||||
.collect::<Vec<T>>();
|
||||
let mut feat_count: Vec<usize> = vec![0_usize; n_categories_i];
|
||||
for row in col.iter() {
|
||||
let index = row.floor().to_usize().unwrap();
|
||||
let index = row.to_usize().unwrap();
|
||||
feat_count[index] += 1;
|
||||
}
|
||||
|
||||
let coef_i_j = feat_count
|
||||
.iter()
|
||||
.map(|c| {
|
||||
((T::from(*c).unwrap() + alpha)
|
||||
/ (T::from(label_count).unwrap()
|
||||
+ T::from(n_categories_i).unwrap() * alpha))
|
||||
.map(|&c| {
|
||||
((c as f64 + alpha) / (label_count as f64 + n_categories_i as f64 * alpha))
|
||||
.ln()
|
||||
})
|
||||
.collect::<Vec<T>>();
|
||||
.collect::<Vec<f64>>();
|
||||
category_count_i.push(feat_count);
|
||||
coef_i.push(coef_i_j);
|
||||
}
|
||||
@@ -224,8 +219,8 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
|
||||
let class_priors = class_count
|
||||
.iter()
|
||||
.map(|&count| T::from(count).unwrap() / T::from(n_samples).unwrap())
|
||||
.collect::<Vec<T>>();
|
||||
.map(|&count| count as f64 / n_samples as f64)
|
||||
.collect::<Vec<f64>>();
|
||||
|
||||
Ok(Self {
|
||||
class_count,
|
||||
@@ -242,44 +237,44 @@ impl<T: RealNumber> CategoricalNBDistribution<T> {
|
||||
/// `CategoricalNB` parameters. Use `Default::default()` for default values.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CategoricalNBParameters<T: RealNumber> {
|
||||
pub struct CategoricalNBParameters {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub alpha: T,
|
||||
pub alpha: f64,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> CategoricalNBParameters<T> {
|
||||
impl CategoricalNBParameters {
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub fn with_alpha(mut self, alpha: T) -> Self {
|
||||
pub fn with_alpha(mut self, alpha: f64) -> Self {
|
||||
self.alpha = alpha;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for CategoricalNBParameters<T> {
|
||||
impl Default for CategoricalNBParameters {
|
||||
fn default() -> Self {
|
||||
Self { alpha: T::one() }
|
||||
Self { alpha: 1f64 }
|
||||
}
|
||||
}
|
||||
|
||||
/// CategoricalNB grid search parameters
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CategoricalNBSearchParameters<T: RealNumber> {
|
||||
pub struct CategoricalNBSearchParameters {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub alpha: Vec<T>,
|
||||
pub alpha: Vec<f64>,
|
||||
}
|
||||
|
||||
/// CategoricalNB grid search iterator
|
||||
pub struct CategoricalNBSearchParametersIterator<T: RealNumber> {
|
||||
categorical_nb_search_parameters: CategoricalNBSearchParameters<T>,
|
||||
pub struct CategoricalNBSearchParametersIterator {
|
||||
categorical_nb_search_parameters: CategoricalNBSearchParameters,
|
||||
current_alpha: usize,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> IntoIterator for CategoricalNBSearchParameters<T> {
|
||||
type Item = CategoricalNBParameters<T>;
|
||||
type IntoIter = CategoricalNBSearchParametersIterator<T>;
|
||||
impl IntoIterator for CategoricalNBSearchParameters {
|
||||
type Item = CategoricalNBParameters;
|
||||
type IntoIter = CategoricalNBSearchParametersIterator;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
CategoricalNBSearchParametersIterator {
|
||||
@@ -289,8 +284,8 @@ impl<T: RealNumber> IntoIterator for CategoricalNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Iterator for CategoricalNBSearchParametersIterator<T> {
|
||||
type Item = CategoricalNBParameters<T>;
|
||||
impl Iterator for CategoricalNBSearchParametersIterator {
|
||||
type Item = CategoricalNBParameters;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_alpha == self.categorical_nb_search_parameters.alpha.len() {
|
||||
@@ -307,7 +302,7 @@ impl<T: RealNumber> Iterator for CategoricalNBSearchParametersIterator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for CategoricalNBSearchParameters<T> {
|
||||
impl Default for CategoricalNBSearchParameters {
|
||||
fn default() -> Self {
|
||||
let default_params = CategoricalNBParameters::default();
|
||||
|
||||
@@ -320,92 +315,90 @@ impl<T: RealNumber> Default for CategoricalNBSearchParameters<T> {
|
||||
/// CategoricalNB implements the categorical naive Bayes algorithm for categorically distributed data.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct CategoricalNB<T: RealNumber, M: Matrix<T>> {
|
||||
inner: BaseNaiveBayes<T, M, CategoricalNBDistribution<T>>,
|
||||
pub struct CategoricalNB<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> {
|
||||
inner: Option<BaseNaiveBayes<T, T, X, Y, CategoricalNBDistribution<T>>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> SupervisedEstimator<M, M::RowVector, CategoricalNBParameters<T>>
|
||||
for CategoricalNB<T, M>
|
||||
impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>>
|
||||
SupervisedEstimator<X, Y, CategoricalNBParameters> for CategoricalNB<T, X, Y>
|
||||
{
|
||||
fn fit(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
parameters: CategoricalNBParameters<T>,
|
||||
) -> Result<Self, Failed> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
inner: Option::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn fit(x: &X, y: &Y, parameters: CategoricalNBParameters) -> Result<Self, Failed> {
|
||||
CategoricalNB::fit(x, y, parameters)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> Predictor<M, M::RowVector> for CategoricalNB<T, M> {
|
||||
fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> Predictor<X, Y> for CategoricalNB<T, X, Y> {
|
||||
fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.predict(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> CategoricalNB<T, M> {
|
||||
impl<T: Number + Unsigned, X: Array2<T>, Y: Array1<T>> CategoricalNB<T, X, Y> {
|
||||
/// Fits CategoricalNB with given data
|
||||
/// * `x` - training data of size NxM where N is the number of samples and M is the number of
|
||||
/// features.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `parameters` - additional parameters like alpha for smoothing
|
||||
pub fn fit(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
parameters: CategoricalNBParameters<T>,
|
||||
) -> Result<Self, Failed> {
|
||||
pub fn fit(x: &X, y: &Y, parameters: CategoricalNBParameters) -> Result<Self, Failed> {
|
||||
let alpha = parameters.alpha;
|
||||
let distribution = CategoricalNBDistribution::fit(x, y, alpha)?;
|
||||
let inner = BaseNaiveBayes::fit(distribution)?;
|
||||
Ok(Self { inner })
|
||||
Ok(Self { inner: Some(inner) })
|
||||
}
|
||||
|
||||
/// Estimates the class labels for the provided data.
|
||||
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
|
||||
/// Returns a vector of size N with class estimates.
|
||||
pub fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
self.inner.predict(x)
|
||||
pub fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.inner.as_ref().unwrap().predict(x)
|
||||
}
|
||||
|
||||
/// Class labels known to the classifier.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn classes(&self) -> &Vec<T> {
|
||||
&self.inner.distribution.class_labels
|
||||
&self.inner.as_ref().unwrap().distribution.class_labels
|
||||
}
|
||||
|
||||
/// Number of training samples observed in each class.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn class_count(&self) -> &Vec<usize> {
|
||||
&self.inner.distribution.class_count
|
||||
&self.inner.as_ref().unwrap().distribution.class_count
|
||||
}
|
||||
|
||||
/// Number of features of each sample
|
||||
pub fn n_features(&self) -> usize {
|
||||
self.inner.distribution.n_features
|
||||
self.inner.as_ref().unwrap().distribution.n_features
|
||||
}
|
||||
|
||||
/// Number of features of each sample
|
||||
pub fn n_categories(&self) -> &Vec<usize> {
|
||||
&self.inner.distribution.n_categories
|
||||
&self.inner.as_ref().unwrap().distribution.n_categories
|
||||
}
|
||||
|
||||
/// Holds arrays of shape (n_classes, n_categories of respective feature)
|
||||
/// for each feature. Each array provides the number of samples
|
||||
/// encountered for each class and category of the specific feature.
|
||||
pub fn category_count(&self) -> &Vec<Vec<Vec<usize>>> {
|
||||
&self.inner.distribution.category_count
|
||||
&self.inner.as_ref().unwrap().distribution.category_count
|
||||
}
|
||||
/// Holds arrays of shape (n_classes, n_categories of respective feature)
|
||||
/// for each feature. Each array provides the empirical log probability
|
||||
/// of categories given the respective feature and class, ``P(x_i|y)``.
|
||||
pub fn feature_log_prob(&self) -> &Vec<Vec<Vec<T>>> {
|
||||
&self.inner.distribution.coefficients
|
||||
pub fn feature_log_prob(&self) -> &Vec<Vec<Vec<f64>>> {
|
||||
&self.inner.as_ref().unwrap().distribution.coefficients
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::linalg::naive::dense_matrix::DenseMatrix;
|
||||
use crate::linalg::basic::matrix::DenseMatrix;
|
||||
|
||||
#[test]
|
||||
fn search_parameters() {
|
||||
@@ -424,28 +417,28 @@ mod tests {
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn run_categorical_naive_bayes() {
|
||||
let x = DenseMatrix::from_2d_array(&[
|
||||
&[0., 2., 1., 0.],
|
||||
&[0., 2., 1., 1.],
|
||||
&[1., 2., 1., 0.],
|
||||
&[2., 1., 1., 0.],
|
||||
&[2., 0., 0., 0.],
|
||||
&[2., 0., 0., 1.],
|
||||
&[1., 0., 0., 1.],
|
||||
&[0., 1., 1., 0.],
|
||||
&[0., 0., 0., 0.],
|
||||
&[2., 1., 0., 0.],
|
||||
&[0., 1., 0., 1.],
|
||||
&[1., 1., 1., 1.],
|
||||
&[1., 2., 0., 0.],
|
||||
&[2., 1., 1., 1.],
|
||||
let x = DenseMatrix::<u32>::from_2d_array(&[
|
||||
&[0, 2, 1, 0],
|
||||
&[0, 2, 1, 1],
|
||||
&[1, 2, 1, 0],
|
||||
&[2, 1, 1, 0],
|
||||
&[2, 0, 0, 0],
|
||||
&[2, 0, 0, 1],
|
||||
&[1, 0, 0, 1],
|
||||
&[0, 1, 1, 0],
|
||||
&[0, 0, 0, 0],
|
||||
&[2, 1, 0, 0],
|
||||
&[0, 1, 0, 1],
|
||||
&[1, 1, 1, 1],
|
||||
&[1, 2, 0, 0],
|
||||
&[2, 1, 1, 1],
|
||||
]);
|
||||
let y = vec![0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 1., 1., 1., 0.];
|
||||
let y: Vec<u32> = vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0];
|
||||
|
||||
let cnb = CategoricalNB::fit(&x, &y, Default::default()).unwrap();
|
||||
|
||||
// checking parity with scikit
|
||||
assert_eq!(cnb.classes(), &[0., 1.]);
|
||||
assert_eq!(cnb.classes(), &[0, 1]);
|
||||
assert_eq!(cnb.class_count(), &[5, 9]);
|
||||
assert_eq!(cnb.n_features(), 4);
|
||||
assert_eq!(cnb.n_categories(), &[3, 3, 2, 2]);
|
||||
@@ -497,67 +490,65 @@ mod tests {
|
||||
]
|
||||
);
|
||||
|
||||
let x_test = DenseMatrix::from_2d_array(&[&[0., 2., 1., 0.], &[2., 2., 0., 0.]]);
|
||||
let x_test = DenseMatrix::from_2d_array(&[&[0, 2, 1, 0], &[2, 2, 0, 0]]);
|
||||
let y_hat = cnb.predict(&x_test).unwrap();
|
||||
assert_eq!(y_hat, vec![0., 1.]);
|
||||
assert_eq!(y_hat, vec![0, 1]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn run_categorical_naive_bayes2() {
|
||||
let x = DenseMatrix::from_2d_array(&[
|
||||
&[3., 4., 0., 1.],
|
||||
&[3., 0., 0., 1.],
|
||||
&[4., 4., 1., 2.],
|
||||
&[4., 2., 4., 3.],
|
||||
&[4., 2., 4., 2.],
|
||||
&[4., 1., 1., 0.],
|
||||
&[1., 1., 1., 1.],
|
||||
&[0., 4., 1., 0.],
|
||||
&[0., 3., 2., 1.],
|
||||
&[0., 3., 1., 1.],
|
||||
&[3., 4., 0., 1.],
|
||||
&[3., 4., 2., 4.],
|
||||
&[0., 3., 1., 2.],
|
||||
&[0., 4., 1., 2.],
|
||||
let x = DenseMatrix::<u32>::from_2d_array(&[
|
||||
&[3, 4, 0, 1],
|
||||
&[3, 0, 0, 1],
|
||||
&[4, 4, 1, 2],
|
||||
&[4, 2, 4, 3],
|
||||
&[4, 2, 4, 2],
|
||||
&[4, 1, 1, 0],
|
||||
&[1, 1, 1, 1],
|
||||
&[0, 4, 1, 0],
|
||||
&[0, 3, 2, 1],
|
||||
&[0, 3, 1, 1],
|
||||
&[3, 4, 0, 1],
|
||||
&[3, 4, 2, 4],
|
||||
&[0, 3, 1, 2],
|
||||
&[0, 4, 1, 2],
|
||||
]);
|
||||
let y = vec![0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 1., 1., 1., 0.];
|
||||
let y: Vec<u32> = vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0];
|
||||
|
||||
let cnb = CategoricalNB::fit(&x, &y, Default::default()).unwrap();
|
||||
let y_hat = cnb.predict(&x).unwrap();
|
||||
assert_eq!(
|
||||
y_hat,
|
||||
vec![0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 0., 1., 1., 1.]
|
||||
);
|
||||
assert_eq!(y_hat, vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
#[cfg(feature = "serde")]
|
||||
fn serde() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[3., 4., 0., 1.],
|
||||
&[3., 0., 0., 1.],
|
||||
&[4., 4., 1., 2.],
|
||||
&[4., 2., 4., 3.],
|
||||
&[4., 2., 4., 2.],
|
||||
&[4., 1., 1., 0.],
|
||||
&[1., 1., 1., 1.],
|
||||
&[0., 4., 1., 0.],
|
||||
&[0., 3., 2., 1.],
|
||||
&[0., 3., 1., 1.],
|
||||
&[3., 4., 0., 1.],
|
||||
&[3., 4., 2., 4.],
|
||||
&[0., 3., 1., 2.],
|
||||
&[0., 4., 1., 2.],
|
||||
]);
|
||||
// TODO: implement serialization
|
||||
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
// #[test]
|
||||
// #[cfg(feature = "serde")]
|
||||
// fn serde() {
|
||||
// let x = DenseMatrix::from_2d_array(&[
|
||||
// &[3, 4, 0, 1],
|
||||
// &[3, 0, 0, 1],
|
||||
// &[4, 4, 1, 2],
|
||||
// &[4, 2, 4, 3],
|
||||
// &[4, 2, 4, 2],
|
||||
// &[4, 1, 1, 0],
|
||||
// &[1, 1, 1, 1],
|
||||
// &[0, 4, 1, 0],
|
||||
// &[0, 3, 2, 1],
|
||||
// &[0, 3, 1, 1],
|
||||
// &[3, 4, 0, 1],
|
||||
// &[3, 4, 2, 4],
|
||||
// &[0, 3, 1, 2],
|
||||
// &[0, 4, 1, 2],
|
||||
// ]);
|
||||
|
||||
let y = vec![0., 0., 1., 1., 1., 0., 1., 0., 1., 1., 1., 1., 1., 0.];
|
||||
let cnb = CategoricalNB::fit(&x, &y, Default::default()).unwrap();
|
||||
// let y: Vec<u32> = vec![0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0];
|
||||
// let cnb = CategoricalNB::fit(&x, &y, Default::default()).unwrap();
|
||||
|
||||
let deserialized_cnb: CategoricalNB<f64, DenseMatrix<f64>> =
|
||||
serde_json::from_str(&serde_json::to_string(&cnb).unwrap()).unwrap();
|
||||
// let deserialized_cnb: CategoricalNB<u32, DenseMatrix<u32>, Vec<u32>> =
|
||||
// serde_json::from_str(&serde_json::to_string(&cnb).unwrap()).unwrap();
|
||||
|
||||
assert_eq!(cnb, deserialized_cnb);
|
||||
}
|
||||
// assert_eq!(cnb, deserialized_cnb);
|
||||
// }
|
||||
}
|
||||
|
||||
+135
-112
@@ -6,7 +6,7 @@
|
||||
//! Example:
|
||||
//!
|
||||
//! ```
|
||||
//! use smartcore::linalg::naive::dense_matrix::*;
|
||||
//! use smartcore::linalg::basic::matrix::DenseMatrix;
|
||||
//! use smartcore::naive_bayes::gaussian::GaussianNB;
|
||||
//!
|
||||
//! let x = DenseMatrix::from_2d_array(&[
|
||||
@@ -17,51 +17,53 @@
|
||||
//! &[ 2., 1.],
|
||||
//! &[ 3., 2.],
|
||||
//! ]);
|
||||
//! let y = vec![1., 1., 1., 2., 2., 2.];
|
||||
//! let y: Vec<u32> = vec![1, 1, 1, 2, 2, 2];
|
||||
//!
|
||||
//! let nb = GaussianNB::fit(&x, &y, Default::default()).unwrap();
|
||||
//! let y_hat = nb.predict(&x).unwrap();
|
||||
//! ```
|
||||
use num_traits::Unsigned;
|
||||
|
||||
use crate::api::{Predictor, SupervisedEstimator};
|
||||
use crate::error::Failed;
|
||||
use crate::linalg::row_iter;
|
||||
use crate::linalg::BaseVector;
|
||||
use crate::linalg::Matrix;
|
||||
use crate::math::num::RealNumber;
|
||||
use crate::math::vector::RealNumberVector;
|
||||
use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1};
|
||||
use crate::naive_bayes::{BaseNaiveBayes, NBDistribution};
|
||||
use crate::numbers::basenum::Number;
|
||||
use crate::numbers::realnum::RealNumber;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Naive Bayes classifier using Gaussian distribution
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct GaussianNBDistribution<T: RealNumber> {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
struct GaussianNBDistribution<T: Number> {
|
||||
/// class labels known to the classifier
|
||||
class_labels: Vec<T>,
|
||||
/// number of training samples observed in each class
|
||||
class_count: Vec<usize>,
|
||||
/// probability of each class.
|
||||
class_priors: Vec<T>,
|
||||
class_priors: Vec<f64>,
|
||||
/// variance of each feature per class
|
||||
var: Vec<Vec<T>>,
|
||||
var: Vec<Vec<f64>>,
|
||||
/// mean of each feature per class
|
||||
theta: Vec<Vec<T>>,
|
||||
theta: Vec<Vec<f64>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for GaussianNBDistribution<T> {
|
||||
fn prior(&self, class_index: usize) -> T {
|
||||
impl<X: Number + RealNumber, Y: Number + Ord + Unsigned> NBDistribution<X, Y>
|
||||
for GaussianNBDistribution<Y>
|
||||
{
|
||||
fn prior(&self, class_index: usize) -> f64 {
|
||||
if class_index >= self.class_labels.len() {
|
||||
T::zero()
|
||||
0f64
|
||||
} else {
|
||||
self.class_priors[class_index]
|
||||
}
|
||||
}
|
||||
|
||||
fn log_likelihood(&self, class_index: usize, j: &M::RowVector) -> T {
|
||||
let mut likelihood = T::zero();
|
||||
for feature in 0..j.len() {
|
||||
let value = j.get(feature);
|
||||
fn log_likelihood<'a>(&self, class_index: usize, j: &'a Box<dyn ArrayView1<X> + 'a>) -> f64 {
|
||||
let mut likelihood = 0f64;
|
||||
for feature in 0..j.shape() {
|
||||
let value = X::to_f64(j.get(feature)).unwrap();
|
||||
let mean = self.theta[class_index][feature];
|
||||
let variance = self.var[class_index][feature];
|
||||
likelihood += self.calculate_log_probability(value, mean, variance);
|
||||
@@ -69,52 +71,54 @@ impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for GaussianNBDistributio
|
||||
likelihood
|
||||
}
|
||||
|
||||
fn classes(&self) -> &Vec<T> {
|
||||
fn classes(&self) -> &Vec<Y> {
|
||||
&self.class_labels
|
||||
}
|
||||
}
|
||||
|
||||
/// `GaussianNB` parameters. Use `Default::default()` for default values.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GaussianNBParameters<T: RealNumber> {
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct GaussianNBParameters {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub priors: Option<Vec<T>>,
|
||||
pub priors: Option<Vec<f64>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> GaussianNBParameters<T> {
|
||||
impl GaussianNBParameters {
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub fn with_priors(mut self, priors: Vec<T>) -> Self {
|
||||
pub fn with_priors(mut self, priors: Vec<f64>) -> Self {
|
||||
self.priors = Some(priors);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for GaussianNBParameters<T> {
|
||||
impl GaussianNBParameters {
|
||||
fn default() -> Self {
|
||||
Self { priors: None }
|
||||
Self {
|
||||
priors: Option::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// GaussianNB grid search parameters
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GaussianNBSearchParameters<T: RealNumber> {
|
||||
pub struct GaussianNBSearchParameters {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub priors: Vec<Option<Vec<T>>>,
|
||||
pub priors: Vec<Option<Vec<f64>>>,
|
||||
}
|
||||
|
||||
/// GaussianNB grid search iterator
|
||||
pub struct GaussianNBSearchParametersIterator<T: RealNumber> {
|
||||
gaussian_nb_search_parameters: GaussianNBSearchParameters<T>,
|
||||
pub struct GaussianNBSearchParametersIterator {
|
||||
gaussian_nb_search_parameters: GaussianNBSearchParameters,
|
||||
current_priors: usize,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> IntoIterator for GaussianNBSearchParameters<T> {
|
||||
type Item = GaussianNBParameters<T>;
|
||||
type IntoIter = GaussianNBSearchParametersIterator<T>;
|
||||
impl IntoIterator for GaussianNBSearchParameters {
|
||||
type Item = GaussianNBParameters;
|
||||
type IntoIter = GaussianNBSearchParametersIterator;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
GaussianNBSearchParametersIterator {
|
||||
@@ -124,8 +128,8 @@ impl<T: RealNumber> IntoIterator for GaussianNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Iterator for GaussianNBSearchParametersIterator<T> {
|
||||
type Item = GaussianNBParameters<T>;
|
||||
impl Iterator for GaussianNBSearchParametersIterator {
|
||||
type Item = GaussianNBParameters;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_priors == self.gaussian_nb_search_parameters.priors.len() {
|
||||
@@ -142,7 +146,7 @@ impl<T: RealNumber> Iterator for GaussianNBSearchParametersIterator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for GaussianNBSearchParameters<T> {
|
||||
impl Default for GaussianNBSearchParameters {
|
||||
fn default() -> Self {
|
||||
let default_params = GaussianNBParameters::default();
|
||||
|
||||
@@ -152,19 +156,19 @@ impl<T: RealNumber> Default for GaussianNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> GaussianNBDistribution<T> {
|
||||
impl<TY: Number + Ord + Unsigned> GaussianNBDistribution<TY> {
|
||||
/// Fits the distribution to a NxM matrix where N is number of samples and M is number of features.
|
||||
/// * `x` - training data.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `priors` - Optional vector with prior probabilities of the classes. If not defined,
|
||||
/// priors are adjusted according to the data.
|
||||
pub fn fit<M: Matrix<T>>(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
priors: Option<Vec<T>>,
|
||||
pub fn fit<TX: Number + RealNumber, X: Array2<TX>, Y: Array1<TY>>(
|
||||
x: &X,
|
||||
y: &Y,
|
||||
priors: Option<Vec<f64>>,
|
||||
) -> Result<Self, Failed> {
|
||||
let (n_samples, n_features) = x.shape();
|
||||
let y_samples = y.len();
|
||||
let (n_samples, _) = x.shape();
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
@@ -178,14 +182,14 @@ impl<T: RealNumber> GaussianNBDistribution<T> {
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
let y = y.to_vec();
|
||||
let (class_labels, indices) = <Vec<T> as RealNumberVector<T>>::unique_with_indices(&y);
|
||||
let (class_labels, indices) = y.unique_with_indices();
|
||||
|
||||
let mut class_count = vec![0_usize; class_labels.len()];
|
||||
|
||||
let mut subdataset: Vec<Vec<Vec<T>>> = vec![vec![]; class_labels.len()];
|
||||
let mut subdataset: Vec<Vec<Box<dyn ArrayView1<TX>>>> =
|
||||
(0..class_labels.len()).map(|_| vec![]).collect();
|
||||
|
||||
for (row, class_index) in row_iter(x).zip(indices.iter()) {
|
||||
for (row, class_index) in x.row_iter().zip(indices.iter()) {
|
||||
class_count[*class_index] += 1;
|
||||
subdataset[*class_index].push(row);
|
||||
}
|
||||
@@ -200,26 +204,25 @@ impl<T: RealNumber> GaussianNBDistribution<T> {
|
||||
} else {
|
||||
class_count
|
||||
.iter()
|
||||
.map(|&c| T::from(c).unwrap() / T::from(n_samples).unwrap())
|
||||
.map(|&c| c as f64 / n_samples as f64)
|
||||
.collect()
|
||||
};
|
||||
|
||||
let subdataset: Vec<M> = subdataset
|
||||
.into_iter()
|
||||
let subdataset: Vec<X> = subdataset
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let mut m = M::zeros(v.len(), n_features);
|
||||
for (row_i, v_i) in v.iter().enumerate() {
|
||||
for (col_j, v_i_j) in v_i.iter().enumerate().take(n_features) {
|
||||
m.set(row_i, col_j, *v_i_j);
|
||||
}
|
||||
}
|
||||
m
|
||||
X::concatenate_1d(
|
||||
&v.iter()
|
||||
.map(|v| v.as_ref())
|
||||
.collect::<Vec<&dyn ArrayView1<TX>>>(),
|
||||
0,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let (var, theta): (Vec<Vec<T>>, Vec<Vec<T>>) = subdataset
|
||||
let (var, theta): (Vec<Vec<f64>>, Vec<Vec<f64>>) = subdataset
|
||||
.iter()
|
||||
.map(|data| (data.var(0), data.mean(0)))
|
||||
.map(|data| (data.variance(0), data.mean_by(0)))
|
||||
.unzip();
|
||||
|
||||
Ok(Self {
|
||||
@@ -233,11 +236,11 @@ impl<T: RealNumber> GaussianNBDistribution<T> {
|
||||
|
||||
/// Calculate probability of x equals to a value of a Gaussian distribution given its mean and its
|
||||
/// variance.
|
||||
fn calculate_log_probability(&self, value: T, mean: T, variance: T) -> T {
|
||||
let pi = T::from(std::f64::consts::PI).unwrap();
|
||||
-((value - mean).powf(T::two()) / (T::two() * variance))
|
||||
- (T::two() * pi).ln() / T::two()
|
||||
- (variance).ln() / T::two()
|
||||
fn calculate_log_probability(&self, value: f64, mean: f64, variance: f64) -> f64 {
|
||||
let pi = std::f64::consts::PI;
|
||||
-((value - mean).powf(2.0) / (2.0 * variance))
|
||||
- (2.0 * pi).ln() / 2.0
|
||||
- (variance).ln() / 2.0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,82 +248,101 @@ impl<T: RealNumber> GaussianNBDistribution<T> {
|
||||
/// distribution.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct GaussianNB<T: RealNumber, M: Matrix<T>> {
|
||||
inner: BaseNaiveBayes<T, M, GaussianNBDistribution<T>>,
|
||||
pub struct GaussianNB<
|
||||
TX: Number + RealNumber + RealNumber,
|
||||
TY: Number + Ord + Unsigned,
|
||||
X: Array2<TX>,
|
||||
Y: Array1<TY>,
|
||||
> {
|
||||
inner: Option<BaseNaiveBayes<TX, TY, X, Y, GaussianNBDistribution<TY>>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> SupervisedEstimator<M, M::RowVector, GaussianNBParameters<T>>
|
||||
for GaussianNB<T, M>
|
||||
impl<
|
||||
TX: Number + RealNumber + RealNumber,
|
||||
TY: Number + Ord + Unsigned,
|
||||
X: Array2<TX>,
|
||||
Y: Array1<TY>,
|
||||
> SupervisedEstimator<X, Y, GaussianNBParameters> for GaussianNB<TX, TY, X, Y>
|
||||
{
|
||||
fn fit(x: &M, y: &M::RowVector, parameters: GaussianNBParameters<T>) -> Result<Self, Failed> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
inner: Option::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn fit(x: &X, y: &Y, parameters: GaussianNBParameters) -> Result<Self, Failed> {
|
||||
GaussianNB::fit(x, y, parameters)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> Predictor<M, M::RowVector> for GaussianNB<T, M> {
|
||||
fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
impl<
|
||||
TX: Number + RealNumber + RealNumber,
|
||||
TY: Number + Ord + Unsigned,
|
||||
X: Array2<TX>,
|
||||
Y: Array1<TY>,
|
||||
> Predictor<X, Y> for GaussianNB<TX, TY, X, Y>
|
||||
{
|
||||
fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.predict(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> GaussianNB<T, M> {
|
||||
impl<TX: Number + RealNumber, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
GaussianNB<TX, TY, X, Y>
|
||||
{
|
||||
/// Fits GaussianNB with given data
|
||||
/// * `x` - training data of size NxM where N is the number of samples and M is the number of
|
||||
/// features.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `parameters` - additional parameters like class priors.
|
||||
pub fn fit(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
parameters: GaussianNBParameters<T>,
|
||||
) -> Result<Self, Failed> {
|
||||
pub fn fit(x: &X, y: &Y, parameters: GaussianNBParameters) -> Result<Self, Failed> {
|
||||
let distribution = GaussianNBDistribution::fit(x, y, parameters.priors)?;
|
||||
let inner = BaseNaiveBayes::fit(distribution)?;
|
||||
Ok(Self { inner })
|
||||
Ok(Self { inner: Some(inner) })
|
||||
}
|
||||
|
||||
/// Estimates the class labels for the provided data.
|
||||
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
|
||||
/// Returns a vector of size N with class estimates.
|
||||
pub fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
self.inner.predict(x)
|
||||
pub fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.inner.as_ref().unwrap().predict(x)
|
||||
}
|
||||
|
||||
/// Class labels known to the classifier.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn classes(&self) -> &Vec<T> {
|
||||
&self.inner.distribution.class_labels
|
||||
pub fn classes(&self) -> &Vec<TY> {
|
||||
&self.inner.as_ref().unwrap().distribution.class_labels
|
||||
}
|
||||
|
||||
/// Number of training samples observed in each class.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn class_count(&self) -> &Vec<usize> {
|
||||
&self.inner.distribution.class_count
|
||||
&self.inner.as_ref().unwrap().distribution.class_count
|
||||
}
|
||||
|
||||
/// Probability of each class
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn class_priors(&self) -> &Vec<T> {
|
||||
&self.inner.distribution.class_priors
|
||||
pub fn class_priors(&self) -> &Vec<f64> {
|
||||
&self.inner.as_ref().unwrap().distribution.class_priors
|
||||
}
|
||||
|
||||
/// Mean of each feature per class
|
||||
/// Returns a 2d vector of shape (n_classes, n_features).
|
||||
pub fn theta(&self) -> &Vec<Vec<T>> {
|
||||
&self.inner.distribution.theta
|
||||
pub fn theta(&self) -> &Vec<Vec<f64>> {
|
||||
&self.inner.as_ref().unwrap().distribution.theta
|
||||
}
|
||||
|
||||
/// Variance of each feature per class
|
||||
/// Returns a 2d vector of shape (n_classes, n_features).
|
||||
pub fn var(&self) -> &Vec<Vec<T>> {
|
||||
&self.inner.distribution.var
|
||||
pub fn var(&self) -> &Vec<Vec<f64>> {
|
||||
&self.inner.as_ref().unwrap().distribution.var
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::linalg::naive::dense_matrix::DenseMatrix;
|
||||
use crate::linalg::basic::matrix::DenseMatrix;
|
||||
|
||||
#[test]
|
||||
fn search_parameters() {
|
||||
@@ -347,13 +369,13 @@ mod tests {
|
||||
&[2., 1.],
|
||||
&[3., 2.],
|
||||
]);
|
||||
let y = vec![1., 1., 1., 2., 2., 2.];
|
||||
let y: Vec<u32> = vec![1, 1, 1, 2, 2, 2];
|
||||
|
||||
let gnb = GaussianNB::fit(&x, &y, Default::default()).unwrap();
|
||||
let y_hat = gnb.predict(&x).unwrap();
|
||||
assert_eq!(y_hat, y);
|
||||
|
||||
assert_eq!(gnb.classes(), &[1., 2.]);
|
||||
assert_eq!(gnb.classes(), &[1, 2]);
|
||||
|
||||
assert_eq!(gnb.class_count(), &[3, 3]);
|
||||
|
||||
@@ -384,7 +406,7 @@ mod tests {
|
||||
&[2., 1.],
|
||||
&[3., 2.],
|
||||
]);
|
||||
let y = vec![1., 1., 1., 2., 2., 2.];
|
||||
let y: Vec<u32> = vec![1, 1, 1, 2, 2, 2];
|
||||
|
||||
let priors = vec![0.3, 0.7];
|
||||
let parameters = GaussianNBParameters::default().with_priors(priors.clone());
|
||||
@@ -393,24 +415,25 @@ mod tests {
|
||||
assert_eq!(gnb.class_priors(), &priors);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
#[cfg(feature = "serde")]
|
||||
fn serde() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[-1., -1.],
|
||||
&[-2., -1.],
|
||||
&[-3., -2.],
|
||||
&[1., 1.],
|
||||
&[2., 1.],
|
||||
&[3., 2.],
|
||||
]);
|
||||
let y = vec![1., 1., 1., 2., 2., 2.];
|
||||
// TODO: implement serialization
|
||||
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
// #[test]
|
||||
// #[cfg(feature = "serde")]
|
||||
// fn serde() {
|
||||
// let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
// &[-1., -1.],
|
||||
// &[-2., -1.],
|
||||
// &[-3., -2.],
|
||||
// &[1., 1.],
|
||||
// &[2., 1.],
|
||||
// &[3., 2.],
|
||||
// ]);
|
||||
// let y: Vec<u32> = vec![1, 1, 1, 2, 2, 2];
|
||||
|
||||
let gnb = GaussianNB::fit(&x, &y, Default::default()).unwrap();
|
||||
let deserialized_gnb: GaussianNB<f64, DenseMatrix<f64>> =
|
||||
serde_json::from_str(&serde_json::to_string(&gnb).unwrap()).unwrap();
|
||||
// let gnb = GaussianNB::fit(&x, &y, Default::default()).unwrap();
|
||||
// let deserialized_gnb: GaussianNB<f64, u32, DenseMatrix<f64>, Vec<u32>> =
|
||||
// serde_json::from_str(&serde_json::to_string(&gnb).unwrap()).unwrap();
|
||||
|
||||
assert_eq!(gnb, deserialized_gnb);
|
||||
}
|
||||
// assert_eq!(gnb, deserialized_gnb);
|
||||
// }
|
||||
}
|
||||
|
||||
+29
-17
@@ -36,49 +36,61 @@
|
||||
//! <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
|
||||
//! <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||
use crate::error::Failed;
|
||||
use crate::linalg::BaseVector;
|
||||
use crate::linalg::Matrix;
|
||||
use crate::math::num::RealNumber;
|
||||
use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1};
|
||||
use crate::numbers::basenum::Number;
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
/// Distribution used in the Naive Bayes classifier.
|
||||
pub(crate) trait NBDistribution<T: RealNumber, M: Matrix<T>> {
|
||||
pub(crate) trait NBDistribution<X: Number, Y: Number>: Clone {
|
||||
/// Prior of class at the given index.
|
||||
fn prior(&self, class_index: usize) -> T;
|
||||
fn prior(&self, class_index: usize) -> f64;
|
||||
|
||||
/// Logarithm of conditional probability of sample j given class in the specified index.
|
||||
fn log_likelihood(&self, class_index: usize, j: &M::RowVector) -> T;
|
||||
#[allow(clippy::borrowed_box)]
|
||||
fn log_likelihood<'a>(&'a self, class_index: usize, j: &'a Box<dyn ArrayView1<X> + 'a>) -> f64;
|
||||
|
||||
/// Possible classes of the distribution.
|
||||
fn classes(&self) -> &Vec<T>;
|
||||
fn classes(&self) -> &Vec<Y>;
|
||||
}
|
||||
|
||||
/// Base struct for the Naive Bayes classifier.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) struct BaseNaiveBayes<T: RealNumber, M: Matrix<T>, D: NBDistribution<T, M>> {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub(crate) struct BaseNaiveBayes<
|
||||
TX: Number,
|
||||
TY: Number,
|
||||
X: Array2<TX>,
|
||||
Y: Array1<TY>,
|
||||
D: NBDistribution<TX, TY>,
|
||||
> {
|
||||
distribution: D,
|
||||
_phantom_t: PhantomData<T>,
|
||||
_phantom_m: PhantomData<M>,
|
||||
_phantom_tx: PhantomData<TX>,
|
||||
_phantom_ty: PhantomData<TY>,
|
||||
_phantom_x: PhantomData<X>,
|
||||
_phantom_y: PhantomData<Y>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>, D: NBDistribution<T, M>> BaseNaiveBayes<T, M, D> {
|
||||
impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: NBDistribution<TX, TY>>
|
||||
BaseNaiveBayes<TX, TY, X, Y, D>
|
||||
{
|
||||
/// Fits NB classifier to a given NBdistribution.
|
||||
/// * `distribution` - NBDistribution of the training data
|
||||
pub fn fit(distribution: D) -> Result<Self, Failed> {
|
||||
Ok(Self {
|
||||
distribution,
|
||||
_phantom_t: PhantomData,
|
||||
_phantom_m: PhantomData,
|
||||
_phantom_tx: PhantomData,
|
||||
_phantom_ty: PhantomData,
|
||||
_phantom_x: PhantomData,
|
||||
_phantom_y: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Estimates the class labels for the provided data.
|
||||
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
|
||||
/// Returns a vector of size N with class estimates.
|
||||
pub fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
pub fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
let y_classes = self.distribution.classes();
|
||||
let (rows, _) = x.shape();
|
||||
let predictions = (0..rows)
|
||||
@@ -98,8 +110,8 @@ impl<T: RealNumber, M: Matrix<T>, D: NBDistribution<T, M>> BaseNaiveBayes<T, M,
|
||||
.unwrap();
|
||||
*prediction
|
||||
})
|
||||
.collect::<Vec<T>>();
|
||||
let y_hat = M::RowVector::from_array(&predictions);
|
||||
.collect::<Vec<TY>>();
|
||||
let y_hat = Y::from_vec_slice(&predictions);
|
||||
Ok(y_hat)
|
||||
}
|
||||
}
|
||||
|
||||
+165
-155
@@ -7,7 +7,7 @@
|
||||
//! Example:
|
||||
//!
|
||||
//! ```
|
||||
//! use smartcore::linalg::naive::dense_matrix::*;
|
||||
//! use smartcore::linalg::basic::matrix::DenseMatrix;
|
||||
//! use smartcore::naive_bayes::multinomial::MultinomialNB;
|
||||
//!
|
||||
//! // Training data points are:
|
||||
@@ -15,69 +15,70 @@
|
||||
//! // Chinese Chinese Shanghai (class: China)
|
||||
//! // Chinese Macao (class: China)
|
||||
//! // Tokyo Japan Chinese (class: Japan)
|
||||
//! let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
//! &[1., 2., 0., 0., 0., 0.],
|
||||
//! &[0., 2., 0., 0., 1., 0.],
|
||||
//! &[0., 1., 0., 1., 0., 0.],
|
||||
//! &[0., 1., 1., 0., 0., 1.],
|
||||
//! let x = DenseMatrix::<u32>::from_2d_array(&[
|
||||
//! &[1, 2, 0, 0, 0, 0],
|
||||
//! &[0, 2, 0, 0, 1, 0],
|
||||
//! &[0, 1, 0, 1, 0, 0],
|
||||
//! &[0, 1, 1, 0, 0, 1],
|
||||
//! ]);
|
||||
//! let y = vec![0., 0., 0., 1.];
|
||||
//! let y: Vec<u32> = vec![0, 0, 0, 1];
|
||||
//! let nb = MultinomialNB::fit(&x, &y, Default::default()).unwrap();
|
||||
//!
|
||||
//! // Testing data point is:
|
||||
//! // Chinese Chinese Chinese Tokyo Japan
|
||||
//! let x_test = DenseMatrix::<f64>::from_2d_array(&[&[0., 3., 1., 0., 0., 1.]]);
|
||||
//! let x_test = DenseMatrix::from_2d_array(&[&[0, 3, 1, 0, 0, 1]]);
|
||||
//! let y_hat = nb.predict(&x_test).unwrap();
|
||||
//! ```
|
||||
//!
|
||||
//! ## References:
|
||||
//!
|
||||
//! * ["Introduction to Information Retrieval", Manning C. D., Raghavan P., Schutze H., 2009, Chapter 13 ](https://nlp.stanford.edu/IR-book/information-retrieval-book.html)
|
||||
use num_traits::Unsigned;
|
||||
|
||||
use crate::api::{Predictor, SupervisedEstimator};
|
||||
use crate::error::Failed;
|
||||
use crate::linalg::row_iter;
|
||||
use crate::linalg::BaseVector;
|
||||
use crate::linalg::Matrix;
|
||||
use crate::math::num::RealNumber;
|
||||
use crate::math::vector::RealNumberVector;
|
||||
use crate::linalg::basic::arrays::{Array1, Array2, ArrayView1};
|
||||
use crate::naive_bayes::{BaseNaiveBayes, NBDistribution};
|
||||
use crate::numbers::basenum::Number;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Naive Bayes classifier for Multinomial features
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct MultinomialNBDistribution<T: RealNumber> {
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
struct MultinomialNBDistribution<T: Number> {
|
||||
/// class labels known to the classifier
|
||||
class_labels: Vec<T>,
|
||||
/// number of training samples observed in each class
|
||||
class_count: Vec<usize>,
|
||||
/// probability of each class
|
||||
class_priors: Vec<T>,
|
||||
class_priors: Vec<f64>,
|
||||
/// Empirical log probability of features given a class
|
||||
feature_log_prob: Vec<Vec<T>>,
|
||||
feature_log_prob: Vec<Vec<f64>>,
|
||||
/// Number of samples encountered for each (class, feature)
|
||||
feature_count: Vec<Vec<usize>>,
|
||||
/// Number of features of each sample
|
||||
n_features: usize,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for MultinomialNBDistribution<T> {
|
||||
fn prior(&self, class_index: usize) -> T {
|
||||
impl<X: Number + Unsigned, Y: Number + Ord + Unsigned> NBDistribution<X, Y>
|
||||
for MultinomialNBDistribution<Y>
|
||||
{
|
||||
fn prior(&self, class_index: usize) -> f64 {
|
||||
self.class_priors[class_index]
|
||||
}
|
||||
|
||||
fn log_likelihood(&self, class_index: usize, j: &M::RowVector) -> T {
|
||||
let mut likelihood = T::zero();
|
||||
for feature in 0..j.len() {
|
||||
let value = j.get(feature);
|
||||
fn log_likelihood<'a>(&self, class_index: usize, j: &'a Box<dyn ArrayView1<X> + 'a>) -> f64 {
|
||||
let mut likelihood = 0f64;
|
||||
for feature in 0..j.shape() {
|
||||
let value = X::to_f64(j.get(feature)).unwrap();
|
||||
likelihood += value * self.feature_log_prob[class_index][feature];
|
||||
}
|
||||
likelihood
|
||||
}
|
||||
|
||||
fn classes(&self) -> &Vec<T> {
|
||||
fn classes(&self) -> &Vec<Y> {
|
||||
&self.class_labels
|
||||
}
|
||||
}
|
||||
@@ -85,33 +86,33 @@ impl<T: RealNumber, M: Matrix<T>> NBDistribution<T, M> for MultinomialNBDistribu
|
||||
/// `MultinomialNB` parameters. Use `Default::default()` for default values.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MultinomialNBParameters<T: RealNumber> {
|
||||
pub struct MultinomialNBParameters {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub alpha: T,
|
||||
pub alpha: f64,
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub priors: Option<Vec<T>>,
|
||||
pub priors: Option<Vec<f64>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> MultinomialNBParameters<T> {
|
||||
impl MultinomialNBParameters {
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub fn with_alpha(mut self, alpha: T) -> Self {
|
||||
pub fn with_alpha(mut self, alpha: f64) -> Self {
|
||||
self.alpha = alpha;
|
||||
self
|
||||
}
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub fn with_priors(mut self, priors: Vec<T>) -> Self {
|
||||
pub fn with_priors(mut self, priors: Vec<f64>) -> Self {
|
||||
self.priors = Some(priors);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for MultinomialNBParameters<T> {
|
||||
impl Default for MultinomialNBParameters {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
alpha: T::one(),
|
||||
priors: None,
|
||||
alpha: 1f64,
|
||||
priors: Option::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,25 +120,25 @@ impl<T: RealNumber> Default for MultinomialNBParameters<T> {
|
||||
/// MultinomialNB grid search parameters
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MultinomialNBSearchParameters<T: RealNumber> {
|
||||
pub struct MultinomialNBSearchParameters {
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).
|
||||
pub alpha: Vec<T>,
|
||||
pub alpha: Vec<f64>,
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
/// Prior probabilities of the classes. If specified the priors are not adjusted according to the data
|
||||
pub priors: Vec<Option<Vec<T>>>,
|
||||
pub priors: Vec<Option<Vec<f64>>>,
|
||||
}
|
||||
|
||||
/// MultinomialNB grid search iterator
|
||||
pub struct MultinomialNBSearchParametersIterator<T: RealNumber> {
|
||||
multinomial_nb_search_parameters: MultinomialNBSearchParameters<T>,
|
||||
pub struct MultinomialNBSearchParametersIterator {
|
||||
multinomial_nb_search_parameters: MultinomialNBSearchParameters,
|
||||
current_alpha: usize,
|
||||
current_priors: usize,
|
||||
}
|
||||
|
||||
impl<T: RealNumber> IntoIterator for MultinomialNBSearchParameters<T> {
|
||||
type Item = MultinomialNBParameters<T>;
|
||||
type IntoIter = MultinomialNBSearchParametersIterator<T>;
|
||||
impl IntoIterator for MultinomialNBSearchParameters {
|
||||
type Item = MultinomialNBParameters;
|
||||
type IntoIter = MultinomialNBSearchParametersIterator;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
MultinomialNBSearchParametersIterator {
|
||||
@@ -148,8 +149,8 @@ impl<T: RealNumber> IntoIterator for MultinomialNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Iterator for MultinomialNBSearchParametersIterator<T> {
|
||||
type Item = MultinomialNBParameters<T>;
|
||||
impl Iterator for MultinomialNBSearchParametersIterator {
|
||||
type Item = MultinomialNBParameters;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current_alpha == self.multinomial_nb_search_parameters.alpha.len()
|
||||
@@ -177,7 +178,7 @@ impl<T: RealNumber> Iterator for MultinomialNBSearchParametersIterator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> Default for MultinomialNBSearchParameters<T> {
|
||||
impl Default for MultinomialNBSearchParameters {
|
||||
fn default() -> Self {
|
||||
let default_params = MultinomialNBParameters::default();
|
||||
|
||||
@@ -188,21 +189,21 @@ impl<T: RealNumber> Default for MultinomialNBSearchParameters<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber> MultinomialNBDistribution<T> {
|
||||
impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
|
||||
/// Fits the distribution to a NxM matrix where N is number of samples and M is number of features.
|
||||
/// * `x` - training data.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `priors` - Optional vector with prior probabilities of the classes. If not defined,
|
||||
/// priors are adjusted according to the data.
|
||||
/// * `alpha` - Additive (Laplace/Lidstone) smoothing parameter.
|
||||
pub fn fit<M: Matrix<T>>(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
alpha: T,
|
||||
priors: Option<Vec<T>>,
|
||||
pub fn fit<TX: Number + Unsigned, X: Array2<TX>, Y: Array1<TY>>(
|
||||
x: &X,
|
||||
y: &Y,
|
||||
alpha: f64,
|
||||
priors: Option<Vec<f64>>,
|
||||
) -> Result<Self, Failed> {
|
||||
let (n_samples, n_features) = x.shape();
|
||||
let y_samples = y.len();
|
||||
let y_samples = y.shape();
|
||||
if y_samples != n_samples {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
||||
@@ -216,16 +217,14 @@ impl<T: RealNumber> MultinomialNBDistribution<T> {
|
||||
n_samples
|
||||
)));
|
||||
}
|
||||
if alpha < T::zero() {
|
||||
if alpha < 0f64 {
|
||||
return Err(Failed::fit(&format!(
|
||||
"Alpha should be greater than 0; |alpha|=[{}]",
|
||||
alpha
|
||||
)));
|
||||
}
|
||||
|
||||
let y = y.to_vec();
|
||||
|
||||
let (class_labels, indices) = <Vec<T> as RealNumberVector<T>>::unique_with_indices(&y);
|
||||
let (class_labels, indices) = y.unique_with_indices();
|
||||
let mut class_count = vec![0_usize; class_labels.len()];
|
||||
|
||||
for class_index in indices.iter() {
|
||||
@@ -242,14 +241,14 @@ impl<T: RealNumber> MultinomialNBDistribution<T> {
|
||||
} else {
|
||||
class_count
|
||||
.iter()
|
||||
.map(|&c| T::from(c).unwrap() / T::from(n_samples).unwrap())
|
||||
.map(|&c| c as f64 / n_samples as f64)
|
||||
.collect()
|
||||
};
|
||||
|
||||
let mut feature_in_class_counter = vec![vec![0_usize; n_features]; class_labels.len()];
|
||||
|
||||
for (row, class_index) in row_iter(x).zip(indices) {
|
||||
for (idx, row_i) in row.iter().enumerate().take(n_features) {
|
||||
for (row, class_index) in x.row_iter().zip(indices) {
|
||||
for (idx, row_i) in row.iterator(0).enumerate().take(n_features) {
|
||||
feature_in_class_counter[class_index][idx] +=
|
||||
row_i.to_usize().ok_or_else(|| {
|
||||
Failed::fit(&format!(
|
||||
@@ -267,9 +266,7 @@ impl<T: RealNumber> MultinomialNBDistribution<T> {
|
||||
feature_count
|
||||
.iter()
|
||||
.map(|&count| {
|
||||
((T::from(count).unwrap() + alpha)
|
||||
/ (T::from(n_c).unwrap() + alpha * T::from(n_features).unwrap()))
|
||||
.ln()
|
||||
((count as f64 + alpha) / (n_c as f64 + alpha * n_features as f64)).ln()
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
@@ -289,87 +286,94 @@ impl<T: RealNumber> MultinomialNBDistribution<T> {
|
||||
/// MultinomialNB implements the naive Bayes algorithm for multinomially distributed data.
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct MultinomialNB<T: RealNumber, M: Matrix<T>> {
|
||||
inner: BaseNaiveBayes<T, M, MultinomialNBDistribution<T>>,
|
||||
pub struct MultinomialNB<
|
||||
TX: Number + Unsigned,
|
||||
TY: Number + Ord + Unsigned,
|
||||
X: Array2<TX>,
|
||||
Y: Array1<TY>,
|
||||
> {
|
||||
inner: Option<BaseNaiveBayes<TX, TY, X, Y, MultinomialNBDistribution<TY>>>,
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> SupervisedEstimator<M, M::RowVector, MultinomialNBParameters<T>>
|
||||
for MultinomialNB<T, M>
|
||||
impl<TX: Number + Unsigned, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
SupervisedEstimator<X, Y, MultinomialNBParameters> for MultinomialNB<TX, TY, X, Y>
|
||||
{
|
||||
fn fit(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
parameters: MultinomialNBParameters<T>,
|
||||
) -> Result<Self, Failed> {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
inner: Option::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn fit(x: &X, y: &Y, parameters: MultinomialNBParameters) -> Result<Self, Failed> {
|
||||
MultinomialNB::fit(x, y, parameters)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> Predictor<M, M::RowVector> for MultinomialNB<T, M> {
|
||||
fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
impl<TX: Number + Unsigned, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
Predictor<X, Y> for MultinomialNB<TX, TY, X, Y>
|
||||
{
|
||||
fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.predict(x)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: RealNumber, M: Matrix<T>> MultinomialNB<T, M> {
|
||||
impl<TX: Number + Unsigned, TY: Number + Ord + Unsigned, X: Array2<TX>, Y: Array1<TY>>
|
||||
MultinomialNB<TX, TY, X, Y>
|
||||
{
|
||||
/// Fits MultinomialNB with given data
|
||||
/// * `x` - training data of size NxM where N is the number of samples and M is the number of
|
||||
/// features.
|
||||
/// * `y` - vector with target values (classes) of length N.
|
||||
/// * `parameters` - additional parameters like class priors, alpha for smoothing and
|
||||
/// binarizing threshold.
|
||||
pub fn fit(
|
||||
x: &M,
|
||||
y: &M::RowVector,
|
||||
parameters: MultinomialNBParameters<T>,
|
||||
) -> Result<Self, Failed> {
|
||||
pub fn fit(x: &X, y: &Y, parameters: MultinomialNBParameters) -> Result<Self, Failed> {
|
||||
let distribution =
|
||||
MultinomialNBDistribution::fit(x, y, parameters.alpha, parameters.priors)?;
|
||||
let inner = BaseNaiveBayes::fit(distribution)?;
|
||||
Ok(Self { inner })
|
||||
Ok(Self { inner: Some(inner) })
|
||||
}
|
||||
|
||||
/// Estimates the class labels for the provided data.
|
||||
/// * `x` - data of shape NxM where N is number of data points to estimate and M is number of features.
|
||||
/// Returns a vector of size N with class estimates.
|
||||
pub fn predict(&self, x: &M) -> Result<M::RowVector, Failed> {
|
||||
self.inner.predict(x)
|
||||
pub fn predict(&self, x: &X) -> Result<Y, Failed> {
|
||||
self.inner.as_ref().unwrap().predict(x)
|
||||
}
|
||||
|
||||
/// Class labels known to the classifier.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn classes(&self) -> &Vec<T> {
|
||||
&self.inner.distribution.class_labels
|
||||
pub fn classes(&self) -> &Vec<TY> {
|
||||
&self.inner.as_ref().unwrap().distribution.class_labels
|
||||
}
|
||||
|
||||
/// Number of training samples observed in each class.
|
||||
/// Returns a vector of size n_classes.
|
||||
pub fn class_count(&self) -> &Vec<usize> {
|
||||
&self.inner.distribution.class_count
|
||||
&self.inner.as_ref().unwrap().distribution.class_count
|
||||
}
|
||||
|
||||
/// Empirical log probability of features given a class, P(x_i|y).
|
||||
/// Returns a 2d vector of shape (n_classes, n_features)
|
||||
pub fn feature_log_prob(&self) -> &Vec<Vec<T>> {
|
||||
&self.inner.distribution.feature_log_prob
|
||||
pub fn feature_log_prob(&self) -> &Vec<Vec<f64>> {
|
||||
&self.inner.as_ref().unwrap().distribution.feature_log_prob
|
||||
}
|
||||
|
||||
/// Number of features of each sample
|
||||
pub fn n_features(&self) -> usize {
|
||||
self.inner.distribution.n_features
|
||||
self.inner.as_ref().unwrap().distribution.n_features
|
||||
}
|
||||
|
||||
/// Number of samples encountered for each (class, feature)
|
||||
/// Returns a 2d vector of shape (n_classes, n_features)
|
||||
pub fn feature_count(&self) -> &Vec<Vec<usize>> {
|
||||
&self.inner.distribution.feature_count
|
||||
&self.inner.as_ref().unwrap().distribution.feature_count
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::linalg::naive::dense_matrix::DenseMatrix;
|
||||
use crate::linalg::basic::matrix::DenseMatrix;
|
||||
|
||||
#[test]
|
||||
fn search_parameters() {
|
||||
@@ -398,19 +402,21 @@ mod tests {
|
||||
// Chinese Chinese Shanghai (class: China)
|
||||
// Chinese Macao (class: China)
|
||||
// Tokyo Japan Chinese (class: Japan)
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[1., 2., 0., 0., 0., 0.],
|
||||
&[0., 2., 0., 0., 1., 0.],
|
||||
&[0., 1., 0., 1., 0., 0.],
|
||||
&[0., 1., 1., 0., 0., 1.],
|
||||
let x = DenseMatrix::from_2d_array(&[
|
||||
&[1, 2, 0, 0, 0, 0],
|
||||
&[0, 2, 0, 0, 1, 0],
|
||||
&[0, 1, 0, 1, 0, 0],
|
||||
&[0, 1, 1, 0, 0, 1],
|
||||
]);
|
||||
let y = vec![0., 0., 0., 1.];
|
||||
let y: Vec<u32> = vec![0, 0, 0, 1];
|
||||
let mnb = MultinomialNB::fit(&x, &y, Default::default()).unwrap();
|
||||
|
||||
assert_eq!(mnb.classes(), &[0., 1.]);
|
||||
assert_eq!(mnb.classes(), &[0, 1]);
|
||||
assert_eq!(mnb.class_count(), &[3, 1]);
|
||||
|
||||
assert_eq!(mnb.inner.distribution.class_priors, &[0.75, 0.25]);
|
||||
let distribution = mnb.inner.clone().unwrap().distribution;
|
||||
|
||||
assert_eq!(&distribution.class_priors, &[0.75, 0.25]);
|
||||
assert_eq!(
|
||||
mnb.feature_log_prob(),
|
||||
&[
|
||||
@@ -435,33 +441,33 @@ mod tests {
|
||||
|
||||
// Testing data point is:
|
||||
// Chinese Chinese Chinese Tokyo Japan
|
||||
let x_test = DenseMatrix::<f64>::from_2d_array(&[&[0., 3., 1., 0., 0., 1.]]);
|
||||
let x_test = DenseMatrix::<u32>::from_2d_array(&[&[0, 3, 1, 0, 0, 1]]);
|
||||
let y_hat = mnb.predict(&x_test).unwrap();
|
||||
|
||||
assert_eq!(y_hat, &[0.]);
|
||||
assert_eq!(y_hat, &[0]);
|
||||
}
|
||||
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
fn multinomial_nb_scikit_parity() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[2., 4., 0., 0., 2., 1., 2., 4., 2., 0.],
|
||||
&[3., 4., 0., 2., 1., 0., 1., 4., 0., 3.],
|
||||
&[1., 4., 2., 4., 1., 0., 1., 2., 3., 2.],
|
||||
&[0., 3., 3., 4., 1., 0., 3., 1., 1., 1.],
|
||||
&[0., 2., 1., 4., 3., 4., 1., 2., 3., 1.],
|
||||
&[3., 2., 4., 1., 3., 0., 2., 4., 0., 2.],
|
||||
&[3., 1., 3., 0., 2., 0., 4., 4., 3., 4.],
|
||||
&[2., 2., 2., 0., 1., 1., 2., 1., 0., 1.],
|
||||
&[3., 3., 2., 2., 0., 2., 3., 2., 2., 3.],
|
||||
&[4., 3., 4., 4., 4., 2., 2., 0., 1., 4.],
|
||||
&[3., 4., 2., 2., 1., 4., 4., 4., 1., 3.],
|
||||
&[3., 0., 1., 4., 4., 0., 0., 3., 2., 4.],
|
||||
&[2., 0., 3., 3., 1., 2., 0., 2., 4., 1.],
|
||||
&[2., 4., 0., 4., 2., 4., 1., 3., 1., 4.],
|
||||
&[0., 2., 2., 3., 4., 0., 4., 4., 4., 4.],
|
||||
let x = DenseMatrix::<u32>::from_2d_array(&[
|
||||
&[2, 4, 0, 0, 2, 1, 2, 4, 2, 0],
|
||||
&[3, 4, 0, 2, 1, 0, 1, 4, 0, 3],
|
||||
&[1, 4, 2, 4, 1, 0, 1, 2, 3, 2],
|
||||
&[0, 3, 3, 4, 1, 0, 3, 1, 1, 1],
|
||||
&[0, 2, 1, 4, 3, 4, 1, 2, 3, 1],
|
||||
&[3, 2, 4, 1, 3, 0, 2, 4, 0, 2],
|
||||
&[3, 1, 3, 0, 2, 0, 4, 4, 3, 4],
|
||||
&[2, 2, 2, 0, 1, 1, 2, 1, 0, 1],
|
||||
&[3, 3, 2, 2, 0, 2, 3, 2, 2, 3],
|
||||
&[4, 3, 4, 4, 4, 2, 2, 0, 1, 4],
|
||||
&[3, 4, 2, 2, 1, 4, 4, 4, 1, 3],
|
||||
&[3, 0, 1, 4, 4, 0, 0, 3, 2, 4],
|
||||
&[2, 0, 3, 3, 1, 2, 0, 2, 4, 1],
|
||||
&[2, 4, 0, 4, 2, 4, 1, 3, 1, 4],
|
||||
&[0, 2, 2, 3, 4, 0, 4, 4, 4, 4],
|
||||
]);
|
||||
let y = vec![2., 2., 0., 0., 0., 2., 1., 1., 0., 1., 0., 0., 2., 0., 2.];
|
||||
let y: Vec<u32> = vec![2, 2, 0, 0, 0, 2, 1, 1, 0, 1, 0, 0, 2, 0, 2];
|
||||
let nb = MultinomialNB::fit(&x, &y, Default::default()).unwrap();
|
||||
|
||||
assert_eq!(nb.n_features(), 10);
|
||||
@@ -476,47 +482,51 @@ mod tests {
|
||||
|
||||
let y_hat = nb.predict(&x).unwrap();
|
||||
|
||||
assert!(nb
|
||||
.inner
|
||||
.distribution
|
||||
.class_priors
|
||||
.approximate_eq(&vec!(0.46, 0.2, 0.33), 1e-2));
|
||||
assert!(nb.feature_log_prob()[1].approximate_eq(
|
||||
let distribution = nb.inner.clone().unwrap().distribution;
|
||||
|
||||
assert_eq!(
|
||||
&distribution.class_priors,
|
||||
&vec!(0.4666666666666667, 0.2, 0.3333333333333333)
|
||||
);
|
||||
|
||||
// Due to float differences in WASM32,
|
||||
// we disable this test for that arch
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
assert_eq!(
|
||||
&nb.feature_log_prob()[1],
|
||||
&vec![
|
||||
-2.00148,
|
||||
-2.35815494,
|
||||
-2.00148,
|
||||
-2.69462718,
|
||||
-2.22462355,
|
||||
-2.91777073,
|
||||
-2.10684052,
|
||||
-2.51230562,
|
||||
-2.69462718,
|
||||
-2.00148
|
||||
],
|
||||
1e-5
|
||||
));
|
||||
assert!(y_hat.approximate_eq(
|
||||
&vec!(2.0, 2.0, 0.0, 0.0, 0.0, 2.0, 2.0, 1.0, 0.0, 1.0, 0.0, 2.0, 0.0, 0.0, 2.0),
|
||||
1e-5
|
||||
));
|
||||
-2.001480000210124,
|
||||
-2.3581549441488563,
|
||||
-2.001480000210124,
|
||||
-2.6946271807700692,
|
||||
-2.2246235515243336,
|
||||
-2.917770732084279,
|
||||
-2.10684051586795,
|
||||
-2.512305623976115,
|
||||
-2.6946271807700692,
|
||||
-2.001480000210124
|
||||
]
|
||||
);
|
||||
assert_eq!(y_hat, vec!(2, 2, 0, 0, 0, 2, 2, 1, 0, 1, 0, 2, 0, 0, 2));
|
||||
}
|
||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
#[test]
|
||||
#[cfg(feature = "serde")]
|
||||
fn serde() {
|
||||
let x = DenseMatrix::<f64>::from_2d_array(&[
|
||||
&[1., 1., 0., 0., 0., 0.],
|
||||
&[0., 1., 0., 0., 1., 0.],
|
||||
&[0., 1., 0., 1., 0., 0.],
|
||||
&[0., 1., 1., 0., 0., 1.],
|
||||
]);
|
||||
let y = vec![0., 0., 0., 1.];
|
||||
|
||||
let mnb = MultinomialNB::fit(&x, &y, Default::default()).unwrap();
|
||||
let deserialized_mnb: MultinomialNB<f64, DenseMatrix<f64>> =
|
||||
serde_json::from_str(&serde_json::to_string(&mnb).unwrap()).unwrap();
|
||||
// TODO: implement serialization
|
||||
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
|
||||
// #[test]
|
||||
// #[cfg(feature = "serde")]
|
||||
// fn serde() {
|
||||
// let x = DenseMatrix::from_2d_array(&[
|
||||
// &[1, 1, 0, 0, 0, 0],
|
||||
// &[0, 1, 0, 0, 1, 0],
|
||||
// &[0, 1, 0, 1, 0, 0],
|
||||
// &[0, 1, 1, 0, 0, 1],
|
||||
// ]);
|
||||
// let y = vec![0, 0, 0, 1];
|
||||
|
||||
assert_eq!(mnb, deserialized_mnb);
|
||||
}
|
||||
// let mnb = MultinomialNB::fit(&x, &y, Default::default()).unwrap();
|
||||
// let deserialized_mnb: MultinomialNB<u32, u32, DenseMatrix<u32>, Vec<u32>> =
|
||||
// serde_json::from_str(&serde_json::to_string(&mnb).unwrap()).unwrap();
|
||||
|
||||
// assert_eq!(mnb, deserialized_mnb);
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user