doc update
This commit is contained in:
@@ -8,6 +8,7 @@ use crate::math::num::RealNumber;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
|
/// Bi-directional map category <-> label num.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CategoryMapper<CategoryType> {
|
pub struct CategoryMapper<CategoryType> {
|
||||||
category_map: HashMap<CategoryType, usize>,
|
category_map: HashMap<CategoryType, usize>,
|
||||||
@@ -16,7 +17,9 @@ pub struct CategoryMapper<CategoryType> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
||||||
fn fit_to_iter(categories: impl Iterator<Item = CategoryType>) -> Self {
|
|
||||||
|
/// Fit an encoder to a lable iterator
|
||||||
|
pub fn fit_to_iter(categories: impl Iterator<Item = CategoryType>) -> Self {
|
||||||
let mut category_map: HashMap<CategoryType, usize> = HashMap::new();
|
let mut category_map: HashMap<CategoryType, usize> = HashMap::new();
|
||||||
let mut category_num = 0usize;
|
let mut category_num = 0usize;
|
||||||
let mut unique_lables: Vec<CategoryType> = Vec::new();
|
let mut unique_lables: Vec<CategoryType> = Vec::new();
|
||||||
@@ -34,8 +37,9 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
|||||||
categories: unique_lables,
|
categories: unique_lables,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_category_map(category_map: HashMap<CategoryType, usize>) -> Self {
|
/// Build an encoder from a predefined (category -> class number) map
|
||||||
|
pub fn from_category_map(category_map: HashMap<CategoryType, usize>) -> Self {
|
||||||
let mut _unique_cat: Vec<(CategoryType, usize)> =
|
let mut _unique_cat: Vec<(CategoryType, usize)> =
|
||||||
category_map.iter().map(|(k, v)| (k.clone(), *v)).collect();
|
category_map.iter().map(|(k, v)| (k.clone(), *v)).collect();
|
||||||
_unique_cat.sort_by(|a, b| a.1.cmp(&b.1));
|
_unique_cat.sort_by(|a, b| a.1.cmp(&b.1));
|
||||||
@@ -46,8 +50,9 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
|||||||
category_map,
|
category_map,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_positional_category_vec(categories: Vec<CategoryType>) -> Self {
|
/// Build an encoder from a predefined positional category-class num vector
|
||||||
|
pub fn from_positional_category_vec(categories: Vec<CategoryType>) -> Self {
|
||||||
let category_map: HashMap<CategoryType, usize> = categories
|
let category_map: HashMap<CategoryType, usize> = categories
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@@ -61,16 +66,17 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get label num of a category
|
/// Get label num of a category
|
||||||
fn get_num(&self, category: &CategoryType) -> Option<&usize> {
|
pub fn get_num(&self, category: &CategoryType) -> Option<&usize> {
|
||||||
self.category_map.get(category)
|
self.category_map.get(category)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return category corresponding to label num
|
/// Return category corresponding to label num
|
||||||
fn get_cat(&self, num: usize) -> &CategoryType {
|
pub fn get_cat(&self, num: usize) -> &CategoryType {
|
||||||
&self.categories[num]
|
&self.categories[num]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_categories(&self) -> &[CategoryType] {
|
/// List all categories (position = category number)
|
||||||
|
pub fn get_categories(&self) -> &[CategoryType] {
|
||||||
&self.categories[..]
|
&self.categories[..]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,14 +86,14 @@ pub trait SeriesEncoder<CategoryType>:
|
|||||||
where
|
where
|
||||||
CategoryType:Hash + Eq + Clone
|
CategoryType:Hash + Eq + Clone
|
||||||
{
|
{
|
||||||
/// Fit an encoder to a lable list
|
/// Fit an encoder to a lable iterator
|
||||||
fn fit_to_iter(categories: impl Iterator<Item = CategoryType>) -> Self;
|
fn fit_to_iter(categories: impl Iterator<Item = CategoryType>) -> Self;
|
||||||
|
|
||||||
/// Number of categories for categorical variable
|
/// Number of categories for categorical variable
|
||||||
fn num_categories(&self) -> usize;
|
fn num_categories(&self) -> usize;
|
||||||
|
|
||||||
/// Build an encoder from a predefined (category -> class number) map
|
/// Build an encoder from a predefined (category -> class number) map
|
||||||
fn from_category_map(category_map: HashMap<CategoryType, usize>) -> Self;
|
fn from_category_map(category_map: HashMap<CategoryType, usize>) -> Self;
|
||||||
|
|
||||||
/// Build an encoder from a predefined positional category-class num vector
|
/// Build an encoder from a predefined positional category-class num vector
|
||||||
fn from_positional_category_vec(categories: Vec<CategoryType>) -> Self;
|
fn from_positional_category_vec(categories: Vec<CategoryType>) -> Self;
|
||||||
@@ -119,6 +125,7 @@ pub trait SeriesEncoder<CategoryType>:
|
|||||||
self.transform_iter(v)
|
self.transform_iter(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make a one-hot encoded vector from a categorical variable
|
/// Make a one-hot encoded vector from a categorical variable
|
||||||
///
|
///
|
||||||
/// Example:
|
/// Example:
|
||||||
@@ -182,20 +189,20 @@ pub struct SeriesOneHotEncoder<CategoryType> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<CategoryType: Hash + Eq + Clone> SeriesEncoder<CategoryType> for SeriesOneHotEncoder<CategoryType> {
|
impl<CategoryType: Hash + Eq + Clone> SeriesEncoder<CategoryType> for SeriesOneHotEncoder<CategoryType> {
|
||||||
|
|
||||||
fn fit_to_iter(categories: impl Iterator<Item = CategoryType>) -> Self {
|
fn fit_to_iter(categories: impl Iterator<Item = CategoryType>) -> Self {
|
||||||
Self {mapper:CategoryMapper::fit_to_iter(categories)}
|
Self {mapper:CategoryMapper::fit_to_iter(categories)}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build an encoder from a predefined (category -> class number) map
|
/// Build an encoder from a predefined (category -> class number) map
|
||||||
fn from_category_map(category_map: HashMap<CategoryType, usize>) -> Self {
|
fn from_category_map(category_map: HashMap<CategoryType, usize>) -> Self {
|
||||||
Self {mapper: CategoryMapper::from_category_map(category_map)}
|
Self {mapper: CategoryMapper::from_category_map(category_map)}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build an encoder from a predefined positional category-class num vector
|
/// Build an encoder from a predefined positional category-class num vector
|
||||||
fn from_positional_category_vec(categories: Vec<CategoryType>) -> Self {
|
fn from_positional_category_vec(categories: Vec<CategoryType>) -> Self {
|
||||||
Self {mapper:CategoryMapper::from_positional_category_vec(categories)}
|
Self {mapper:CategoryMapper::from_positional_category_vec(categories)}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn num_categories(&self) -> usize {
|
fn num_categories(&self) -> usize {
|
||||||
self.mapper.num_categories
|
self.mapper.num_categories
|
||||||
@@ -207,25 +214,25 @@ impl<CategoryType: Hash + Eq + Clone> SeriesEncoder<CategoryType> for SeriesOneH
|
|||||||
|
|
||||||
fn invert_one<U: RealNumber, V: BaseVector<U>>(&self, one_hot: V) -> Result<CategoryType, Failed>
|
fn invert_one<U: RealNumber, V: BaseVector<U>>(&self, one_hot: V) -> Result<CategoryType, Failed>
|
||||||
{
|
{
|
||||||
let pos = U::from_f64(1f64).unwrap();
|
let pos = U::from_f64(1f64).unwrap();
|
||||||
|
|
||||||
let oh_it = (0..one_hot.len()).map(|idx| one_hot.get(idx));
|
let oh_it = (0..one_hot.len()).map(|idx| one_hot.get(idx));
|
||||||
|
|
||||||
let s: Vec<usize> = oh_it
|
let s: Vec<usize> = oh_it
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(idx, v)| if v == pos { Some(idx) } else { None })
|
.filter_map(|(idx, v)| if v == pos { Some(idx) } else { None })
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if s.len() == 1 {
|
if s.len() == 1 {
|
||||||
let idx = s[0];
|
let idx = s[0];
|
||||||
return Ok(self.mapper.get_cat(idx).clone());
|
return Ok(self.mapper.get_cat(idx).clone());
|
||||||
|
}
|
||||||
|
let pos_entries = format!(
|
||||||
|
"Expected a single positive entry, {} entires found",
|
||||||
|
s.len()
|
||||||
|
);
|
||||||
|
Err(Failed::transform(&pos_entries[..]))
|
||||||
}
|
}
|
||||||
let pos_entries = format!(
|
|
||||||
"Expected a single positive entry, {} entires found",
|
|
||||||
s.len()
|
|
||||||
);
|
|
||||||
Err(Failed::transform(&pos_entries[..]))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transform_one<U: RealNumber, V: BaseVector<U>>(&self, category: &CategoryType) -> Option<V> {
|
fn transform_one<U: RealNumber, V: BaseVector<U>>(&self, category: &CategoryType) -> Option<V> {
|
||||||
match self.mapper.get_num(category) {
|
match self.mapper.get_num(category) {
|
||||||
@@ -233,6 +240,7 @@ impl<CategoryType: Hash + Eq + Clone> SeriesEncoder<CategoryType> for SeriesOneH
|
|||||||
Some(&idx) => Some(make_one_hot(idx, self.num_categories())),
|
Some(&idx) => Some(make_one_hot(idx, self.num_categories())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user