anofox-ml-preprocessing 0.1.0

Feature preprocessing, scaling, and PCA for the anofox-ml machine learning library
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
use anofox_ml_core::{FitUnsupervised, Float, Result, RustMlError, Transform};
use ndarray::Array2;

/// Strategy for computing bin edges.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum BinStrategy {
    /// All bins have equal width: `(max - min) / n_bins`.
    Uniform,
    /// All bins have approximately the same number of samples (quantile-based).
    Quantile,
}

/// Encoding strategy for transformed output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum EncodeStrategy {
    /// Each value is replaced by its integer bin index (0-based).
    Ordinal,
    /// Each feature is expanded into `n_bins` binary columns (one-hot encoding).
    Onehot,
}

/// Parameters for KBinsDiscretizer (unfitted state).
///
/// Bins continuous features into discrete intervals. Two binning strategies
/// are supported: uniform-width and quantile-based. Output can be ordinal
/// (bin indices) or one-hot encoded.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct KBinsDiscretizer {
    /// Number of bins per feature.
    pub n_bins: usize,
    /// Strategy for computing bin edges.
    pub strategy: BinStrategy,
    /// Encoding strategy for the output.
    pub encode: EncodeStrategy,
}

impl KBinsDiscretizer {
    /// Create a new `KBinsDiscretizer` with defaults (5 bins, quantile strategy, ordinal encoding).
    pub fn new() -> Self {
        Self {
            n_bins: 5,
            strategy: BinStrategy::Quantile,
            encode: EncodeStrategy::Ordinal,
        }
    }

    /// Set the number of bins.
    pub fn n_bins(mut self, n_bins: usize) -> Self {
        self.n_bins = n_bins;
        self
    }

    /// Set the binning strategy.
    pub fn strategy(mut self, strategy: BinStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Set the encoding strategy.
    pub fn encode(mut self, encode: EncodeStrategy) -> Self {
        self.encode = encode;
        self
    }
}

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

/// Fitted KBinsDiscretizer -- holds bin edges per feature.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(bound(deserialize = "F: serde::de::DeserializeOwned"))]
pub struct FittedKBinsDiscretizer<F: Float> {
    /// Bin edges per feature. Each inner vec has `n_bins + 1` values.
    bin_edges: Vec<Vec<F>>,
    n_bins: usize,
    encode: EncodeStrategy,
}

/// Compute a percentile from a sorted slice using linear interpolation.
fn percentile_sorted<F: Float>(sorted: &[F], p: f64) -> F {
    let n = sorted.len();
    if n == 1 {
        return sorted[0];
    }
    let idx = p * (n - 1) as f64;
    let lo = idx.floor() as usize;
    let hi = idx.ceil().min((n - 1) as f64) as usize;
    if lo == hi {
        sorted[lo]
    } else {
        let frac = F::from_f64(idx - lo as f64).unwrap();
        sorted[lo] * (F::one() - frac) + sorted[hi] * frac
    }
}

impl<F: Float> FitUnsupervised<F> for KBinsDiscretizer {
    type Fitted = FittedKBinsDiscretizer<F>;

