datarust 0.7.0

Scikit-learn-style preprocessing and classical ML in Rust
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! `DecisionTreeClassifier` and `DecisionTreeRegressor`.

use super::{Criterion, MaxFeatures, TreeBuilder, TreeOptions, TreeStructure};
use crate::error::{DatarustError, Result};
use crate::label_space::LabelSpace;
use crate::matrix::Matrix;
use crate::traits::{
    Classifier, Estimator, ParamValue, Params, PredictProba, Predictor, Regressor,
};

/// CART classification tree (Gini / entropy criteria).
///
/// # Example
///
/// ```rust
/// use datarust::tree::DecisionTreeClassifier;
/// use datarust::traits::{Classifier, Predictor};
/// use datarust::Matrix;
///
/// let x = Matrix::new(vec![
///     vec![1.0, 1.0],
///     vec![2.0, 1.0],
///     vec![5.0, 4.0],
///     vec![6.0, 5.0],
/// ])?;
/// let y = vec![0.0, 0.0, 1.0, 1.0];
///
/// let mut clf = DecisionTreeClassifier::new();
/// clf.fit(&x, &y)?;
/// assert_eq!(clf.predict(&x)?, vec![0.0, 0.0, 1.0, 1.0]);
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct DecisionTreeClassifier {
    options: TreeOptions,
    criterion: Criterion,
    n_features: usize,
    tree: Option<TreeStructure>,
}

impl Default for DecisionTreeClassifier {
    fn default() -> Self {
        Self::new()
    }
}

impl DecisionTreeClassifier {
    /// Creates a classifier with sklearn-default parameters.
    pub fn new() -> Self {
        DecisionTreeClassifier {
            options: TreeOptions::default(),
            criterion: Criterion::Gini,
            n_features: 0,
            tree: None,
        }
    }

    /// Sets the split criterion (default `Gini`). `Criterion::SquaredError`
    /// is regression-only and is rejected by `fit`.
    pub fn with_criterion(mut self, criterion: Criterion) -> Self {
        self.criterion = criterion;
        self
    }

    /// Sets the maximum tree depth (`None` = unlimited, the default).
    pub fn with_max_depth(mut self, max_depth: Option<usize>) -> Self {
        self.options.max_depth = max_depth;
        self
    }

    /// Sets the minimum samples required to split a node (default 2).
    pub fn with_min_samples_split(mut self, n: usize) -> Self {
        self.options.min_samples_split = n;
        self
    }

    /// Sets the minimum samples required in each leaf (default 1).
    pub fn with_min_samples_leaf(mut self, n: usize) -> Self {
        self.options.min_samples_leaf = n;
        self
    }

    /// Sets the minimum impurity decrease required to split (default 0).
    pub fn with_min_impurity_decrease(mut self, v: f64) -> Self {
        self.options.min_impurity_decrease = v;
        self
    }

    /// Sets the per-split feature subset strategy (default all features).
    pub fn with_max_features(mut self, max_features: MaxFeatures) -> Self {
        self.options.max_features = max_features;
        self
    }

    /// Sets the seed for deterministic per-split feature subsampling.
    pub fn with_random_state(mut self, seed: u64) -> Self {
        self.options.random_state = seed;
        self
    }

    /// The fitted tree structure, or `None` before `fit`.
    pub fn tree(&self) -> Option<&TreeStructure> {
        self.tree.as_ref()
    }

    /// Number of features seen at fit time.
    pub fn n_features_in(&self) -> usize {
        self.n_features
    }

    /// Per-feature normalized impurity reduction (sklearn's
    /// `feature_importances_`). Requires `fit`.
    pub fn feature_importances(&self) -> Result<Vec<f64>> {
        let tree = self
            .tree
            .as_ref()
            .ok_or_else(|| DatarustError::NotFitted("DecisionTreeClassifier".into()))?;
        Ok(tree.feature_importances(self.n_features))
    }

    fn fit_impl(&mut self, x: &Matrix, y: &[f64]) -> Result<()> {
        self.fit_weighted_impl(x, y, None, None)
    }

