Clippy fixes
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::math::num::RealNumber;
|
|||||||
pub type CategoricalFloat = u16;
|
pub type CategoricalFloat = u16;
|
||||||
|
|
||||||
// pub struct CategoricalFloat(u16);
|
// pub struct CategoricalFloat(u16);
|
||||||
|
const ERROR_MARGIN: f64 = 0.001;
|
||||||
|
|
||||||
pub trait Categorizable: RealNumber {
|
pub trait Categorizable: RealNumber {
|
||||||
type A;
|
type A;
|
||||||
@@ -13,11 +14,9 @@ pub trait Categorizable: RealNumber {
|
|||||||
fn to_category(self) -> CategoricalFloat;
|
fn to_category(self) -> CategoricalFloat;
|
||||||
|
|
||||||
fn is_valid(self) -> bool;
|
fn is_valid(self) -> bool;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Categorizable for f32 {
|
impl Categorizable for f32 {
|
||||||
|
|
||||||
type A = CategoricalFloat;
|
type A = CategoricalFloat;
|
||||||
|
|
||||||
fn to_category(self) -> CategoricalFloat {
|
fn to_category(self) -> CategoricalFloat {
|
||||||
@@ -26,20 +25,19 @@ impl Categorizable for f32 {
|
|||||||
|
|
||||||
fn is_valid(self) -> bool {
|
fn is_valid(self) -> bool {
|
||||||
let a = self.to_category();
|
let a = self.to_category();
|
||||||
a as f32 == self
|
(a as f32 - self).abs() < (ERROR_MARGIN as f32)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Categorizable for f64 {
|
impl Categorizable for f64 {
|
||||||
|
|
||||||
type A = CategoricalFloat;
|
type A = CategoricalFloat;
|
||||||
|
|
||||||
fn to_category(self) ->CategoricalFloat {
|
fn to_category(self) -> CategoricalFloat {
|
||||||
self as CategoricalFloat
|
self as CategoricalFloat
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid(self) -> bool {
|
fn is_valid(self) -> bool {
|
||||||
let a = self.to_category();
|
let a = self.to_category();
|
||||||
a as f64 == self
|
(a as f64 - self).abs() < ERROR_MARGIN
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/// Transform a data matrix by replaceing all categorical variables with their one-hot vector equivalents
|
/// 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;
|
mod data_traits;
|
||||||
/// Encode a series (column, array) of categorical variables as one-hot vectors
|
/// Encode a series (column, array) of categorical variables as one-hot vectors
|
||||||
pub mod series_encoder;
|
pub mod series_encoder;
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ pub fn make_one_hot<T: RealNumber, V: BaseVector<T>>(
|
|||||||
/// println!("{:?}", enc_lv.get_categories());
|
/// println!("{:?}", enc_lv.get_categories());
|
||||||
/// assert_eq!(enc_lv.transform_one::<f64>(&"dog"), enc_lm.transform_one::<f64>(&"dog"))
|
/// assert_eq!(enc_lv.transform_one::<f64>(&"dog"), enc_lm.transform_one::<f64>(&"dog"))
|
||||||
/// ```
|
/// ```
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct SeriesOneHotEncoder<CategoryType> {
|
pub struct SeriesOneHotEncoder<CategoryType> {
|
||||||
category_map: HashMap<CategoryType, usize>,
|
category_map: HashMap<CategoryType, usize>,
|
||||||
categories: Vec<CategoryType>,
|
categories: Vec<CategoryType>,
|
||||||
@@ -134,7 +135,7 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> SeriesOneHotEncoder<CategoryType>
|
|||||||
&self,
|
&self,
|
||||||
categories: &'a [CategoryType],
|
categories: &'a [CategoryType],
|
||||||
) -> Vec<Option<Vec<U>>> {
|
) -> Vec<Option<Vec<U>>> {
|
||||||
let v = categories.iter().map(|a| a.clone());
|
let v = categories.iter().cloned();
|
||||||
self.transform_iter(v)
|
self.transform_iter(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user