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
//! Common rustlearn traits.

use std::cmp::Ordering;

use array::dense::*;


/// Trait describing supervised models.
pub trait SupervisedModel<T> {
    fn fit(&mut self, X: T, y: &Array) -> Result<(), &'static str>;
    fn decision_function(&self, X: T) -> Result<Array, &'static str>;
    fn predict(&self, x: T) -> Result<Array, &'static str> {

        let decision_func = try!(self.decision_function(x));

        Ok(Array::from(decision_func.data()
            .iter()
            .map(|v| {
                match v.partial_cmp(&0.5) {
                    Some(Ordering::Greater) => 1.0,
                    _ => 0.0,
                }
            })
            .collect::<Vec<f32>>()))
    }
}


/// Applies to models capable of making predictions in a parallel fashion.
pub trait ParallelPredict<T> {
    fn decision_function_parallel(&self, X: T, num_threads: usize) -> Result<Array, &'static str>;
    fn predict_parallel(&self, X: T, num_threads: usize) -> Result<Array, &'static str> {

        let decision_func = try!(self.decision_function_parallel(X, num_threads));

        Ok(Array::from(decision_func.data()
            .iter()
            .map(|v| {
                match v.partial_cmp(&0.5) {
                    Some(Ordering::Greater) => 1.0,
                    _ => 0.0,
                }
            })
            .collect::<Vec<f32>>()))
    }
}


/// Applies to models capable of being trained in a parallel fashion.
pub trait ParallelSupervisedModel<T> {
    fn fit_parallel(&mut self, X: T, y: &Array, num_threads: usize) -> Result<(), &'static str>;
}