featrs 0.2.0

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Categorical encoding.
//!
//! Analogous to `sklearn.preprocessing`. Provides:
//! - [`OneHotEncoder`] — create dummy/binary columns for each category
//! - [`LabelEncoder`] — encode labels as `0..n_classes-1` integers
//! - [`OrdinalEncoder`] — encode categorical features as integer columns

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

fn column_unique_strings(col: &Column) -> Result<Vec<String>> {
    let s = col.as_materialized_series();
    let ca = s.str().map_err(|e| {
        Error::InvalidInput(format!(
            "Encoder: column '{}' has dtype {}; expected String. \
             Only string columns can be encoded. {}",
            col.name(),
            col.dtype(),
            e
        ))
    })?;
    let mut unique: Vec<String> = ca
        .iter()
        .flatten()
        .map(|s| s.to_string())
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect();
    unique.sort();
    Ok(unique)
}

/// Encode categorical features as a one-hot numeric array.
///
/// Creates a binary column for each category value. Non-string columns
/// are ignored.
///
/// # Example
///
/// ```rust
/// use featrs::preprocessing::encoder::OneHotEncoder;
/// use featrs::traits::{Fit, Transform};
///
/// let mut enc = OneHotEncoder::new().drop_first(false);
/// # let df = polars::prelude::DataFrame::new(0usize, vec![]).unwrap();
/// // enc.fit(df.clone(), target)?;
/// // let encoded = enc.transform(df)?;
/// ```
pub struct OneHotEncoder {
    fitted: bool,
    categories: Option<Vec<OneHotCategory>>,
    drop_first: bool,
}

struct OneHotCategory {
    column: String,
    categories: Vec<String>,
}

impl OneHotEncoder {
    /// Create a new `OneHotEncoder`.
    ///
    /// By default, a column is created for every category. Use
    /// [`drop_first`](Self::drop_first) to drop the first category
    /// and avoid multicollinearity.
    pub fn new() -> Self {
        Self {
            fitted: false,
            categories: None,
            drop_first: false,
        }
    }

    /// Whether to drop the first category of each feature (default: `false`).
    ///
    /// When `true`, the first category (alphabetically) is omitted from the
    /// output, producing `k-1` columns for a feature with `k` categories.
    pub fn drop_first(mut self, value: bool) -> Self {
        self.drop_first = value;
        self
    }
}

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

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

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

        for col in x.columns() {
            let name = col.name().to_string();
            let unique = column_unique_strings(col)?;

            if !unique.is_empty() {
                cats.push(OneHotCategory {
                    column: name,
                    categories: unique,
                });
            }
        }

        if cats.is_empty() {
            return Err(Error::InvalidInput(
                "OneHotEncoder.fit: no string columns found. \
                 OneHotEncoder operates on String columns only. \
                 Cast categorical columns to String first."
                    .into(),
            ));
        }

        self.categories = Some(cats);
        self.fitted = true;
        Ok(())
    }
}

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

    fn transform(&self, x: DataFrame) -> Result<DataFrame> {
        if !self.fitted {
            return Err(Error::NotFitted(
                "OneHotEncoder has not been fitted. \
                 Call .fit(dataframe, target) before .transform()."
                    .into(),
            ));
        }
        let cats = self.categories.as_ref().unwrap();
        let mut new_cols: Vec<Column> = Vec::new();
        let n_rows = x.height();

        for cat in cats {
            let s = x.column(&cat.column).map_err(|e| {
                Error::InvalidInput(format!(
                    "OneHotEncoder.transform: column '{}' not found in input. \
                     The encoder was fitted on columns: {:?}. {}",
                    cat.column,
                    cats.iter().map(|c| &c.column).collect::<Vec<_>>(),
                    e
                ))
            })?;
            let ca = s.as_materialized_series().str().map_err(|e| {
                Error::InvalidInput(format!(
                    "OneHotEncoder.transform: column '{}' has dtype {}; expected String. {}",
                    cat.column,
                    s.dtype(),
                    e
                ))
            })?;
            let start_idx = if self.drop_first { 1 } else { 0 };

            for (_j, category) in cat.categories.iter().enumerate().skip(start_idx) {
                let mut vals = vec![0.0f64; n_rows];
                for (i, opt) in ca.iter().enumerate() {
                    if let Some(v) = opt
                        && v == *category
                    {
                        vals[i] = 1.0;
                    }
                }
                let col_name = format!("{}_{}", cat.column, category);
                new_cols.push(Column::from(Series::new(col_name.as_str().into(), &vals)));
            }
        }

        DataFrame::new(n_rows, new_cols).map_err(|e| Error::Computation(e.to_string()))
    }
}