    fn fit(&self, x: &Array2<F>) -> Result<Self::Fitted> {
        if x.is_empty() {
            return Err(RustMlError::EmptyInput("input array is empty".into()));
        }
        if self.n_bins < 2 {
            return Err(RustMlError::InvalidParameter(
                "n_bins must be at least 2".into(),
            ));
        }

        let ncols = x.ncols();
        let mut bin_edges = Vec::with_capacity(ncols);

        for j in 0..ncols {
            let mut col: Vec<F> = x.column(j).to_vec();
            col.sort_by(|a, b| a.partial_cmp(b).unwrap());

            let edges = match self.strategy {
                BinStrategy::Uniform => {
                    let min_val = col[0];
                    let max_val = col[col.len() - 1];
                    let range = max_val - min_val;
                    let step = range / F::from_usize(self.n_bins).unwrap();
                    let mut e = Vec::with_capacity(self.n_bins + 1);
                    for i in 0..=self.n_bins {
                        e.push(min_val + step * F::from_usize(i).unwrap());
                    }
                    e
                }
                BinStrategy::Quantile => {
                    let mut e = Vec::with_capacity(self.n_bins + 1);
                    for i in 0..=self.n_bins {
                        let p = i as f64 / self.n_bins as f64;
                        e.push(percentile_sorted(&col, p));
                    }
                    e
                }
            };

            bin_edges.push(edges);
        }

        Ok(FittedKBinsDiscretizer {
            bin_edges,
            n_bins: self.n_bins,
            encode: self.encode,
        })
    }
}

/// Find the bin index for a value given bin edges.
/// Returns a 0-based bin index in [0, n_bins - 1].
fn find_bin<F: Float>(val: F, edges: &[F], n_bins: usize) -> usize {
    // Binary search: find the rightmost edge <= val
    let mut lo = 0;
    let mut hi = edges.len() - 1;

    // Clamp to first/last bin for out-of-range values
    if val <= edges[0] {
        return 0;
    }
    if val >= edges[edges.len() - 1] {
        return n_bins - 1;
    }

    while lo + 1 < hi {
        let mid = (lo + hi) / 2;
        if edges[mid] <= val {
            lo = mid;
        } else {
            hi = mid;
        }
    }

    // lo is the index of the left edge of the bin, bin index = lo
    // Clamp to [0, n_bins - 1]
    lo.min(n_bins - 1)
}

impl<F: Float> Transform<F> for FittedKBinsDiscretizer<F> {
    fn transform(&self, x: &Array2<F>) -> Result<Array2<F>> {
        let expected_cols = self.bin_edges.len();
        if x.ncols() != expected_cols {
            return Err(RustMlError::ShapeMismatch(format!(
                "expected {} features, got {}",
                expected_cols,
                x.ncols()
            )));
        }

        match self.encode {
            EncodeStrategy::Ordinal => {
                let mut result = Array2::<F>::zeros(x.raw_dim());
                for i in 0..x.nrows() {
                    for j in 0..x.ncols() {
                        let bin = find_bin(x[[i, j]], &self.bin_edges[j], self.n_bins);
                        result[[i, j]] = F::from_usize(bin).unwrap();
                    }
                }
                Ok(result)
            }
            EncodeStrategy::Onehot => {
                let out_cols = expected_cols * self.n_bins;
                let mut result = Array2::<F>::zeros((x.nrows(), out_cols));
                for i in 0..x.nrows() {
                    for j in 0..x.ncols() {
                        let bin = find_bin(x[[i, j]], &self.bin_edges[j], self.n_bins);
                        let col_offset = j * self.n_bins + bin;
                        result[[i, col_offset]] = F::one();
                    }
                }
                Ok(result)
            }
        }
    }
}

impl<F: Float> FittedKBinsDiscretizer<F> {
    /// Return the bin edges per feature.
    pub fn bin_edges(&self) -> &Vec<Vec<F>> {
        &self.bin_edges
    }

