ferrolearn-preprocess 0.2.0

Preprocessing transformers for the ferrolearn ML framework
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
//! Binary encoder: encode categorical integers as binary digits.
//!
//! [`BinaryEncoder`] encodes each categorical integer feature into `ceil(log2(k))`
//! binary columns, where `k` is the number of distinct categories. This is more
//! compact than one-hot encoding for high-cardinality features.
//!
//! # Example
//!
//! ```text
//! Input column with categories {0, 1, 2, 3}:
//!   0 → [0, 0]
//!   1 → [0, 1]
//!   2 → [1, 0]
//!   3 → [1, 1]
//! ```

use ferrolearn_core::error::FerroError;
use ferrolearn_core::traits::{Fit, FitTransform, Transform};
use ndarray::Array2;
use num_traits::Float;

// ---------------------------------------------------------------------------
// BinaryEncoder (unfitted)
// ---------------------------------------------------------------------------

/// An unfitted binary encoder.
///
/// Takes a matrix of categorical integer features and encodes each category
/// as a sequence of binary digits. For `k` categories, each feature produces
/// `ceil(log2(k))` output columns.
///
/// # Examples
///
/// ```
/// use ferrolearn_preprocess::binary_encoder::BinaryEncoder;
/// use ferrolearn_core::traits::{Fit, Transform};
/// use ndarray::array;
///
/// let enc = BinaryEncoder::<f64>::new();
/// let x = array![[0usize], [1], [2], [3]];
/// let fitted = enc.fit(&x, &()).unwrap();
/// let out = fitted.transform(&x).unwrap();
/// // 4 categories → ceil(log2(4)) = 2 binary columns
/// assert_eq!(out.ncols(), 2);
/// ```
#[must_use]
#[derive(Debug, Clone)]
pub struct BinaryEncoder<F> {
    _marker: std::marker::PhantomData<F>,
}

impl<F: Float + Send + Sync + 'static> BinaryEncoder<F> {
    /// Create a new `BinaryEncoder`.
    pub fn new() -> Self {
        Self {
            _marker: std::marker::PhantomData,
        }
    }
}

impl<F: Float + Send + Sync + 'static> Default for BinaryEncoder<F> {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// FittedBinaryEncoder
// ---------------------------------------------------------------------------

/// A fitted binary encoder holding the number of categories and binary digits
/// per input feature.
///
/// Created by calling [`Fit::fit`] on a [`BinaryEncoder`].
#[derive(Debug, Clone)]
pub struct FittedBinaryEncoder<F> {
    /// Number of categories for each input column.
    n_categories: Vec<usize>,
    /// Number of binary digits for each input column.
    n_digits: Vec<usize>,
    _marker: std::marker::PhantomData<F>,
}

impl<F: Float + Send + Sync + 'static> FittedBinaryEncoder<F> {
    /// Return the number of categories per feature.
    #[must_use]
    pub fn n_categories(&self) -> &[usize] {
        &self.n_categories
    }

    /// Return the number of binary digits per feature.
    #[must_use]
    pub fn n_digits(&self) -> &[usize] {
        &self.n_digits
    }

    /// Return the total number of output columns.
    #[must_use]
    pub fn n_output_features(&self) -> usize {
        self.n_digits.iter().sum()
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Compute ceil(log2(k)), with a minimum of 1.
fn n_binary_digits(k: usize) -> usize {
    if k <= 1 {
        return 1;
    }
    // ceil(log2(k)) = number of bits needed to represent 0..k-1
    let mut bits = 0usize;
    let mut val = k - 1; // maximum value to represent
    while val > 0 {
        bits += 1;
        val >>= 1;
    }
    bits
}

// ---------------------------------------------------------------------------
// Trait implementations
// ---------------------------------------------------------------------------

impl<F: Float + Send + Sync + 'static> Fit<Array2<usize>, ()> for BinaryEncoder<F> {
    type Fitted = FittedBinaryEncoder<F>;
    type Error = FerroError;

    /// Fit by determining the number of categories per column.
    ///
    /// The number of categories for column `j` is `max(x[:, j]) + 1`.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::InsufficientSamples`] if the input has zero rows.
    fn fit(&self, x: &Array2<usize>, _y: &()) -> Result<FittedBinaryEncoder<F>, FerroError> {
        let n_samples = x.nrows();
        if n_samples == 0 {
            return Err(FerroError::InsufficientSamples {
                required: 1,
                actual: 0,
                context: "BinaryEncoder::fit".into(),
            });
        }

        let n_features = x.ncols();
        let mut n_categories = Vec::with_capacity(n_features);
        let mut n_digits_vec = Vec::with_capacity(n_features);

        for j in 0..n_features {
            let col = x.column(j);
            let max_cat = col.iter().copied().max().unwrap_or(0);
            let k = max_cat + 1;
            n_categories.push(k);
            n_digits_vec.push(n_binary_digits(k));
        }

        Ok(FittedBinaryEncoder {
            n_categories,
            n_digits: n_digits_vec,
            _marker: std::marker::PhantomData,
        })
    }
}