/// Encode labels as integers `0` to `n_classes - 1`.
///
/// Operates on a single string column. The mapping is sorted alphabetically.
///
/// # Example
///
/// ```rust
/// use featrs::preprocessing::encoder::LabelEncoder;
/// use featrs::traits::{Fit, Transform};
///
/// let mut enc = LabelEncoder::new();
/// # let df = polars::prelude::DataFrame::new(0usize, vec![]).unwrap();
/// // enc.fit(df.clone(), target)?;
/// // let encoded = enc.transform(df)?;
/// ```
pub struct LabelEncoder {
    fitted: bool,
    classes: Option<Vec<String>>,
    mapping: Option<HashMap<String, usize>>,
}

impl LabelEncoder {
    /// Create a new `LabelEncoder`.
    pub fn new() -> Self {
        Self {
            fitted: false,
            classes: None,
            mapping: None,
        }
    }
}

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

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

    fn fit(&mut self, x: DataFrame, _y: DataFrame) -> Result<()> {
        if x.width() != 1 {
            return Err(Error::InvalidInput(format!(
                "LabelEncoder.fit expects a single column but got {} columns. \
                 Select one column before calling fit, e.g. df.select(['target']).",
                x.width()
            )));
        }
        let col = &x.columns()[0];
        let classes = column_unique_strings(col)?;

        if classes.is_empty() {
            return Err(Error::InvalidInput(format!(
                "LabelEncoder.fit: column '{}' contains no unique values. \
                 Provide data with at least one non-null string.",
                col.name()
            )));
        }

        let mapping: HashMap<String, usize> = classes
            .iter()
            .enumerate()
            .map(|(i, c)| (c.clone(), i))
            .collect();

        self.classes = Some(classes);
        self.mapping = Some(mapping);
        self.fitted = true;
        Ok(())
    }
}

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

    fn transform(&self, x: DataFrame) -> Result<DataFrame> {
        if !self.fitted {
            return Err(Error::NotFitted(
                "LabelEncoder has not been fitted. \
                 Call .fit(dataframe, target) before .transform()."
                    .into(),
            ));
        }
        let mapping = self.mapping.as_ref().unwrap();
        let s = x.columns()[0].as_materialized_series();
        let ca = s.str().map_err(|e| {
            Error::InvalidInput(format!(
                "LabelEncoder.transform: column '{}' has dtype {}; expected String. {}",
                s.name(),
                s.dtype(),
                e
            ))
        })?;

        let encoded: ChunkedArray<UInt32Type> = ca
            .iter()
            .map(|opt| opt.and_then(|v| mapping.get(v).copied().map(|x| x as u32)))
            .collect();

        let mut series = encoded.into_series();
        series.rename(s.name().clone());
        DataFrame::new(x.height(), vec![Column::from(series)])
            .map_err(|e| Error::Computation(e.to_string()))
    }
}

/// Encode categorical features as integer columns.
///
/// Similar to [`LabelEncoder`] but operates on multiple columns at once.
/// Each column receives its own `0..n_categories` mapping.
///
/// # Example
///
/// ```rust
/// use featrs::preprocessing::encoder::OrdinalEncoder;
/// use featrs::traits::{Fit, Transform};
///
/// let mut enc = OrdinalEncoder::new();
/// # let df = polars::prelude::DataFrame::new(0usize, vec![]).unwrap();
/// // enc.fit(df.clone(), target)?;
/// // let encoded = enc.transform(df)?;
/// ```
pub struct OrdinalEncoder {
    fitted: bool,
    categories: Option<Vec<(String, HashMap<String, u32>)>>,
}

