ferricml 0.2.1

A lean, pure-Rust toolkit for classical machine learning
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
use crate::api::{
    Capabilities, Classifier, Estimator, HasCapabilities, HasParams, ModelError,
    ProbabilisticClassifier, validate_scalar_row,
};
use crate::data::{BinaryTargets, MatrixView};

/// Parameters for [`DummyClassifier`].
///
/// The majority-class baseline has nothing to tune. This type exists so the
/// baseline is fitted exactly like every other FerricML estimator, and so a
/// future strategy choice can be added without changing the `fit` signature.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DummyClassifierParams;

/// A classifier that ignores its features and predicts the majority class.
///
/// Probabilities are the observed class frequencies, so they are identical for
/// every row and sum to one. This is the quality floor a real classifier has to
/// beat: matching it means the features contributed nothing.
///
/// ```
/// use ferricml::api::{Classifier, ProbabilisticClassifier};
/// use ferricml::data::{BinaryTargets, DenseMatrix};
/// use ferricml::dummy::{DummyClassifier, DummyClassifierParams};
///
/// // Three of class 0, one of class 1.
/// let data = DenseMatrix::new(vec![0.0, 1.0, 2.0, 3.0], 4, 1)?;
/// let labels = BinaryTargets::new(vec![0, 0, 0, 1])?;
///
/// let baseline = DummyClassifier::fit(
///     &data.as_view(),
///     &labels,
///     DummyClassifierParams::default(),
/// )?;
///
/// // The majority class, for every row, whatever the features say.
/// assert_eq!(baseline.predict(&data.as_view())?, vec![0, 0, 0, 0]);
///
/// // Probabilities are the observed frequencies, identical on every row.
/// let probabilities = baseline.predict_proba(&data.as_view())?;
/// assert_eq!(&probabilities[0..2], &[0.75, 0.25]);
/// assert_eq!(&probabilities[6..8], &[0.75, 0.25]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct DummyClassifier {
    n_features_in: usize,
    params: DummyClassifierParams,
    classes: Vec<u8>,
    priors: Vec<f32>,
    majority: u8,
}

impl DummyClassifier {
    /// Fits the class frequencies observed in the training targets.
    ///
    /// Ties are resolved towards the smaller class label, matching the tie
    /// rule every other FerricML classifier uses.
    pub fn fit(
        data: &MatrixView<'_>,
        targets: &BinaryTargets,
        params: DummyClassifierParams,
    ) -> Result<Self, ModelError> {
        if data.rows() == 0 || data.columns() == 0 {
            return Err(ModelError::EmptyData);
        }
        if targets.len() != data.rows() {
            return Err(ModelError::TargetLength {
                rows: data.rows(),
                targets: targets.len(),
            });
        }

        // `BinaryTargets` admits only `0` and `1`, so the label is the counter
        // index and there is no third case to reject first.
        let mut counts = [0_usize; 2];
        for &value in targets.as_slice() {
            counts[usize::from(value)] += 1;
        }

        let classes: Vec<u8> = (0..2_u8)
            .filter(|&label| counts[usize::from(label)] > 0)
            .collect();
        let total = targets.len() as f64;
        let priors: Vec<f32> = classes
            .iter()
            .map(|&label| (counts[usize::from(label)] as f64 / total) as f32)
            .collect();
        let majority = Self::majority_label(&classes, &counts);

        Ok(Self {
            n_features_in: data.columns(),
            params,
            classes,
            priors,
            majority,
        })
    }

    /// Scans ascending and keeps only a strictly larger **count**, so an exact
    /// tie resolves towards the smaller label.
    ///
    /// The comparison is on the counts rather than on [`Self::class_priors`]
    /// because a prior is an `f32` view of a ratio of `usize` counts, and
    /// narrowing is not order-preserving: two counts that differ by one collapse
    /// onto one `f32` as soon as their relative gap falls below half an ulp.
    /// The first such pair is `(16_777_216, 16_777_217)`, where both ratios
    /// round to exactly `0.5` — the strict `>` then fails and the *minority*
    /// class is returned. Counts are exact, so the comparison on them is exact.
    ///
    /// One consequence is deliberate: at such a total the predicted label is no
    /// longer the first maximum of the reported probabilities, because the
    /// reported probabilities are equal where the counts are not. The
    /// prediction follows the counts; [`Self::class_priors`] says so.
    fn majority_label(classes: &[u8], counts: &[usize; 2]) -> u8 {
        let mut best = 0;
        for index in 1..classes.len() {
            if counts[usize::from(classes[index])] > counts[usize::from(classes[best])] {
                best = index;
            }
        }
        classes[best]
    }

    /// Returns the feature width required by this model.
    pub const fn n_features_in(&self) -> usize {
        self.n_features_in
    }

    /// Returns sorted class labels observed during fitting.
    pub fn classes(&self) -> &[u8] {
        &self.classes
    }

