featrs 0.3.4

Feature engineering library for Rust, inspired by scikit-learn
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
//! Missing value imputation.
//!
//! [`SimpleImputer`] replaces missing (`None` / `null`) values with
//! a statistic computed from the non-missing values in each column.

use crate::traits::{Error, Fit, Result, Transform};
use polars::prelude::*;
use std::collections::HashMap;

/// Strategy for computing imputation values.
#[derive(Clone, Copy)]
pub enum Strategy {
    /// Replace missing values with the column mean.
    Mean,
    /// Replace missing values with the column median.
    Median,
    /// Replace missing values with the most frequent value in the column.
    MostFrequent,
    /// Replace missing values with a constant value.
    Constant(f64),
}

/// Impute missing values using basic statistics.
///
/// Only operates on [`Float64`](DataType::Float64) columns.
///
/// # Example
///
/// ```rust
/// use featrs::preprocessing::imputer::SimpleImputer;
/// use featrs::traits::{Fit, Transform};
/// use polars::prelude::{Column, DataFrame, NamedFrom, Series};
///
/// let col = Column::from(Series::new("x".into(), &[Some(1.0_f64), None, Some(3.0)]));
/// let df = DataFrame::new(3, vec![col])?;
///
/// let mut imp = SimpleImputer::mean();
/// imp.fit(df.clone())?;
/// let filled = imp.transform(df)?;
/// assert_eq!(filled.height(), 3);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct SimpleImputer {
    fitted: bool,
    strategy: Strategy,
    fill_values: Option<HashMap<String, f64>>,
}

impl SimpleImputer {
    /// Create a new `SimpleImputer` with the given strategy.
    pub fn new(strategy: Strategy) -> Self {
        Self {
            fitted: false,
            strategy,
            fill_values: None,
        }
    }

    /// Impute using the column mean.
    pub fn mean() -> Self {
        Self::new(Strategy::Mean)
    }

    /// Impute using the column median.
    pub fn median() -> Self {
        Self::new(Strategy::Median)
    }

    /// Impute using the most frequent (mode) value.
    pub fn most_frequent() -> Self {
        Self::new(Strategy::MostFrequent)
    }

    /// Impute with a constant value.
    pub fn constant(value: f64) -> Self {
        Self::new(Strategy::Constant(value))
    }
}

impl Fit<DataFrame> for SimpleImputer {
    type Output = ();

    fn fit(&mut self, x: DataFrame) -> Result<()> {
        if x.height() == 0 {
            return Err(Error::InvalidInput(
                "SimpleImputer.fit received a DataFrame with 0 rows. \
                 Provide data with at least 1 row."
                    .into(),
            ));
        }
        let mut fill_values = HashMap::new();

        for col in x.columns() {
            let name = col.name().to_string();
            if col.dtype() != &DataType::Float64 {
                continue;
            }
            let ca = col.f64().map_err(|e| {
                Error::InvalidInput(format!(
                    "SimpleImputer.fit: column '{}' has dtype {}; expected Float64. {}",
                    name,
                    col.dtype(),
                    e
                ))
            })?;
            let all_vals: Vec<f64> = ca.iter().flatten().collect();
            let has_missing = ca.iter().any(|v| v.is_none());

            if !has_missing {
                continue;
            }

            let fill = match self.strategy {
                Strategy::Mean => {
                    if all_vals.is_empty() {
                        return Err(Error::Computation(format!(
                            "SimpleImputer(Mean): column '{}' has no non-null values. \
                             Cannot compute the mean of an all-null column. \
                             Use Strategy::Constant instead.",
                            name
                        )));
                    }
                    all_vals.iter().sum::<f64>() / all_vals.len() as f64
                }
                Strategy::Median => {
                    let mut sorted = all_vals.clone();
                    sorted.sort_by(|a, b| a.total_cmp(b));
                    if sorted.is_empty() {
                        return Err(Error::Computation(format!(
                            "SimpleImputer(Median): column '{}' has no non-null values. \
                             Cannot compute the median of an all-null column. \
                             Use Strategy::Constant instead.",
                            name
                        )));
                    }
                    let mid = sorted.len() / 2;
                    if sorted.len().is_multiple_of(2) {
                        (sorted[mid - 1] + sorted[mid]) / 2.0
                    } else {
                        sorted[mid]
                    }
                }
                Strategy::MostFrequent => {
                    if all_vals.is_empty() {
                        return Err(Error::Computation(format!(
                            "SimpleImputer(MostFrequent): column '{}' has no non-null values. \
                             Cannot compute the mode of an all-null column. \
                             Use Strategy::Constant instead.",
                            name
                        )));
                    }
                    let mut freq: HashMap<u64, usize> = HashMap::new();
                    for &v in &all_vals {
                        *freq.entry(v.to_bits()).or_default() += 1;
                    }
                    let (max_key, _) = freq
                        .into_iter()
                        .max_by(|a, b| a.1.cmp(&b.1).then_with(|| b.0.cmp(&a.0)))
                        .ok_or_else(|| {
                            Error::Computation(format!(
                                "SimpleImputer(MostFrequent): column '{}' has no non-null values",
                                name
                            ))
                        })?;
                    f64::from_bits(max_key)
                }
                Strategy::Constant(v) => v,
            };

            fill_values.insert(name, fill);
        }

        self.fill_values = Some(fill_values);
        self.fitted = true;
        Ok(())
    }
}

