Port ensemble. Add Display to naive_bayes (#208)

This commit is contained in:
Lorenzo
2022-10-31 17:35:33 +00:00
committed by morenol
parent d91f4f7ce4
commit a16927aa16
10 changed files with 330 additions and 242 deletions
+32 -19
View File
@@ -22,6 +22,8 @@
//! let nb = GaussianNB::fit(&x, &y, Default::default()).unwrap();
//! let y_hat = nb.predict(&x).unwrap();
//! ```
use std::fmt;
use num_traits::Unsigned;
use crate::api::{Predictor, SupervisedEstimator};
@@ -49,6 +51,18 @@ struct GaussianNBDistribution<T: Number> {
theta: Vec<Vec<f64>>,
}
impl<T: Number + Ord + Unsigned> fmt::Display for GaussianNBDistribution<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(
f,
"GaussianNBDistribution: class_count: {:?}",
self.class_count
)?;
writeln!(f, "class_labels: {:?}", self.class_labels)?;
Ok(())
}
}
impl<X: Number + RealNumber, Y: Number + Ord + Unsigned> NBDistribution<X, Y>
for GaussianNBDistribution<Y>
{
@@ -415,25 +429,24 @@ mod tests {
assert_eq!(gnb.class_priors(), &priors);
}
// 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];
#[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, u32, DenseMatrix<f64>, Vec<u32>> =
// 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);
}
}