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
use std::error::Error;
use std::fmt;

use crate::api::Regressor;
use crate::data::{ClassificationTargets, MatrixView, RegressionTargets};
use crate::model_selection::{
    ClassificationScore, RegressionScore, ScorableClassifier, ScoringError, ScoringWorkspace,
    score_labelled, score_regressor_with,
};
use crate::numeric::OwnedRng;

/// Parameters for a permutation-importance run.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PermutationImportanceParams {
    n_repeats: usize,
    random_state: u64,
}

impl Default for PermutationImportanceParams {
    fn default() -> Self {
        Self {
            n_repeats: 5,
            random_state: 0,
        }
    }
}

impl PermutationImportanceParams {
    /// Sets how many independent permutations each feature receives.
    #[must_use]
    pub fn with_n_repeats(mut self, n_repeats: usize) -> Self {
        self.n_repeats = n_repeats;
        self
    }

    /// Sets the deterministic permutation seed.
    #[must_use]
    pub fn with_random_state(mut self, random_state: u64) -> Self {
        self.random_state = random_state;
        self
    }

    /// Returns the number of permutations per feature.
    pub const fn n_repeats(&self) -> usize {
        self.n_repeats
    }

    /// Returns the deterministic permutation seed.
    pub const fn random_state(&self) -> u64 {
        self.random_state
    }
}

/// Per-feature permutation importance.
///
/// Each value is the loss of scorer quality caused by permuting that feature,
/// so a larger number always means a more important feature whether the
/// underlying metric is maximized (accuracy, R²) or minimized (log loss, mean
/// squared error). Both slices are indexed by input feature.
#[derive(Clone, Debug, PartialEq)]
pub struct PermutationImportance {
    means: Vec<f64>,
    std_devs: Vec<f64>,
}

impl PermutationImportance {
    /// Number of inspected features.
    pub fn n_features(&self) -> usize {
        self.means.len()
    }

    /// Mean quality loss per feature, in scorer units.
    pub fn means(&self) -> &[f64] {
        &self.means
    }

    /// Population standard deviation of the per-repeat quality loss.
    pub fn std_devs(&self) -> &[f64] {
        &self.std_devs
    }

    /// Feature indices ordered by decreasing mean importance.
    ///
    /// Equal means keep their natural feature order, so the ranking is
    /// deterministic for every input.
    pub fn ranked(&self) -> Vec<usize> {
        let mut order: Vec<usize> = (0..self.means.len()).collect();
        order.sort_by(|&left, &right| {
            self.means[right]
                .total_cmp(&self.means[left])
                .then(left.cmp(&right))
        });
        order
    }
}

/// Errors produced while inspecting a fitted model.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum InspectionError {
    /// The requested permutation count was zero.
    InvalidRepeatCount,
    /// A caller-owned output slice did not have one entry per feature.
    OutputLength {
        /// Required length.
        expected: usize,
        /// Supplied length.
        actual: usize,
    },
    /// Scoring the fitted model failed.
    Scoring(ScoringError),
}

impl fmt::Display for InspectionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidRepeatCount => f.write_str("permutation repeats must be at least one"),
            Self::OutputLength { expected, actual } => write!(
                f,
                "importance output length {actual} does not match {expected} features"
            ),
            Self::Scoring(error) => write!(f, "scoring failed: {error}"),
        }
    }
}

impl Error for InspectionError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::Scoring(error) => Some(error),
            _ => None,
        }
    }
}

