1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use super::Classifier;
use crate::prelude::Predictable;
use crate::prelude::Trainable;
use linfa::prelude::{Fit, Predict};
use linfa::Dataset;
use linfa::Float;
use linfa::Label;
use linfa_bayes::GaussianNb;
use ndarray::Array1;
use ndarray::ArrayView1;
use ndarray::ArrayView2;

/// The `BayesClassifier` struct provides a Gaussian Naive Bayes classifier.
///
/// This classifier is implemented using the `linfa_bayes` crate
/// and can be trained on a given dataset to classify future data points.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct BayesClassifier<X, Y>
where
    X: Float,
    Y: Label,
{
    model: Option<GaussianNb<X, Y>>,
}

impl<X, Y> BayesClassifier<X, Y>
where
    X: Float,
    Y: Label,
{
    /// Creates a new `BayesClassifier` instance with default parameters.
    ///
    /// # Example
    ///
    /// ```
    /// use object_detector_rust::classifier::BayesClassifier;
    ///
    /// let bayes_classifier = BayesClassifier::<f32, bool>::new();
    /// ```
    pub fn new() -> Self {
        BayesClassifier { model: None }
    }
}

impl<X, Y> Trainable<X, Y> for BayesClassifier<X, Y>
where
    X: Float,
    Y: Label,
{
    fn fit(&mut self, x: &ArrayView2<X>, y: &ArrayView1<Y>) -> Result<(), String> {
        let dataset = Dataset::new(x.to_owned(), y.to_owned());
        let model = GaussianNb::<X, Y>::params().fit(&dataset).unwrap();
        self.model = Some(model);
        Ok(())
    }
}

impl<X, Y> Predictable<X, Y> for BayesClassifier<X, Y>
where
    X: Float,
    Y: Label,
{
    fn predict(&self, x: &ArrayView2<X>) -> Result<Array1<Y>, String> {
        Ok(self.model.as_ref().unwrap().predict(x))
    }
}

impl<X, Y> Classifier<X, Y> for BayesClassifier<X, Y>
where
    X: Float,
    Y: Label,
{
}

#[cfg(test)]
mod tests {
    use super::*;
    use linfa::prelude::ToConfusionMatrix;
    use ndarray::{arr1, arr2};

    #[test]
    fn test_bayes_classifier() {
        let mut classifier = BayesClassifier::new();

        let x = arr2(&[[1., 1.], [1., 2.], [2., 1.], [2., 2.]]);
        let y = arr1(&[1, 1, 0, 0]);
        assert!(classifier.fit(&x.view(), &y.view()).is_ok());

        let x_pred = arr2(&[[1., 1.], [1., 2.], [2., 1.], [2., 2.]]);
        let y_pred = arr1(&[1, 1, 0, 0]);
        let y_pred_res = classifier.predict(&x_pred.view()).unwrap();
        assert_eq!(y_pred, y_pred_res);
        let predicted_y = classifier.predict(&x.view()).unwrap();
        let cm = predicted_y.confusion_matrix(&y).unwrap();
        println!("{:?}", cm);
        assert!(cm.precision() > 0.8);
        assert!(cm.accuracy() > 0.5);
    }

    #[test]
    fn test_partial_eq() {
        let classifier = BayesClassifier::<f32, usize>::new();
        let classifier2 = classifier.clone();
        assert!(classifier.eq(&classifier2));
    }
}