impl<F: Float + Send + Sync + 'static> Transform<Array2<usize>> for FittedBinaryEncoder<F> {
    type Output = Array2<F>;
    type Error = FerroError;

    /// Transform categorical data into binary encoded columns.
    ///
    /// # Errors
    ///
    /// Returns [`FerroError::ShapeMismatch`] if the number of columns differs
    /// from the number of features seen during fitting.
    ///
    /// Returns [`FerroError::InvalidParameter`] if any category value exceeds
    /// the maximum seen during fitting.
    fn transform(&self, x: &Array2<usize>) -> Result<Array2<F>, FerroError> {
        let n_features = self.n_categories.len();
        if x.ncols() != n_features {
            return Err(FerroError::ShapeMismatch {
                expected: vec![x.nrows(), n_features],
                actual: vec![x.nrows(), x.ncols()],
                context: "FittedBinaryEncoder::transform".into(),
            });
        }

        let n_samples = x.nrows();
        let n_out = self.n_output_features();
        let mut out = Array2::zeros((n_samples, n_out));

        let mut col_offset = 0;
        for j in 0..n_features {
            let n_cats = self.n_categories[j];
            let digits = self.n_digits[j];

            for i in 0..n_samples {
                let cat = x[[i, j]];
                if cat >= n_cats {
                    return Err(FerroError::InvalidParameter {
                        name: format!("x[{i},{j}]"),
                        reason: format!(
                            "category {cat} exceeds max seen during fitting ({})",
                            n_cats - 1
                        ),
                    });
                }

                // Encode category as binary digits (MSB first)
                for bit in 0..digits {
                    let bit_pos = digits - 1 - bit;
                    if (cat >> bit_pos) & 1 == 1 {
                        out[[i, col_offset + bit]] = F::one();
                    }
                }
            }

            col_offset += digits;
        }

        Ok(out)
    }
}

/// Implement `Transform` on the unfitted encoder.
impl<F: Float + Send + Sync + 'static> Transform<Array2<usize>> for BinaryEncoder<F> {
    type Output = Array2<F>;
    type Error = FerroError;

    /// Always returns an error — the encoder must be fitted first.
    fn transform(&self, _x: &Array2<usize>) -> Result<Array2<F>, FerroError> {
        Err(FerroError::InvalidParameter {
            name: "BinaryEncoder".into(),
            reason: "encoder must be fitted before calling transform; use fit() first".into(),
        })
    }
}