    pub(crate) fn fit_weighted_impl(
        &mut self,
        x: &Matrix,
        y: &[f64],
        sample_weights: Option<Vec<f64>>,
        known_classes: Option<LabelSpace>,
    ) -> Result<()> {
        self.options.validate()?;
        if matches!(self.criterion, Criterion::SquaredError) {
            return Err(DatarustError::InvalidInput(
                "DecisionTreeClassifier requires Criterion::Gini or Criterion::Entropy; \
                 Criterion::SquaredError is regression-only"
                    .into(),
            ));
        }
        if x.nrows() != y.len() {
            return Err(DatarustError::ShapeMismatch {
                expected: format!("{} rows in y", x.nrows()),
                actual: format!("{} rows in y", y.len()),
            });
        }
        if x.nrows() == 0 {
            return Err(DatarustError::EmptyInput("X is empty".into()));
        }
        x.validate_finite()?;
        let classes = known_classes.unwrap_or(LabelSpace::fit(y)?);
        let n_classes = classes.len();
        let codes: Vec<usize> = y
            .iter()
            .map(|&v| classes.encode(v))
            .collect::<Result<Vec<_>>>()?;
        let mut builder = TreeBuilder::new_with_weights(
            x,
            y,
            Some(&codes),
            n_classes,
            self.criterion,
            self.options.clone(),
            sample_weights,
        );
        builder.build();
        self.n_features = x.ncols();
        self.tree = Some(TreeStructure::new(builder.nodes, Some(classes)));
        Ok(())
    }
}

impl Estimator for DecisionTreeClassifier {}

impl Predictor for DecisionTreeClassifier {
    fn fit(&mut self, x: &Matrix, y: &[f64]) -> Result<()> {
        self.fit_impl(x, y)
    }

    fn predict(&self, x: &Matrix) -> Result<Vec<f64>> {
        let tree = self
            .tree
            .as_ref()
            .ok_or_else(|| DatarustError::NotFitted("DecisionTreeClassifier".into()))?;
        let classes = tree.classes.as_ref().expect("classification tree");
        if x.ncols() != self.n_features {
            return Err(DatarustError::ShapeMismatch {
                expected: format!("{} features", self.n_features),
                actual: format!("{} features", x.ncols()),
            });
        }
        x.validate_finite()?;
        let mut out = Vec::with_capacity(x.nrows());
        for i in 0..x.nrows() {
            let leaf = tree.leaf_for(x.row(i))?;
            let class_idx = tree.nodes[leaf].value.argmax_class()?;
            out.push(classes.decode(class_idx)?);
        }
        Ok(out)
    }

    fn is_fitted(&self) -> bool {
        self.tree.is_some()
    }
}

impl Classifier for DecisionTreeClassifier {}

impl PredictProba for DecisionTreeClassifier {
    fn predict_proba(&self, x: &Matrix) -> Result<Matrix> {
        let tree = self
            .tree
            .as_ref()
            .ok_or_else(|| DatarustError::NotFitted("DecisionTreeClassifier".into()))?;
        let classes = tree.classes.as_ref().expect("classification tree");
        if x.ncols() != self.n_features {
            return Err(DatarustError::ShapeMismatch {
                expected: format!("{} features", self.n_features),
                actual: format!("{} features", x.ncols()),
            });
        }
        x.validate_finite()?;
        let n_classes = classes.len();
        let mut flat: Vec<f64> = Vec::with_capacity(x.nrows() * n_classes);
        for i in 0..x.nrows() {
            let leaf = tree.leaf_for(x.row(i))?;
            flat.extend(tree.nodes[leaf].value.class_probabilities()?);
        }
        Matrix::from_flat(x.nrows(), n_classes, flat)
    }
}

impl Params for DecisionTreeClassifier {
    fn get_params(&self) -> Vec<(&'static str, ParamValue)> {
        let mut params = self.options.shared_params();
        params.push(("criterion", ParamValue::Int(self.criterion_code())));
        params.push((
            "max_depth",
            ParamValue::Int(self.options.max_depth.unwrap_or(0)),
        ));
        params.push(("max_features", ParamValue::Int(self.max_features_code())));
        params
    }

