doc update
This commit is contained in:
@@ -8,6 +8,7 @@ use crate::math::num::RealNumber;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
|
||||
/// Bi-directional map category <-> label num.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CategoryMapper<CategoryType> {
|
||||
category_map: HashMap<CategoryType, usize>,
|
||||
@@ -16,7 +17,9 @@ pub struct 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_num = 0usize;
|
||||
let mut unique_lables: Vec<CategoryType> = Vec::new();
|
||||
@@ -35,7 +38,8 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
||||
}
|
||||
}
|
||||
|
||||
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)> =
|
||||
category_map.iter().map(|(k, v)| (k.clone(), *v)).collect();
|
||||
_unique_cat.sort_by(|a, b| a.1.cmp(&b.1));
|
||||
@@ -47,7 +51,8 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -61,16 +66,17 @@ impl<'a, CategoryType: 'a + Hash + Eq + Clone> CategoryMapper<CategoryType> {
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Return category corresponding to label num
|
||||
fn get_cat(&self, num: usize) -> &CategoryType {
|
||||
pub fn get_cat(&self, num: usize) -> &CategoryType {
|
||||
&self.categories[num]
|
||||
}
|
||||
|
||||
fn get_categories(&self) -> &[CategoryType] {
|
||||
/// List all categories (position = category number)
|
||||
pub fn get_categories(&self) -> &[CategoryType] {
|
||||
&self.categories[..]
|
||||
}
|
||||
}
|
||||
@@ -80,7 +86,7 @@ pub trait SeriesEncoder<CategoryType>:
|
||||
where
|
||||
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;
|
||||
|
||||
/// Number of categories for categorical variable
|
||||
@@ -119,6 +125,7 @@ pub trait SeriesEncoder<CategoryType>:
|
||||
self.transform_iter(v)
|
||||
}
|
||||
}
|
||||
|
||||
/// Make a one-hot encoded vector from a categorical variable
|
||||
///
|
||||
/// Example:
|
||||
@@ -233,6 +240,7 @@ impl<CategoryType: Hash + Eq + Clone> SeriesEncoder<CategoryType> for SeriesOneH
|
||||
Some(&idx) => Some(make_one_hot(idx, self.num_categories())),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user