diff --git a/src/preprocessing/data_traits.rs b/src/preprocessing/data_traits.rs index 16924bb..38d9e3e 100644 --- a/src/preprocessing/data_traits.rs +++ b/src/preprocessing/data_traits.rs @@ -1,11 +1,12 @@ //! Traits to indicate that float variables can be viewed as categorical -//! This module assumes +//! This module assumes use crate::math::num::RealNumber; pub type CategoricalFloat = u16; // pub struct CategoricalFloat(u16); +const ERROR_MARGIN: f64 = 0.001; pub trait Categorizable: RealNumber { type A; @@ -13,11 +14,9 @@ pub trait Categorizable: RealNumber { fn to_category(self) -> CategoricalFloat; fn is_valid(self) -> bool; - } impl Categorizable for f32 { - type A = CategoricalFloat; fn to_category(self) -> CategoricalFloat { @@ -26,20 +25,19 @@ impl Categorizable for f32 { fn is_valid(self) -> bool { let a = self.to_category(); - a as f32 == self + (a as f32 - self).abs() < (ERROR_MARGIN as f32) } } impl Categorizable for f64 { - type A = CategoricalFloat; - fn to_category(self) ->CategoricalFloat { + fn to_category(self) -> CategoricalFloat { self as CategoricalFloat } fn is_valid(self) -> bool { let a = self.to_category(); - a as f64 == self + (a as f64 - self).abs() < ERROR_MARGIN } -} \ No newline at end of file +} diff --git a/src/preprocessing/mod.rs b/src/preprocessing/mod.rs index c07b982..4a1abf3 100644 --- a/src/preprocessing/mod.rs +++ b/src/preprocessing/mod.rs @@ -1,5 +1,5 @@ /// Transform a data matrix by replaceing all categorical variables with their one-hot vector equivalents -pub mod categorical_encoders; +pub mod categorical_encoder; mod data_traits; /// Encode a series (column, array) of categorical variables as one-hot vectors pub mod series_encoder; diff --git a/src/preprocessing/series_encoder.rs b/src/preprocessing/series_encoder.rs index 321f049..438d678 100644 --- a/src/preprocessing/series_encoder.rs +++ b/src/preprocessing/series_encoder.rs @@ -65,6 +65,7 @@ pub fn make_one_hot>( /// println!("{:?}", enc_lv.get_categories()); /// assert_eq!(enc_lv.transform_one::(&"dog"), enc_lm.transform_one::(&"dog")) /// ``` +#[derive(Debug, Clone)] pub struct SeriesOneHotEncoder { category_map: HashMap, categories: Vec, @@ -134,7 +135,7 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> SeriesOneHotEncoder &self, categories: &'a [CategoryType], ) -> Vec>> { - let v = categories.iter().map(|a| a.clone()); + let v = categories.iter().cloned(); self.transform_iter(v) }