    fn set_params(&mut self, name: &str, value: ParamValue) -> Result<()> {
        if self.options.set_shared_param(name, value.clone())? {
            return Ok(());
        }
        match (name, value) {
            ("criterion", ParamValue::Int(0)) => {
                self.criterion = Criterion::Gini;
                Ok(())
            }
            ("criterion", ParamValue::Int(1)) => {
                self.criterion = Criterion::Entropy;
                Ok(())
            }
            // Storable for `get_params` round-trips; rejected by `fit`.
            ("criterion", ParamValue::Int(2)) => {
                self.criterion = Criterion::SquaredError;
                Ok(())
            }
            ("criterion", _) => Err(DatarustError::InvalidInput(
                "criterion expects Int(0)=gini, Int(1)=entropy or Int(2)=squared_error".into(),
            )),
            ("max_depth", ParamValue::Int(0)) => {
                self.options.max_depth = None;
                Ok(())
            }
            ("max_depth", ParamValue::Int(v)) => {
                self.options.max_depth = Some(v);
                Ok(())
            }
            ("max_features", ParamValue::Int(v)) => {
                self.options.max_features = match v {
                    0 => MaxFeatures::All,
                    1 => MaxFeatures::Sqrt,
                    2 => MaxFeatures::Log2,
                    k => MaxFeatures::Count(k - 2),
                };
                Ok(())
            }
            _ => Err(DatarustError::InvalidInput(format!(
                "unknown parameter '{name}' for DecisionTreeClassifier"
            ))),
        }
    }
}

impl DecisionTreeClassifier {
    /// Stable integer code for the criterion (0 gini, 1 entropy; 2 marks
    /// the regression-only squared error, which `fit` rejects).
    fn criterion_code(&self) -> usize {
        match self.criterion {
            Criterion::Gini => 0,
            Criterion::Entropy => 1,
            Criterion::SquaredError => 2,
        }
    }

    /// Stable integer code for `max_features` (0 all, 1 sqrt, 2 log2, k+2
    /// an explicit count).
    fn max_features_code(&self) -> usize {
        match self.options.max_features {
            MaxFeatures::All => 0,
            MaxFeatures::Sqrt => 1,
            MaxFeatures::Log2 => 2,
            MaxFeatures::Count(k) => k.saturating_add(2),
        }
    }
}

/// CART regression tree (squared-error criterion).
///
/// # Example
///
/// ```rust
/// use datarust::tree::DecisionTreeRegressor;
/// use datarust::traits::{Predictor, Regressor};
/// use datarust::Matrix;
///
/// let x = Matrix::new(vec![vec![1.0], vec![2.0], vec![9.0], vec![10.0]])?;
/// let y = vec![1.0, 1.5, 9.0, 9.5];
///
/// let mut reg = DecisionTreeRegressor::new();
/// reg.fit(&x, &y)?;
/// // With default settings the tree grows to pure leaves, so training
/// // predictions reproduce the targets exactly (sklearn parity).
/// let pred = reg.predict(&x)?;
/// assert_eq!(pred, vec![1.0, 1.5, 9.0, 9.5]);
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct DecisionTreeRegressor {
    options: TreeOptions,
    n_features: usize,
    tree: Option<TreeStructure>,
}

impl Default for DecisionTreeRegressor {
    fn default() -> Self {
        Self::new()
    }
}

impl DecisionTreeRegressor {
    /// Creates a regressor with sklearn-default parameters.
    pub fn new() -> Self {
        DecisionTreeRegressor {
            options: TreeOptions::default(),
            n_features: 0,
            tree: None,
        }
    }

    /// Sets the maximum tree depth (`None` = unlimited, the default).
    pub fn with_max_depth(mut self, max_depth: Option<usize>) -> Self {
        self.options.max_depth = max_depth;
        self
    }

    /// Sets the minimum samples required to split a node (default 2).
    pub fn with_min_samples_split(mut self, n: usize) -> Self {
        self.options.min_samples_split = n;
        self
    }

    /// Sets the minimum samples required in each leaf (default 1).
    pub fn with_min_samples_leaf(mut self, n: usize) -> Self {
        self.options.min_samples_leaf = n;
        self
    }

    /// Sets the minimum impurity decrease required to split (default 0).
    pub fn with_min_impurity_decrease(mut self, v: f64) -> Self {
        self.options.min_impurity_decrease = v;
        self
    }

    /// Sets the per-split feature subset strategy (default all features).
    pub fn with_max_features(mut self, max_features: MaxFeatures) -> Self {
        self.options.max_features = max_features;
        self
    }

    /// Sets the seed for deterministic per-split feature subsampling.
    pub fn with_random_state(mut self, seed: u64) -> Self {
        self.options.random_state = seed;
        self
    }

    /// The fitted tree structure, or `None` before `fit`.
    pub fn tree(&self) -> Option<&TreeStructure> {
        self.tree.as_ref()
    }

    /// Number of features seen at fit time.
    pub fn n_features_in(&self) -> usize {
        self.n_features
    }