    /// Returns the exact fitted parameters.
    pub const fn get_params(&self) -> &DummyClassifierParams {
        &self.params
    }

    /// Returns the observed class frequencies, ordered like [`Self::classes`].
    ///
    /// These are `f32` narrowings of exact `usize` ratios, so two frequencies
    /// can be equal here while the counts behind them are not — from a training
    /// set of `33_554_433` rows onwards, a one-row majority is invisible at this
    /// precision. [`Self::predict`] compares the counts, so it still reports the
    /// true majority; only the reported frequencies lose the distinction.
    pub fn class_priors(&self) -> &[f32] {
        &self.priors
    }

    /// Predicts the majority class for one validated row.
    pub fn predict_one(&self, row: &[f32]) -> Result<u8, ModelError> {
        validate_scalar_row(row, self.n_features_in)?;
        Ok(self.majority)
    }

    /// Predicts one label per row, allocating the output.
    pub fn predict(&self, data: &MatrixView<'_>) -> Result<Vec<u8>, ModelError> {
        <Self as Classifier>::predict(self, data)
    }

    /// Predicts one label per row into caller-owned storage.
    pub fn predict_into(&self, data: &MatrixView<'_>, output: &mut [u8]) -> Result<(), ModelError> {
        <Self as Classifier>::predict_into(self, data, output)
    }

    /// Predicts row-major probabilities, allocating the output.
    pub fn predict_proba(&self, data: &MatrixView<'_>) -> Result<Vec<f32>, ModelError> {
        <Self as ProbabilisticClassifier>::predict_proba(self, data)
    }

    /// Predicts row-major probabilities into caller-owned storage.
    pub fn predict_proba_into(
        &self,
        data: &MatrixView<'_>,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        <Self as ProbabilisticClassifier>::predict_proba_into(self, data, output)
    }

    /// Predicts one requested probability column, allocating the output.
    pub fn predict_class_proba(
        &self,
        data: &MatrixView<'_>,
        class: u8,
    ) -> Result<Vec<f32>, ModelError> {
        <Self as ProbabilisticClassifier>::predict_class_proba(self, data, class)
    }

    /// Predicts one requested probability column into caller-owned storage.
    pub fn predict_class_proba_into(
        &self,
        data: &MatrixView<'_>,
        class: u8,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        <Self as ProbabilisticClassifier>::predict_class_proba_into(self, data, class, output)
    }

    fn validate_batch(&self, data: &MatrixView<'_>) -> Result<(), ModelError> {
        if data.columns() != self.n_features_in {
            return Err(ModelError::FeatureDimension {
                expected: self.n_features_in,
                actual: data.columns(),
            });
        }
        Ok(())
    }

    fn validate_output(expected: usize, actual: usize) -> Result<(), ModelError> {
        if expected != actual {
            return Err(ModelError::OutputLength { expected, actual });
        }
        Ok(())
    }
}

impl Estimator for DummyClassifier {
    fn n_features_in(&self) -> usize {
        self.n_features_in
    }
}

impl HasParams for DummyClassifier {
    type Params = DummyClassifierParams;

    fn get_params(&self) -> &Self::Params {
        &self.params
    }
}

/// Declares probabilities and nothing else: a baseline is refitted rather than
/// persisted, and has no weighted entry point. The probabilities are the fitted
/// class frequencies, which is a real distribution rather than a squashed
/// score, so the declaration is earned rather than a formality.
impl HasCapabilities for DummyClassifier {
    const CAPABILITIES: Capabilities = Capabilities::NONE.with_probability(true);
}

impl Classifier for DummyClassifier {
    fn classes(&self) -> &[u8] {
        &self.classes
    }

    fn predict_into(&self, data: &MatrixView<'_>, output: &mut [u8]) -> Result<(), ModelError> {
        self.validate_batch(data)?;
        Self::validate_output(data.rows(), output.len())?;
        output.fill(self.majority);
        Ok(())
    }
}

impl ProbabilisticClassifier for DummyClassifier {
    fn predict_proba_into(
        &self,
        data: &MatrixView<'_>,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        self.validate_batch(data)?;
        let expected =
            data.rows()
                .checked_mul(self.classes.len())
                .ok_or(ModelError::OutputShapeOverflow {
                    rows: data.rows(),
                    columns: self.classes.len(),
                })?;
        Self::validate_output(expected, output.len())?;
        for row in output.chunks_exact_mut(self.classes.len()) {
            row.copy_from_slice(&self.priors);
        }
        Ok(())
    }