/// Measures permutation importance for a fitted classifier.
///
/// This is the crate's only classifier permutation-importance entry point, over
/// **any** [`ClassificationTargets`] vocabulary. Shuffling a column and
/// re-scoring says how much the model relied on it; how many classes the labels
/// name is a property of the *metric*, so
/// [`ClassTargets`](crate::data::ClassTargets) is inspected here exactly as
/// [`BinaryTargets`](crate::data::BinaryTargets) is. A score reading
/// [`ClassifierOutputKind::ProbabilityMatrix`](crate::model_selection::ClassifierOutputKind::ProbabilityMatrix)
/// works for any observed class set, while the binary positive-probability
/// layouts still refuse a wider one with
/// [`ScoringError::UnsupportedClasses`] rather than reinterpreting a column.
///
/// Values are quality **losses**, oriented so a larger number always means a
/// more important feature — whichever direction the underlying metric improves
/// in. The orientation is taken from the score's own declaration rather than
/// assumed.
///
/// ```
/// use ferricml::data::{ClassTargets, DenseMatrix};
/// use ferricml::inspection::{PermutationImportanceParams, permutation_importance_classifier};
/// use ferricml::model_selection::{ClassificationScorer, ScorableClassifier};
/// use ferricml::tree::{DecisionTreeClassifier, DecisionTreeClassifierParams};
///
/// // Column 0 determines one of three classes; column 1 alternates and says
/// // nothing. The labels are neither contiguous nor zero-based.
/// let mut values = Vec::new();
/// let mut labels = Vec::new();
/// for index in 0..30_usize {
///     values.push((index % 3) as f32);
///     values.push(if index % 2 == 0 { 1.0 } else { -1.0 });
///     labels.push([3, 7, 10][index % 3]);
/// }
/// let data = DenseMatrix::new(values, 30, 2)?;
/// let targets = ClassTargets::new(labels)?;
/// assert_eq!(targets.classes(), &[3, 7, 10]);
///
/// let model = DecisionTreeClassifier::fit_multiclass(
///     &data.as_view(),
///     &targets,
///     DecisionTreeClassifierParams::default().with_min_samples_leaf(1),
/// )?;
///
/// let importance = permutation_importance_classifier(
///     ScorableClassifier::probabilistic(&model),
///     &data.as_view(),
///     &targets,
///     ClassificationScorer::MulticlassLogLoss,
///     PermutationImportanceParams::default().with_random_state(5),
/// )?;
///
/// // The deciding column matters; the alternating one does not.
/// assert_eq!(importance.ranked()[0], 0);
/// assert!(importance.means()[0] > importance.means()[1]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn permutation_importance_classifier<T: ClassificationTargets, S: ClassificationScore>(
    classifier: ScorableClassifier<'_>,
    data: &MatrixView<'_>,
    targets: &T,
    scorer: S,
    params: PermutationImportanceParams,
) -> Result<PermutationImportance, InspectionError> {
    let mut means = vec![0.0; data.columns()];
    let mut std_devs = vec![0.0; data.columns()];
    permutation_importance_classifier_into(
        classifier,
        data,
        targets,
        scorer,
        params,
        &mut means,
        &mut std_devs,
    )?;
    Ok(PermutationImportance { means, std_devs })
}

/// Measures permutation importance for a fitted classifier into caller-owned
/// storage.
///
/// Both output slices must have one entry per input feature. Prediction and
/// permutation workspace is allocated once, so the cost of extra repeats is
/// scoring alone.
pub fn permutation_importance_classifier_into<T: ClassificationTargets, S: ClassificationScore>(
    classifier: ScorableClassifier<'_>,
    data: &MatrixView<'_>,
    targets: &T,
    scorer: S,
    params: PermutationImportanceParams,
    means: &mut [f64],
    std_devs: &mut [f64],
) -> Result<(), InspectionError> {
    let labels = targets.as_slice();
    validate(data, labels.len(), params, means, std_devs)?;
    let greater_is_better = scorer.greater_is_better();
    let mut workspace = ScoringWorkspace::new();
    let mut score =
        |view: &MatrixView<'_>| score_labelled(classifier, view, labels, &scorer, &mut workspace);
    run_permutations(data, params, greater_is_better, means, std_devs, &mut score)
}

/// Measures permutation importance for a fitted regressor.
///
/// Shuffling a column and re-scoring says how much the model relied on it.
/// This works through the public batch prediction and scoring contracts alone,
/// so it needs no cooperation from the estimator and exposes no internals.
///
/// Values are quality **losses**, oriented so a larger number always means a
/// more important feature — whichever direction the underlying metric improves
/// in. The orientation is taken from the score's own declaration rather than
/// assumed.
///
/// ```
/// use ferricml::data::{DenseMatrix, RegressionTargets};
/// use ferricml::inspection::{PermutationImportanceParams, permutation_importance_regressor};
/// use ferricml::linear_model::{LinearRegression, LinearRegressionParams};
/// use ferricml::model_selection::RegressionScorer;
///
/// // Column 0 drives the target; column 1 is noise.
/// let mut values = Vec::new();
/// let mut targets = Vec::new();
/// for index in 0..24 {
///     let signal = index as f32;
///     values.push(signal);
///     values.push(if index % 2 == 0 { 1.0 } else { -1.0 });
///     targets.push(2.0 * signal);
/// }
/// let data = DenseMatrix::new(values, 24, 2)?;
/// let targets = RegressionTargets::new(targets)?;
///
/// let model = LinearRegression::fit(
///     &data.as_view(),
///     &targets,
///     LinearRegressionParams::default(),
/// )?;
///
/// let importance = permutation_importance_regressor(
///     &model,
///     &data.as_view(),
///     &targets,
///     RegressionScorer::MeanSquaredError,
///     PermutationImportanceParams::default().with_random_state(4),
/// )?;
///
/// assert_eq!(importance.n_features(), 2);
/// // The signal column matters; the noise column does not.
/// assert!(importance.means()[0] > importance.means()[1]);
/// // `ranked` orders features most important first.
/// assert_eq!(importance.ranked()[0], 0);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn permutation_importance_regressor<S: RegressionScore>(
    regressor: &dyn Regressor,
    data: &MatrixView<'_>,
    targets: &RegressionTargets,
    scorer: S,
    params: PermutationImportanceParams,
) -> Result<PermutationImportance, InspectionError> {
    let mut means = vec![0.0; data.columns()];
    let mut std_devs = vec![0.0; data.columns()];
    permutation_importance_regressor_into(
        regressor,
        data,
        targets,
        scorer,
        params,
        &mut means,
        &mut std_devs,
    )?;
    Ok(PermutationImportance { means, std_devs })
}