    /// Return the number of bins.
    pub fn n_bins(&self) -> usize {
        self.n_bins
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use ndarray::array;

    #[test]
    fn test_uniform_ordinal() {
        let x = array![
            [0.0, 0.0],
            [2.5, 5.0],
            [5.0, 10.0],
            [7.5, 15.0],
            [10.0, 20.0],
        ];
        let kbd = KBinsDiscretizer::new()
            .n_bins(4)
            .strategy(BinStrategy::Uniform)
            .encode(EncodeStrategy::Ordinal);
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();
        let transformed = fitted.transform(&x).unwrap();

        // Uniform bins for col 0: [0, 2.5, 5, 7.5, 10]
        // 0.0 -> bin 0, 2.5 -> bin 1, 5.0 -> bin 2, 7.5 -> bin 3, 10.0 -> bin 3
        assert_abs_diff_eq!(transformed[[0, 0]], 0.0, epsilon = 1e-10);
        assert_abs_diff_eq!(transformed[[1, 0]], 1.0, epsilon = 1e-10);
        assert_abs_diff_eq!(transformed[[2, 0]], 2.0, epsilon = 1e-10);
        assert_abs_diff_eq!(transformed[[4, 0]], 3.0, epsilon = 1e-10);
    }

    #[test]
    fn test_quantile_ordinal() {
        let x = array![
            [1.0],
            [2.0],
            [3.0],
            [4.0],
            [5.0],
            [6.0],
            [7.0],
            [8.0],
            [9.0],
            [10.0],
        ];
        let kbd = KBinsDiscretizer::new()
            .n_bins(5)
            .strategy(BinStrategy::Quantile)
            .encode(EncodeStrategy::Ordinal);
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();
        let transformed = fitted.transform(&x).unwrap();

        // All bin indices should be in [0, 4]
        for &v in transformed.iter() {
            assert!(v >= 0.0 && v <= 4.0, "bin index out of range: {}", v);
        }

        // Values should be non-decreasing (monotonic)
        for i in 1..x.nrows() {
            assert!(
                transformed[[i, 0]] >= transformed[[i - 1, 0]],
                "monotonicity violated at row {}",
                i
            );
        }
    }

    #[test]
    fn test_onehot_encoding() {
        let x = array![[1.0], [3.0], [5.0], [7.0], [9.0]];
        let kbd = KBinsDiscretizer::new()
            .n_bins(3)
            .strategy(BinStrategy::Uniform)
            .encode(EncodeStrategy::Onehot);
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();
        let transformed = fitted.transform(&x).unwrap();

        // Output should have 3 columns (1 feature * 3 bins)
        assert_eq!(transformed.ncols(), 3);

        // Each row should have exactly one 1.0 and two 0.0
        for i in 0..transformed.nrows() {
            let row_sum: f64 = transformed.row(i).sum();
            assert_abs_diff_eq!(row_sum, 1.0, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_onehot_multiple_features() {
        let x = array![[1.0, 10.0], [5.0, 50.0], [9.0, 90.0]];
        let kbd = KBinsDiscretizer::new()
            .n_bins(3)
            .strategy(BinStrategy::Uniform)
            .encode(EncodeStrategy::Onehot);
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();
        let transformed = fitted.transform(&x).unwrap();

        // Output should have 6 columns (2 features * 3 bins)
        assert_eq!(transformed.ncols(), 6);

        // Each row: exactly two 1.0 values (one per feature)
        for i in 0..transformed.nrows() {
            let row_sum: f64 = transformed.row(i).sum();
            assert_abs_diff_eq!(row_sum, 2.0, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_empty_input() {
        let x: Array2<f64> = Array2::zeros((0, 0));
        let kbd = KBinsDiscretizer::default();
        assert!(FitUnsupervised::<f64>::fit(&kbd, &x).is_err());
    }

    #[test]
    fn test_invalid_n_bins() {
        let x = array![[1.0], [2.0], [3.0]];
        let kbd = KBinsDiscretizer::new().n_bins(1);
        assert!(FitUnsupervised::<f64>::fit(&kbd, &x).is_err());
    }

    #[test]
    fn test_shape_mismatch() {
        let x = array![[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]];
        let kbd = KBinsDiscretizer::default();
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();

        let x_wrong = array![[1.0, 2.0, 3.0]];
        assert!(fitted.transform(&x_wrong).is_err());
    }

    #[test]
    fn test_out_of_range_values() {
        let x = array![[1.0], [2.0], [3.0], [4.0], [5.0]];
        let kbd = KBinsDiscretizer::new()
            .n_bins(3)
            .strategy(BinStrategy::Uniform)
            .encode(EncodeStrategy::Ordinal);
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();

        // Transform values outside the fitted range
        let x_test = array![[-10.0], [0.0], [3.0], [6.0], [100.0]];
        let transformed = fitted.transform(&x_test).unwrap();

        // Out-of-range should clamp to first/last bin
        assert_abs_diff_eq!(transformed[[0, 0]], 0.0, epsilon = 1e-10); // below min
        assert_abs_diff_eq!(transformed[[4, 0]], 2.0, epsilon = 1e-10); // above max
    }

    #[test]
    fn test_constant_feature() {
        let x = array![[5.0], [5.0], [5.0], [5.0]];
        let kbd = KBinsDiscretizer::new()
            .n_bins(3)
            .strategy(BinStrategy::Uniform)
            .encode(EncodeStrategy::Ordinal);
        let fitted = FitUnsupervised::<f64>::fit(&kbd, &x).unwrap();
        let transformed = fitted.transform(&x).unwrap();

        // All values should map to the same bin (or at least be finite)
        for &v in transformed.iter() {
            assert!(v.is_finite(), "constant feature produced non-finite: {}", v);
        }
    }

    #[test]
    fn test_f32() {
        let x = array![[1.0f32, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]];
        let kbd = KBinsDiscretizer::new()
            .n_bins(3)
            .strategy(BinStrategy::Quantile)
            .encode(EncodeStrategy::Ordinal);
        let fitted = FitUnsupervised::<f32>::fit(&kbd, &x).unwrap();
        let transformed = fitted.transform(&x).unwrap();

        for &v in transformed.iter() {
            assert!(v.is_finite());
            assert!(v >= 0.0 && v < 3.0);
        }
    }
}