    fn predict_class_proba_into(
        &self,
        data: &MatrixView<'_>,
        class: u8,
        output: &mut [f32],
    ) -> Result<(), ModelError> {
        self.validate_batch(data)?;
        let column = self
            .classes
            .iter()
            .position(|&label| label == class)
            .ok_or(ModelError::UnknownClass { class })?;
        Self::validate_output(data.rows(), output.len())?;
        output.fill(self.priors[column]);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::DenseMatrix;

    fn data() -> DenseMatrix {
        DenseMatrix::new(vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0], 3, 2).unwrap()
    }

    #[test]
    fn predicts_the_majority_class_and_its_observed_frequencies() {
        let model = DummyClassifier::fit(
            &data().as_view(),
            &BinaryTargets::new(vec![0, 1, 1]).unwrap(),
            DummyClassifierParams,
        )
        .unwrap();

        assert_eq!(model.classes(), &[0, 1]);
        assert_eq!(model.class_priors(), &[1.0 / 3.0, 2.0 / 3.0]);
        assert_eq!(model.predict(&data().as_view()).unwrap(), vec![1; 3]);
        assert_eq!(
            model.predict_proba(&data().as_view()).unwrap(),
            vec![
                1.0 / 3.0,
                2.0 / 3.0,
                1.0 / 3.0,
                2.0 / 3.0,
                1.0 / 3.0,
                2.0 / 3.0
            ]
        );
        assert_eq!(model.predict_one(&[9.0, 9.0]).unwrap(), 1);
    }

    #[test]
    fn an_exact_tie_selects_the_smaller_class() {
        let data = DenseMatrix::new(vec![0.0, 1.0, 2.0, 3.0], 2, 2).unwrap();
        let model = DummyClassifier::fit(
            &data.as_view(),
            &BinaryTargets::new(vec![0, 1]).unwrap(),
            DummyClassifierParams,
        )
        .unwrap();

        assert_eq!(model.class_priors(), &[0.5, 0.5]);
        assert_eq!(model.predict(&data.as_view()).unwrap(), vec![0; 2]);
    }

    /// The smallest training set on which the majority is invisible in `f32`.
    ///
    /// `16_777_216 / 33_554_433` and `16_777_217 / 33_554_433` both round to
    /// exactly `0.5` — bit pattern `0x3f00_0000` for each — so a comparison on
    /// the reported frequencies sees a tie, resolves it towards the smaller
    /// label, and predicts the class that occurs *less* often. Searched rather
    /// than guessed: at every smaller power-of-two total, down to `1_048_577`,
    /// the same one-row majority is still strictly larger as an `f32`, so this
    /// pair is the first collapse and not merely one of them.
    ///
    /// The row count is what the defect costs to state; the test is cheap
    /// anyway, because the features are one constant column a baseline ignores.
    #[test]
    fn the_majority_is_predicted_where_the_two_frequencies_round_to_one_f32() {
        let minority = 16_777_216_usize;
        let majority = minority + 1;
        let rows = minority + majority;

        let features = vec![0.0_f32; rows];
        let view = MatrixView::new(&features, rows, 1).unwrap();
        let mut labels = vec![0_u8; rows];
        labels[minority..].fill(1);
        let model = DummyClassifier::fit(
            &view,
            &BinaryTargets::new(labels).unwrap(),
            DummyClassifierParams,
        )
        .unwrap();

        // The premise: the two frequencies really are one `f32`, so a
        // comparison on them cannot see the majority. Asserted so that a future
        // change to a wider prior turns this test into a statement about a
        // condition that no longer holds rather than into a silent pass.
        assert_eq!(model.class_priors(), &[0.5, 0.5]);
        assert_eq!(
            model.class_priors()[0].to_bits(),
            model.class_priors()[1].to_bits()
        );

        // Class `1` occurs once more than class `0`, so it is the majority.
        assert_eq!(model.predict_one(&[0.0]).unwrap(), 1);
        assert_eq!(
            model
                .predict(&MatrixView::new(&features[..3], 3, 1).unwrap())
                .unwrap(),
            vec![1; 3]
        );
    }

    #[test]
    fn a_single_observed_class_uses_one_probability_column() {
        let model = DummyClassifier::fit(
            &data().as_view(),
            &BinaryTargets::new(vec![1, 1, 1]).unwrap(),
            DummyClassifierParams,
        )
        .unwrap();

        assert_eq!(model.classes(), &[1]);
        assert_eq!(
            model.predict_proba(&data().as_view()).unwrap(),
            vec![1.0; 3]
        );
        assert_eq!(
            model.predict_class_proba(&data().as_view(), 0).unwrap_err(),
            ModelError::UnknownClass { class: 0 }
        );
    }

    #[test]
    fn fitting_rejects_mismatched_targets_before_any_work() {
        assert_eq!(
            DummyClassifier::fit(
                &data().as_view(),
                &BinaryTargets::new(vec![0, 1]).unwrap(),
                DummyClassifierParams,
            )
            .unwrap_err(),
            ModelError::TargetLength {
                rows: 3,
                targets: 2
            }
        );
    }
}