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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use super::core::{RandomForest, RandomForestOptions};
use crate::criterion::ClassificationCriterion;
use crate::functions;
use crate::table::Table;
use std::io::{Read, Write};
use std::num::NonZeroUsize;
#[derive(Debug, Clone, Default)]
pub struct RandomForestClassifierOptions {
inner: RandomForestOptions,
}
impl RandomForestClassifierOptions {
pub fn new() -> Self {
Self::default()
}
pub fn seed(&mut self, seed: u64) -> &mut Self {
self.inner.seed(seed);
self
}
pub fn trees(&mut self, trees: NonZeroUsize) -> &mut Self {
self.inner.trees(trees);
self
}
pub fn max_features(&mut self, max: NonZeroUsize) -> &mut Self {
self.inner.max_features(max);
self
}
pub fn max_samples(&mut self, max: NonZeroUsize) -> &mut Self {
self.inner.max_samples(max);
self
}
pub fn parallel(&mut self) -> &mut Self {
self.inner.parallel();
self
}
pub fn fit<T: ClassificationCriterion>(
&self,
criterion: T,
table: Table,
) -> RandomForestClassifier {
RandomForestClassifier {
inner: self.inner.fit(criterion, false, table),
}
}
}
#[derive(Debug)]
pub struct RandomForestClassifier {
inner: RandomForest,
}
impl RandomForestClassifier {
pub fn fit<T: ClassificationCriterion>(criterion: T, table: Table) -> Self {
RandomForestClassifierOptions::default().fit(criterion, table)
}
pub fn predict(&self, features: &[f64]) -> f64 {
functions::most_frequent(self.inner.predict(features))
}
pub fn serialize<W: Write>(&self, writer: W) -> std::io::Result<()> {
self.inner.serialize(writer)
}
pub fn deserialize<R: Read>(reader: R) -> std::io::Result<Self> {
let inner = RandomForest::deserialize(reader)?;
Ok(Self { inner })
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::criterion::Gini;
use crate::table::{ColumnType, TableBuilder};
#[test]
fn classification_works() -> Result<(), anyhow::Error> {
let features = [
&[0.0, 1.0, 0.0][..],
&[1.0, 1.0, 1.0][..],
&[0.0, 0.0, 1.0][..],
&[1.0, 0.0, 0.0][..],
&[1.0, 0.0, 0.0][..],
&[0.0, 1.0, 0.0][..],
&[1.0, 0.0, 1.0][..],
];
let target = [1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0];
let train_len = target.len() - 1;
let mut table_builder = TableBuilder::new();
table_builder.set_feature_column_types(&[
ColumnType::Categorical,
ColumnType::Categorical,
ColumnType::Categorical,
])?;
for (xs, y) in features.iter().zip(target.iter()).take(train_len) {
table_builder.add_row(xs, *y)?;
}
let table = table_builder.build()?;
let classifier = RandomForestClassifierOptions::new()
.seed(0)
.fit(Gini, table.clone());
assert_eq!(classifier.predict(&features[train_len]), 0.0);
let classifier_parallel = RandomForestClassifierOptions::new()
.seed(0)
.parallel()
.fit(Gini, table);
assert_eq!(
classifier.predict(&features[train_len]),
classifier_parallel.predict(&features[train_len])
);
let mut bytes = Vec::new();
classifier.serialize(&mut bytes)?;
let classifier_deserialized = RandomForestClassifier::deserialize(&mut &bytes[..])?;
assert_eq!(
classifier.predict(&features[train_len]),
classifier_deserialized.predict(&features[train_len])
);
Ok(())
}
}