impl OrdinalEncoder {
    /// Create a new `OrdinalEncoder`.
    pub fn new() -> Self {
        Self {
            fitted: false,
            categories: None,
        }
    }
}

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

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

    fn fit(&mut self, x: DataFrame, _y: DataFrame) -> Result<()> {
        if x.width() == 0 {
            return Err(Error::InvalidInput(
                "OrdinalEncoder.fit received a DataFrame with 0 columns. \
                 Provide at least one string column to encode."
                    .into(),
            ));
        }
        let mut cats = Vec::new();

        for col in x.columns() {
            let name = col.name().to_string();
            let classes = column_unique_strings(col)?;

            if classes.is_empty() {
                continue;
            }

            let mapping: HashMap<String, u32> = classes
                .iter()
                .enumerate()
                .map(|(i, c)| (c.clone(), i as u32))
                .collect();

            cats.push((name, mapping));
        }

        if cats.is_empty() {
            return Err(Error::InvalidInput(
                "OrdinalEncoder.fit: no string columns found. \
                 OrdinalEncoder operates on String columns only."
                    .into(),
            ));
        }

        self.categories = Some(cats);
        self.fitted = true;
        Ok(())
    }
}

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

    fn transform(&self, x: DataFrame) -> Result<DataFrame> {
        if !self.fitted {
            return Err(Error::NotFitted(
                "OrdinalEncoder has not been fitted. \
                 Call .fit(dataframe, target) before .transform()."
                    .into(),
            ));
        }
        let mut out_cols = Vec::new();

        for (name, mapping) in self.categories.as_ref().unwrap() {
            let s = x.column(name.as_str()).map_err(|e| {
                Error::InvalidInput(format!(
                    "OrdinalEncoder.transform: column '{}' not found. \
                     The encoder was fitted on columns: {:?}. {}",
                    name,
                    self.categories
                        .as_ref()
                        .unwrap()
                        .iter()
                        .map(|(n, _)| n)
                        .collect::<Vec<_>>(),
                    e
                ))
            })?;
            let ca = s.as_materialized_series().str().map_err(|e| {
                Error::InvalidInput(format!(
                    "OrdinalEncoder.transform: column '{}' has dtype {}; expected String. {}",
                    name,
                    s.dtype(),
                    e
                ))
            })?;

            let encoded: ChunkedArray<UInt32Type> = ca
                .iter()
                .map(|opt| opt.and_then(|v| mapping.get(v).copied()))
                .collect();

            let mut series = encoded.into_series();
            series.rename(name.as_str().into());
            out_cols.push(Column::from(series));
        }

        DataFrame::new(x.height(), out_cols).map_err(|e| Error::Computation(e.to_string()))
    }
}

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

    fn make_categorical_df() -> DataFrame {
        let a = Column::from(Series::new(
            "color".into(),
            &["red", "blue", "red", "green"],
        ));
        let b = Column::from(Series::new("size".into(), &["S", "M", "L", "M"]));
        DataFrame::new(4, vec![a, b]).unwrap()
    }

    #[test]
    fn test_one_hot_encoder() {
        let mut enc = OneHotEncoder::new();
        let df = make_categorical_df();
        let y = df.clone();

        enc.fit(df.clone(), y).unwrap();
        let result = enc.transform(df).unwrap();

        assert_eq!(result.width(), 6);
        assert_eq!(result.height(), 4);
    }

    #[test]
    fn test_one_hot_encoder_drop_first() {
        let mut enc = OneHotEncoder::new().drop_first(true);
        let df = make_categorical_df();
        let y = df.clone();

        enc.fit(df.clone(), y).unwrap();
        let result = enc.transform(df).unwrap();

        assert_eq!(result.width(), 4);
    }

    #[test]
    fn test_label_encoder() {
        let mut enc = LabelEncoder::new();
        let colors = Column::from(Series::new(
            "color".into(),
            &["red", "blue", "red", "green"],
        ));
        let df = DataFrame::new(4, vec![colors]).unwrap();
        let y = df.clone();

        enc.fit(df.clone(), y).unwrap();
        let result = enc.transform(df).unwrap();

        let vals: Vec<u32> = result
            .column("color")
            .unwrap()
            .u32()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_eq!(vals, vec![2, 0, 2, 1]);
    }

    #[test]
    fn test_ordinal_encoder() {
        let mut enc = OrdinalEncoder::new();
        let df = make_categorical_df();
        let y = df.clone();

        enc.fit(df.clone(), y).unwrap();
        let result = enc.transform(df).unwrap();

        let color_vals: Vec<u32> = result
            .column("color")
            .unwrap()
            .u32()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_eq!(color_vals, vec![2, 0, 2, 1]);

        let size_vals: Vec<u32> = result
            .column("size")
            .unwrap()
            .u32()
            .unwrap()
            .iter()
            .flatten()
            .collect();
        assert_eq!(size_vals, vec![2, 1, 0, 1]);
    }
}