/// Measures permutation importance for a fitted regressor into caller-owned
/// storage.
///
/// Both output slices must have one entry per input feature. Prediction and
/// permutation workspace is allocated once, so the cost of extra repeats is
/// scoring alone.
pub fn permutation_importance_regressor_into<S: RegressionScore>(
    regressor: &dyn Regressor,
    data: &MatrixView<'_>,
    targets: &RegressionTargets,
    scorer: S,
    params: PermutationImportanceParams,
    means: &mut [f64],
    std_devs: &mut [f64],
) -> Result<(), InspectionError> {
    validate(data, targets.len(), params, means, std_devs)?;
    let greater_is_better = scorer.greater_is_better();
    let mut workspace = ScoringWorkspace::new();
    let mut score = |view: &MatrixView<'_>| {
        score_regressor_with(regressor, view, targets, &scorer, &mut workspace)
    };
    run_permutations(data, params, greater_is_better, means, std_devs, &mut score)
}

/// Rejects shape and parameter problems before any prediction work happens.
fn validate(
    data: &MatrixView<'_>,
    targets: usize,
    params: PermutationImportanceParams,
    means: &[f64],
    std_devs: &[f64],
) -> Result<(), InspectionError> {
    if data.rows() != targets {
        return Err(InspectionError::Scoring(ScoringError::TargetLength {
            rows: data.rows(),
            targets,
        }));
    }
    for output in [means, std_devs] {
        if output.len() != data.columns() {
            return Err(InspectionError::OutputLength {
                expected: data.columns(),
                actual: output.len(),
            });
        }
    }
    if params.n_repeats == 0 {
        return Err(InspectionError::InvalidRepeatCount);
    }
    Ok(())
}

/// Scores the unpermuted data once, then each feature `n_repeats` times.
///
/// Features are visited in ascending index order and repeats in ascending
/// order within a feature, drawing from one seeded stream, which is what makes
/// the result reproducible for a given seed.
fn run_permutations(
    data: &MatrixView<'_>,
    params: PermutationImportanceParams,
    greater_is_better: bool,
    means: &mut [f64],
    std_devs: &mut [f64],
    score: &mut dyn FnMut(&MatrixView<'_>) -> Result<f64, ScoringError>,
) -> Result<(), InspectionError> {
    let rows = data.rows();
    let columns = data.columns();
    let baseline = score(data).map_err(InspectionError::Scoring)?;

    let mut values: Vec<f32> = data.iter_rows().flatten().copied().collect();
    let mut order: Vec<usize> = (0..rows).collect();
    let mut original = vec![0.0_f32; rows];
    let mut rng = OwnedRng::new(params.random_state);

    for feature in 0..columns {
        for (row, slot) in original.iter_mut().enumerate() {
            *slot = values[row * columns + feature];
        }
        let mut total = 0.0_f64;
        let mut total_squares = 0.0_f64;
        for _ in 0..params.n_repeats {
            shuffle(&mut order, &mut rng);
            for row in 0..rows {
                values[row * columns + feature] = original[order[row]];
            }
            let view = MatrixView::new(&values, rows, columns)
                .expect("permuting a column preserves the validated matrix");
            let permuted = score(&view).map_err(InspectionError::Scoring)?;
            let delta = if greater_is_better {
                baseline - permuted
            } else {
                permuted - baseline
            };
            total += delta;
            total_squares += delta * delta;
        }
        for (row, &value) in original.iter().enumerate() {
            values[row * columns + feature] = value;
        }

        let repeats = params.n_repeats as f64;
        let mean = total / repeats;
        means[feature] = mean;
        // Population variance, clamped against cancellation noise.
        std_devs[feature] = (total_squares / repeats - mean * mean).max(0.0).sqrt();
    }
    Ok(())
}

fn shuffle(order: &mut [usize], rng: &mut OwnedRng) {
    for index in (1..order.len()).rev() {
        order.swap(index, rng.index(index + 1));
    }
}