pub struct CategoryMapper<C> { /* private fields */ }
Expand description
§Bi-directional map category <-> label num.
Turn Hashable objects into a one-hot vectors or ordinal values. This struct encodes single class per exmample
You can fit_to_iter a category enumeration by passing an iterator of categories. category numbers will be assigned in the order they are encountered
Example:
use std::collections::HashMap;
use smartcore::preprocessing::series_encoder::CategoryMapper;
let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4];
let it = fake_categories.iter().map(|&a| a);
let enc = CategoryMapper::<usize>::fit_to_iter(it);
let oh_vec: Vec<f64> = enc.get_one_hot(&1).unwrap();
// notice that 1 is actually a zero-th positional category
assert_eq!(oh_vec, vec![1.0, 0.0, 0.0, 0.0, 0.0]);
You can also pass a predefined category enumeration such as a hashmap HashMap<C, usize>
or a vector Vec<C>
use std::collections::HashMap;
use smartcore::preprocessing::series_encoder::CategoryMapper;
let category_map: HashMap<&str, usize> =
vec![("cat", 2), ("background",0), ("dog", 1)]
.into_iter()
.collect();
let category_vec = vec!["background", "dog", "cat"];
let enc_lv = CategoryMapper::<&str>::from_positional_category_vec(category_vec);
let enc_lm = CategoryMapper::<&str>::from_category_map(category_map);
// ["background", "dog", "cat"]
println!("{:?}", enc_lv.get_categories());
let lv: Vec<f64> = enc_lv.get_one_hot(&"dog").unwrap();
let lm: Vec<f64> = enc_lm.get_one_hot(&"dog").unwrap();
assert_eq!(lv, lm);
Implementations§
Source§impl<C> CategoryMapper<C>
impl<C> CategoryMapper<C>
Sourcepub fn num_categories(&self) -> usize
pub fn num_categories(&self) -> usize
Get the number of categories in the mapper
Sourcepub fn fit_to_iter(categories: impl Iterator<Item = C>) -> Self
pub fn fit_to_iter(categories: impl Iterator<Item = C>) -> Self
Fit an encoder to a lable iterator
Sourcepub fn from_category_map(category_map: HashMap<C, usize>) -> Self
pub fn from_category_map(category_map: HashMap<C, usize>) -> Self
Build an encoder from a predefined (category -> class number) map
Sourcepub fn from_positional_category_vec(categories: Vec<C>) -> Self
pub fn from_positional_category_vec(categories: Vec<C>) -> Self
Build an encoder from a predefined positional category-class num vector
Sourcepub fn get_categories(&self) -> &[C]
pub fn get_categories(&self) -> &[C]
List all categories (position = category number)
Sourcepub fn get_one_hot<U, V>(&self, category: &C) -> Option<V>where
U: RealNumber,
V: Array1<U>,
pub fn get_one_hot<U, V>(&self, category: &C) -> Option<V>where
U: RealNumber,
V: Array1<U>,
Get one-hot encoding of the category
Sourcepub fn invert_one_hot<U, V>(&self, one_hot: V) -> Result<C, Failed>where
U: RealNumber,
V: Array1<U>,
pub fn invert_one_hot<U, V>(&self, one_hot: V) -> Result<C, Failed>where
U: RealNumber,
V: Array1<U>,
Invert one-hot vector, back to the category
Sourcepub fn get_ordinal<U>(&self, category: &C) -> Option<U>where
U: RealNumber,
pub fn get_ordinal<U>(&self, category: &C) -> Option<U>where
U: RealNumber,
Get ordinal encoding of the catergory
Trait Implementations§
Source§impl<C: Clone> Clone for CategoryMapper<C>
impl<C: Clone> Clone for CategoryMapper<C>
Source§fn clone(&self) -> CategoryMapper<C>
fn clone(&self) -> CategoryMapper<C>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl<C> Freeze for CategoryMapper<C>
impl<C> RefUnwindSafe for CategoryMapper<C>where
C: RefUnwindSafe,
impl<C> Send for CategoryMapper<C>where
C: Send,
impl<C> Sync for CategoryMapper<C>where
C: Sync,
impl<C> Unpin for CategoryMapper<C>where
C: Unpin,
impl<C> UnwindSafe for CategoryMapper<C>where
C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more