Struct CategoryMapper

Source
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>
where C: Hash + Eq + Clone,

Source

pub fn num_categories(&self) -> usize

Get the number of categories in the mapper

Source

pub fn fit_to_iter(categories: impl Iterator<Item = C>) -> Self

Fit an encoder to a lable iterator

Source

pub fn from_category_map(category_map: HashMap<C, usize>) -> Self

Build an encoder from a predefined (category -> class number) map

Source

pub fn from_positional_category_vec(categories: Vec<C>) -> Self

Build an encoder from a predefined positional category-class num vector

Source

pub fn get_num(&self, category: &C) -> Option<&usize>

Get label num of a category

Source

pub fn get_cat(&self, num: usize) -> &C

Return category corresponding to label num

Source

pub fn get_categories(&self) -> &[C]

List all categories (position = category number)

Source

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

Source

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

Source

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>

Source§

fn clone(&self) -> CategoryMapper<C>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug> Debug for CategoryMapper<C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.