impl Transform<DataFrame> for SimpleImputer {
    type Output = DataFrame;

    fn transform(&self, x: DataFrame) -> Result<DataFrame> {
        if !self.fitted {
            return Err(Error::NotFitted(
                "SimpleImputer has not been fitted. \
                 Call .fit(dataframe) before .transform()."
                    .into(),
            ));
        }
        let fill_values = self.fill_values.as_ref().ok_or_else(|| {
            Error::NotFitted(
                "SimpleImputer has not been fitted. \
                 Call .fit(dataframe) before .transform()."
                    .into(),
            )
        })?;
        let mut out = x.clone();

        for (name, fill) in fill_values {
            let s = out.column(name.as_str()).map_err(|e| {
                Error::InvalidInput(format!(
                    "SimpleImputer.transform: column '{}' not found. \
                     The imputer was fitted on columns: {:?}. {}",
                    name,
                    fill_values.keys().collect::<Vec<_>>(),
                    e
                ))
            })?;
            let ca = s.f64().map_err(|e| {
                Error::InvalidInput(format!(
                    "SimpleImputer.transform: column '{}' has dtype {}; expected Float64. {}",
                    name,
                    s.dtype(),
                    e
                ))
            })?;
            let filled: ChunkedArray<Float64Type> =
                ca.iter().map(|opt| opt.or(Some(*fill))).collect();
            out.replace(name.as_str(), filled.into_series().into())
                .map_err(|e| {
                    Error::Computation(format!(
                        "SimpleImputer.transform: failed to replace column '{}'. {}",
                        name, e
                    ))
                })?;
        }

        Ok(out)
    }
}

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

    fn make_test_df() -> DataFrame {
        let a = Column::from(Series::new("x".into(), &[Some(1.0f64), None, Some(3.0)]));
        let b = Column::from(Series::new("y".into(), &[Some(10.0f64), Some(20.0), None]));
        DataFrame::new(3, vec![a, b]).unwrap()
    }

    #[test]
    fn test_imputer_mean() {
        let mut imp = SimpleImputer::mean();
        let df = make_test_df();

        imp.fit(df.clone()).unwrap();
        let result = imp.transform(df).unwrap();

        let x_vals: Vec<f64> = result
            .column("x")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_relative_eq!(x_vals[1], 2.0, epsilon = 1e-6);

        let y_vals: Vec<f64> = result
            .column("y")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_relative_eq!(y_vals[2], 15.0, epsilon = 1e-6);
    }

    #[test]
    fn test_imputer_constant() {
        let mut imp = SimpleImputer::constant(0.0);
        let df = make_test_df();

        imp.fit(df.clone()).unwrap();
        let result = imp.transform(df).unwrap();

        let x_vals: Vec<f64> = result
            .column("x")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_relative_eq!(x_vals[1], 0.0, epsilon = 1e-6);
    }

    /// Regression: the Median path sorted with `partial_cmp().unwrap()`, which
    /// panicked when a NaN was present. `total_cmp` sorts NaN deterministically.
    #[test]
    fn test_imputer_median_with_nan_does_not_panic() {
        // Column `x` has a NaN among the non-null values; the median sort must
        // not panic.
        let x = Column::from(Series::new(
            "x".into(),
            &[Some(1.0f64), Some(f64::NAN), None, Some(3.0)],
        ));
        let df = DataFrame::new(4, vec![x]).unwrap();
        let mut imp = SimpleImputer::median();
        imp.fit(df.clone()).unwrap();
        let _ = imp.transform(df).unwrap();
    }

    #[test]
    fn test_imputer_median_value() {
        // Non-null values [1.0, 3.0] → median = 2.0; the null at index 2 is
        // imputed with 2.0.
        let x = Column::from(Series::new("x".into(), &[Some(1.0f64), Some(3.0), None]));
        let df = DataFrame::new(3, vec![x]).unwrap();
        let mut imp = SimpleImputer::median();
        imp.fit(df.clone()).unwrap();
        let result = imp.transform(df).unwrap();

        let vals: Vec<f64> = result
            .column("x")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_relative_eq!(vals[2], 2.0, epsilon = 1e-6);
    }

    #[test]
    fn test_imputer_most_frequent_value() {
        // Non-null values [1.0, 2.0, 2.0] → mode = 2.0; the null is imputed
        // with 2.0.
        let x = Column::from(Series::new(
            "x".into(),
            &[Some(1.0f64), Some(2.0), Some(2.0), None],
        ));
        let df = DataFrame::new(4, vec![x]).unwrap();
        let mut imp = SimpleImputer::most_frequent();
        imp.fit(df.clone()).unwrap();
        let result = imp.transform(df).unwrap();

        let vals: Vec<f64> = result
            .column("x")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_relative_eq!(vals[3], 2.0, epsilon = 1e-6);
    }

    #[test]
    fn test_imputer_most_frequent_ties() {
        // Tied frequencies: 1.0 appears 2×, 2.0 appears 2×, two nulls.
        // On ties, the smallest value (1.0) must be chosen deterministically.
        let x = Column::from(Series::new(
            "x".into(),
            &[Some(1.0f64), Some(1.0), Some(2.0), Some(2.0), None, None],
        ));
        let df = DataFrame::new(6, vec![x]).unwrap();
        let mut imp = SimpleImputer::most_frequent();
        imp.fit(df.clone()).unwrap();
        let result = imp.transform(df.clone()).unwrap();
        let vals: Vec<f64> = result
            .column("x")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_eq!(vals[4], 1.0);
        assert_eq!(vals[5], 1.0);
        // Run twice to confirm reproducibility
        let result2 = imp.transform(df).unwrap();
        let vals2: Vec<f64> = result2
            .column("x")
            .unwrap()
            .f64()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_eq!(vals, vals2);
    }

    #[test]
    fn test_imputer_all_null_column_error() {
        let x = Column::from(Series::new("x".into(), &[None::<f64>, None, None]));
        let df = DataFrame::new(3, vec![x]).unwrap();
        let mut imp = SimpleImputer::mean();
        let fit_result = imp.fit(df.clone());
        assert!(
            fit_result.is_err(),
            "fitting an all-null column with Mean must error"
        );
    }

    #[test]
    fn test_imputer_not_fitted() {
        let imp = SimpleImputer::mean();
        let x = Column::from(Series::new("x".into(), &[Some(1.0f64), None]));
        let df = DataFrame::new(2, vec![x]).unwrap();
        assert!(imp.transform(df).is_err());
    }
}