    /// Per-feature normalized impurity reduction (sklearn's
    /// `feature_importances_`). Requires `fit`.
    pub fn feature_importances(&self) -> Result<Vec<f64>> {
        let tree = self
            .tree
            .as_ref()
            .ok_or_else(|| DatarustError::NotFitted("DecisionTreeRegressor".into()))?;
        Ok(tree.feature_importances(self.n_features))
    }

    fn fit_impl(&mut self, x: &Matrix, y: &[f64]) -> Result<()> {
        self.fit_weighted_impl(x, y, None)
    }

    pub(crate) fn fit_weighted_impl(
        &mut self,
        x: &Matrix,
        y: &[f64],
        sample_weights: Option<Vec<f64>>,
    ) -> Result<()> {
        self.options.validate()?;
        if x.nrows() != y.len() {
            return Err(DatarustError::ShapeMismatch {
                expected: format!("{} rows in y", x.nrows()),
                actual: format!("{} rows in y", y.len()),
            });
        }
        if x.nrows() == 0 {
            return Err(DatarustError::EmptyInput("X is empty".into()));
        }
        x.validate_finite()?;
        for (i, &v) in y.iter().enumerate() {
            if !v.is_finite() {
                return Err(DatarustError::InvalidInput(format!(
                    "y contains a non-finite value at index {i}"
                )));
            }
        }
        let mut builder = TreeBuilder::new_with_weights(
            x,
            y,
            None,
            0,
            Criterion::SquaredError,
            self.options.clone(),
            sample_weights,
        );
        builder.build();
        self.n_features = x.ncols();
        self.tree = Some(TreeStructure::new(builder.nodes, None));
        Ok(())
    }
}

impl Estimator for DecisionTreeRegressor {}

impl Predictor for DecisionTreeRegressor {
    fn fit(&mut self, x: &Matrix, y: &[f64]) -> Result<()> {
        self.fit_impl(x, y)
    }

    fn predict(&self, x: &Matrix) -> Result<Vec<f64>> {
        let tree = self
            .tree
            .as_ref()
            .ok_or_else(|| DatarustError::NotFitted("DecisionTreeRegressor".into()))?;
        if x.ncols() != self.n_features {
            return Err(DatarustError::ShapeMismatch {
                expected: format!("{} features", self.n_features),
                actual: format!("{} features", x.ncols()),
            });
        }
        x.validate_finite()?;
        let mut out = Vec::with_capacity(x.nrows());
        for i in 0..x.nrows() {
            let leaf = tree.leaf_for(x.row(i))?;
            out.push(tree.nodes[leaf].value.regression_mean()?);
        }
        Ok(out)
    }

    fn is_fitted(&self) -> bool {
        self.tree.is_some()
    }
}

impl Regressor for DecisionTreeRegressor {
    fn name(&self) -> &'static str {
        "DecisionTreeRegressor"
    }
}

impl Params for DecisionTreeRegressor {
    fn get_params(&self) -> Vec<(&'static str, ParamValue)> {
        let mut params = self.options.shared_params();
        params.push((
            "max_depth",
            ParamValue::Int(self.options.max_depth.unwrap_or(0)),
        ));
        params.push(("max_features", ParamValue::Int(self.max_features_code())));
        params
    }

    fn set_params(&mut self, name: &str, value: ParamValue) -> Result<()> {
        if self.options.set_shared_param(name, value.clone())? {
            return Ok(());
        }
        match (name, value) {
            ("max_depth", ParamValue::Int(0)) => {
                self.options.max_depth = None;
                Ok(())
            }
            ("max_depth", ParamValue::Int(v)) => {
                self.options.max_depth = Some(v);
                Ok(())
            }
            ("max_features", ParamValue::Int(v)) => {
                self.options.max_features = match v {
                    0 => MaxFeatures::All,
                    1 => MaxFeatures::Sqrt,
                    2 => MaxFeatures::Log2,
                    k => MaxFeatures::Count(k - 2),
                };
                Ok(())
            }
            _ => Err(DatarustError::InvalidInput(format!(
                "unknown parameter '{name}' for DecisionTreeRegressor"
            ))),
        }
    }
}

impl DecisionTreeRegressor {
    /// Stable integer code for `max_features` (0 all, 1 sqrt, 2 log2, k+2
    /// an explicit count).
    fn max_features_code(&self) -> usize {
        match self.options.max_features {
            MaxFeatures::All => 0,
            MaxFeatures::Sqrt => 1,
            MaxFeatures::Log2 => 2,
            MaxFeatures::Count(k) => k.saturating_add(2),
        }
    }
}