tests + force Categorizable be RealNumber

This commit is contained in:
gaxler
2021-01-30 19:31:09 -08:00
parent fd6b2e8014
commit c987d39d43
2 changed files with 114 additions and 28 deletions
+110 -26
View File
@@ -1,6 +1,8 @@
//! # One-hot Encoding For [RealNumber](../../math/num/trait.RealNumber.html) Matricies //! # One-hot Encoding For [RealNumber](../../math/num/trait.RealNumber.html) Matricies
//! Transform a data [Matrix](../../linalg/trait.BaseMatrix.html) by replacing all categorical variables with their one-hot equivalents //! Transform a data [Matrix](../../linalg/trait.BaseMatrix.html) by replacing all categorical variables with their one-hot equivalents
//! //!
//! Internally OneHotEncoder treats every categorical column as a series and transforms it using [SeriesOneHotEncoder](../series_encoder/struct.SeriesOneHotEncoder.html)
//!
//! ### Usage Example //! ### Usage Example
//! ``` //! ```
//! use smartcore::linalg::naive::dense_matrix::DenseMatrix; //! use smartcore::linalg::naive::dense_matrix::DenseMatrix;
@@ -22,25 +24,33 @@
//! // &[1.5, 1.0, 0.0, 1.5, 0.0, 0.0, 1.0, 0.0] //! // &[1.5, 1.0, 0.0, 1.5, 0.0, 0.0, 1.0, 0.0]
//! // &[1.5, 0.0, 1.0, 1.5, 0.0, 0.0, 0.0, 1.0] //! // &[1.5, 0.0, 1.0, 1.5, 0.0, 0.0, 0.0, 1.0]
//! ``` //! ```
use std::iter;
use crate::error::Failed; use crate::error::Failed;
use crate::linalg::{BaseVector, Matrix}; use crate::linalg::Matrix;
use crate::math::num::RealNumber;
use crate::preprocessing::data_traits::{CategoricalFloat, Categorizable};
use crate::preprocessing::series_encoder::SeriesOneHotEncoder; use crate::preprocessing::series_encoder::SeriesOneHotEncoder;
pub type HashableReal = u32; /// OneHotEncoder Parameters
fn hashable_num<T: RealNumber>(v: &T) -> HashableReal {
// gaxler: If first 32 bits are the same, assume numbers are the same for the categorical coercion
v.to_f32_bits()
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct OneHotEncoderParams { pub struct OneHotEncoderParams {
pub categorical_param_idxs: Option<Vec<usize>>, /// Column number that contain categorical variable
pub col_idx_categorical: Option<Vec<usize>>,
/// (Currently not implemented) Try and infer which of the matrix columns are categorical variables
pub infer_categorical: bool, pub infer_categorical: bool,
} }
impl OneHotEncoderParams {
/// Generate parameters from categorical variable column numbers
pub fn from_cat_idx(categorical_params: &[usize]) -> Self {
Self {
col_idx_categorical: Some(categorical_params.to_vec()),
infer_categorical: false,
}
}
}
/// Calculate the offset to parameters to due introduction of one-hot encoding /// Calculate the offset to parameters to due introduction of one-hot encoding
fn find_new_idxs(num_params: usize, cat_sizes: &[usize], encoded_idxs: &[usize]) -> Vec<usize> { fn find_new_idxs(num_params: usize, cat_sizes: &[usize], encoded_idxs: &[usize]) -> Vec<usize> {
// This functions uses iterators and returns a vector. // This functions uses iterators and returns a vector.
@@ -75,12 +85,14 @@ fn find_new_idxs(num_params: usize, cat_sizes: &[usize], encoded_idxs: &[usize])
.collect(); .collect();
new_param_idxs new_param_idxs
} }
fn validate_col_is_categorical<T: Categorizable>(data: &Vec<T>) -> bool { fn validate_col_is_categorical<T: Categorizable>(data: &Vec<T>) -> bool {
for v in data { for v in data {
if !v.is_valid() { return false} if !v.is_valid() { return false}
} }
true true
} }
/// Encode Categorical variavbles of data matrix to one-hot /// Encode Categorical variavbles of data matrix to one-hot
pub struct OneHotEncoder { pub struct OneHotEncoder {
series_encoders: Vec<SeriesOneHotEncoder<CategoricalFloat>>, series_encoders: Vec<SeriesOneHotEncoder<CategoricalFloat>>,
@@ -167,13 +179,13 @@ impl OneHotEncoder {
// Bad value in a series causes in to be invalid // Bad value in a series causes in to be invalid
// todo: proper error handling, so user can know where the bad value is // todo: proper error handling, so user can know where the bad value is
return None; return None;
} }
Some(v) => { Some(v) => {
// copy one hot vectors to their place in the data matrix; // copy one hot vectors to their place in the data matrix;
for (col_ofst, &val) in v.iter().enumerate() { for (col_ofst, &val) in v.iter().enumerate() {
res.set(row, cidx + col_ofst, val); res.set(row, cidx + col_ofst, val);
} }
} }
} }
} }
} }
@@ -181,21 +193,93 @@ impl OneHotEncoder {
} }
} }
fn build_series_encoders(data: &M, idxs: &[usize]) -> Vec<SeriesOneHotEncoder<HashableReal>> { #[cfg(test)]
let (nrows, _) = data.shape(); mod tests {
// let mut res: Vec<SeriesOneHotEncoder<HashableReal>> = Vec::with_capacity(idxs.len()); use super::*;
let mut tmp_col: Vec<T> = Vec::with_capacity(nrows); use crate::linalg::naive::dense_matrix::DenseMatrix;
use crate::preprocessing::series_encoder::SeriesOneHotEncoder;
let res: Vec<SeriesOneHotEncoder<HashableReal>> = idxs #[test]
.iter() fn adjust_idxs() {
.map(|&idx| { assert_eq!(find_new_idxs(0, &[], &[]), Vec::new());
data.copy_col_as_vec(idx, &mut tmp_col); // [0,1,2] -> [0, 1, 1, 1, 2]
let hashable_col = tmp_col.iter().map(|v| hashable_num::<T>(v)); assert_eq!(find_new_idxs(3, &[3], &[1]), vec![0, 1, 4]);
SeriesOneHotEncoder::fit_to_iter(hashable_col)
})
.collect();
res
} }
fn build_cat_first_and_last() -> (DenseMatrix<f64>, DenseMatrix<f64>) {
let orig = DenseMatrix::from_2d_array(&[
&[1.0, 1.5, 3.0],
&[2.0, 1.5, 4.0],
&[1.0, 1.5, 5.0],
&[2.0, 1.5, 6.0],
]);
let oh_enc = DenseMatrix::from_2d_array(&[
&[1.0, 0.0, 1.5, 1.0, 0.0, 0.0, 0.0],
&[0.0, 1.0, 1.5, 0.0, 1.0, 0.0, 0.0],
&[1.0, 0.0, 1.5, 0.0, 0.0, 1.0, 0.0],
&[0.0, 1.0, 1.5, 0.0, 0.0, 0.0, 1.0],
]);
(orig, oh_enc)
}
fn build_fake_matrix() -> (DenseMatrix<f64>, DenseMatrix<f64>) {
// Categorical first and last
let orig = DenseMatrix::from_2d_array(&[
&[1.5, 1.0, 1.5, 3.0],
&[1.5, 2.0, 1.5, 4.0],
&[1.5, 1.0, 1.5, 5.0],
&[1.5, 2.0, 1.5, 6.0],
]);
let oh_enc = DenseMatrix::from_2d_array(&[
&[1.5, 1.0, 0.0, 1.5, 1.0, 0.0, 0.0, 0.0],
&[1.5, 0.0, 1.0, 1.5, 0.0, 1.0, 0.0, 0.0],
&[1.5, 1.0, 0.0, 1.5, 0.0, 0.0, 1.0, 0.0],
&[1.5, 0.0, 1.0, 1.5, 0.0, 0.0, 0.0, 1.0],
]);
(orig, oh_enc)
}
#[test]
fn hash_encode_f64_series() {
let series = vec![3.0, 1.0, 2.0, 1.0];
let hashable_series: Vec<CategoricalFloat> =
series.iter().map(|v| v.to_category()).collect();
let enc = SeriesOneHotEncoder::from_positional_category_vec(hashable_series);
let inv = enc.invert_one(vec![0.0, 0.0, 1.0]);
let orig_val: f64 = inv.unwrap().into();
assert_eq!(orig_val, 2.0);
}
#[test]
fn test_fit() {
let (X, _) = build_fake_matrix();
let params = OneHotEncoderParams::from_cat_idx(&[1, 3]);
let oh_enc = OneHotEncoder::fit(&X, params).unwrap();
assert_eq!(oh_enc.series_encoders.len(), 2);
let num_cat: Vec<usize> = oh_enc
.series_encoders
.iter()
.map(|a| a.num_categories)
.collect();
assert_eq!(num_cat, vec![2, 4]);
}
#[test]
fn matrix_transform_test() {
let (X, expectedX) = build_fake_matrix();
let params = OneHotEncoderParams::from_cat_idx(&[1, 3]);
let oh_enc = OneHotEncoder::fit(&X, params).unwrap();
let nm = oh_enc.transform(&X).unwrap();
assert_eq!(nm, expectedX);
let (X, expectedX) = build_cat_first_and_last();
let params = OneHotEncoderParams::from_cat_idx(&[0, 2]);
let oh_enc = OneHotEncoder::fit(&X, params).unwrap();
let nm = oh_enc.transform(&X).unwrap();
assert_eq!(nm, expectedX);
}
} }
+3 -1
View File
@@ -1,11 +1,13 @@
//! Traits to indicate that float variables can be viewed as categorical //! 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 type CategoricalFloat = u16;
// pub struct CategoricalFloat(u16); // pub struct CategoricalFloat(u16);
pub trait Categorizable { pub trait Categorizable: RealNumber {
type A; type A;
fn to_category(self) -> CategoricalFloat; fn to_category(self) -> CategoricalFloat;