impl<F: Float + Send + Sync + 'static> FitTransform<Array2<usize>> for BinaryEncoder<F> {
    type FitError = FerroError;

    /// Fit and transform in one step.
    ///
    /// # Errors
    ///
    /// Returns an error if fitting or transformation fails.
    fn fit_transform(&self, x: &Array2<usize>) -> Result<Array2<F>, FerroError> {
        let fitted = self.fit(x, &())?;
        fitted.transform(x)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_binary_encoder_basic() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [1], [2], [3]];
        let fitted = enc.fit(&x, &()).unwrap();
        let out = fitted.transform(&x).unwrap();
        // 4 categories → ceil(log2(4)) = 2 columns
        assert_eq!(out.ncols(), 2);
        // 0 → [0, 0]
        assert_eq!(out.row(0).to_vec(), vec![0.0, 0.0]);
        // 1 → [0, 1]
        assert_eq!(out.row(1).to_vec(), vec![0.0, 1.0]);
        // 2 → [1, 0]
        assert_eq!(out.row(2).to_vec(), vec![1.0, 0.0]);
        // 3 → [1, 1]
        assert_eq!(out.row(3).to_vec(), vec![1.0, 1.0]);
    }

    #[test]
    fn test_binary_encoder_five_categories() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [1], [2], [3], [4]];
        let fitted = enc.fit(&x, &()).unwrap();
        let out = fitted.transform(&x).unwrap();
        // 5 categories → ceil(log2(5)) = 3 columns
        assert_eq!(out.ncols(), 3);
        // 0 → [0, 0, 0]
        assert_eq!(out.row(0).to_vec(), vec![0.0, 0.0, 0.0]);
        // 4 → [1, 0, 0]
        assert_eq!(out.row(4).to_vec(), vec![1.0, 0.0, 0.0]);
    }

    #[test]
    fn test_binary_encoder_single_category() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [0], [0]];
        let fitted = enc.fit(&x, &()).unwrap();
        let out = fitted.transform(&x).unwrap();
        // 1 category → 1 binary column (always 0)
        assert_eq!(out.ncols(), 1);
        for i in 0..3 {
            assert_eq!(out[[i, 0]], 0.0);
        }
    }

    #[test]
    fn test_binary_encoder_two_categories() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [1]];
        let fitted = enc.fit(&x, &()).unwrap();
        let out = fitted.transform(&x).unwrap();
        // 2 categories → ceil(log2(2)) = 1 column
        assert_eq!(out.ncols(), 1);
        assert_eq!(out[[0, 0]], 0.0);
        assert_eq!(out[[1, 0]], 1.0);
    }

    #[test]
    fn test_binary_encoder_multi_feature() {
        let enc = BinaryEncoder::<f64>::new();
        // Feature 0: 3 categories → 2 digits
        // Feature 1: 2 categories → 1 digit
        let x = array![[0usize, 0], [1, 1], [2, 0]];
        let fitted = enc.fit(&x, &()).unwrap();
        assert_eq!(fitted.n_output_features(), 3); // 2 + 1
        let out = fitted.transform(&x).unwrap();
        assert_eq!(out.ncols(), 3);
    }

    #[test]
    fn test_binary_encoder_n_binary_digits() {
        assert_eq!(n_binary_digits(1), 1);
        assert_eq!(n_binary_digits(2), 1);
        assert_eq!(n_binary_digits(3), 2);
        assert_eq!(n_binary_digits(4), 2);
        assert_eq!(n_binary_digits(5), 3);
        assert_eq!(n_binary_digits(8), 3);
        assert_eq!(n_binary_digits(9), 4);
    }

    #[test]
    fn test_binary_encoder_fit_transform() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [1], [2], [3]];
        let out: Array2<f64> = enc.fit_transform(&x).unwrap();
        assert_eq!(out.ncols(), 2);
    }

    #[test]
    fn test_binary_encoder_zero_rows_error() {
        let enc = BinaryEncoder::<f64>::new();
        let x: Array2<usize> = Array2::zeros((0, 2));
        assert!(enc.fit(&x, &()).is_err());
    }

    #[test]
    fn test_binary_encoder_out_of_range_error() {
        let enc = BinaryEncoder::<f64>::new();
        let x_train = array![[0usize], [1]]; // max category = 1
        let fitted = enc.fit(&x_train, &()).unwrap();
        let x_bad = array![[2usize]]; // category 2 not seen
        assert!(fitted.transform(&x_bad).is_err());
    }

    #[test]
    fn test_binary_encoder_shape_mismatch_error() {
        let enc = BinaryEncoder::<f64>::new();
        let x_train = array![[0usize, 1], [1, 0]];
        let fitted = enc.fit(&x_train, &()).unwrap();
        let x_bad = array![[0usize]]; // wrong number of columns
        assert!(fitted.transform(&x_bad).is_err());
    }

    #[test]
    fn test_binary_encoder_unfitted_error() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize]];
        assert!(enc.transform(&x).is_err());
    }

    #[test]
    fn test_binary_encoder_accessors() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [1], [2], [3]];
        let fitted = enc.fit(&x, &()).unwrap();
        assert_eq!(fitted.n_categories(), &[4]);
        assert_eq!(fitted.n_digits(), &[2]);
        assert_eq!(fitted.n_output_features(), 2);
    }

    #[test]
    fn test_binary_encoder_eight_categories() {
        let enc = BinaryEncoder::<f64>::new();
        let x = array![[0usize], [1], [2], [3], [4], [5], [6], [7]];
        let fitted = enc.fit(&x, &()).unwrap();
        let out = fitted.transform(&x).unwrap();
        // 8 categories → ceil(log2(8)) = 3 columns
        assert_eq!(out.ncols(), 3);
        // 7 → [1, 1, 1]
        assert_eq!(out.row(7).to_vec(), vec![1.0, 1.0, 1.0]);
        // 5 → [1, 0, 1]
        assert_eq!(out.row(5).to_vec(), vec![1.0, 0.0, 1.0]);
    }
}