Skip to main content

FeatureCollection

Struct FeatureCollection 

Source
pub struct FeatureCollection {
    pub features: Vec<Array1<f64>>,
    pub feature_names: Vec<String>,
    pub add_constant: bool,
    /* private fields */
}
Expand description

Collection of features that are organized for modelling.

§Examples

  1. Working with feature collection
use ndarray::{Array1, Array2, array};
use digifi::utilities::FeatureCollection;
 
// Features
let x_1: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let x_2: Vec<f64> = vec![6.0, 5.0, 4.0, 3.0, 2.0, 1.0];
let x_3: Array1<f64> = Array1::from_vec(vec![9.0, 8.0, 7.0, 6.0, 5.0, 4.0]);
let x_4: Array1<f64> = Array1::from_vec(vec![4.0, 5.0, 6.0, 7.0, 8.0, 9.0]);
 
// Create feature collection with iterators from different types of data structures
let mut fc: FeatureCollection = FeatureCollection::new();
fc.add_feature(x_1.iter(), "x_1").unwrap();
assert_eq!(fc.len(), 1);
assert_eq!(fc.feature_size(), Some(6));
fc.add_feature(x_2.into_iter(), "x_2").unwrap();
assert_eq!(fc.len(), 2);
assert_eq!(fc.feature_size(), Some(6));
fc.add_feature(x_3.iter(), "x_3").unwrap();
assert_eq!(fc.len(), 3);
assert_eq!(fc.feature_size(), Some(6));
fc.add_feature(x_4.into_iter(), "x_4").unwrap();
assert_eq!(fc.len(), 4);
assert_eq!(fc.feature_size(), Some(6));
 
// Get features from the collection
assert_eq!(fc.get_feature_array("x_3").unwrap(), &x_3);
let matrix: Array2<f64> = array![
    [1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
    [6.0, 5.0, 4.0, 3.0, 2.0, 1.0],
    [9.0, 8.0, 7.0, 6.0, 5.0, 4.0],
    [4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
].reversed_axes();
assert_eq!(fc.get_matrix().unwrap(), matrix);
 
// Remove feature
fc.remove_feature("x_1").unwrap();
assert_eq!(fc.len(), 3);
assert_eq!(fc.feature_size(), Some(6));
  1. Using feature collection for modelling
use ndarray::{Array1, Array2, array};
use digifi::utilities::{TEST_ACCURACY, FeatureCollection};
use digifi::statistics::linear_regression;
 
let mut fc: FeatureCollection = FeatureCollection::new();
fc.add_constant = true;
fc.add_feature(vec![1.0, 4.0, 6.0].into_iter(), "x_1").unwrap();
fc.add_feature(vec![3.0, 4.0, 5.0].into_iter(), "x_2").unwrap();
let y: Array1<f64> = array![1.0, 2.0, 3.0];
let params: Array1<f64> = linear_regression(&fc.into_matrix().unwrap(), &y).unwrap();
 
// The results were found using LinearRegression from sklearn
let results: Array1<f64> = Array1::from(vec![-2.49556592e-16, 1.0, -2.0]);
assert!((&params - &results).map(|v| v.abs() ).sum() < TEST_ACCURACY);

Fields§

§features: Vec<Array1<f64>>§feature_names: Vec<String>§add_constant: bool

Implementations§

Source§

impl FeatureCollection

Source

pub fn new() -> Self

Creates a new instance of FeatureCollection.

Source

pub fn len(&self) -> usize

Returns the number of features in the collection.

Source

pub fn feature_size(&self) -> Option<usize>

Returns the length of a feature in the collection.

Note: This length will be the same for every feature.

Source

pub fn get_feature_index( &self, feature_name: &str, ) -> Result<usize, DigiFiError>

Returns the index of the feature in the collection.

Source

pub fn add_feature<T, I>( &mut self, feature: T, feature_name: &str, ) -> Result<(), DigiFiError>
where T: Iterator<Item = I> + ExactSizeIterator, I: Borrow<f64>,

Adds the feature to the collection.

Source

pub fn remove_feature(&mut self, feature_name: &str) -> Result<(), DigiFiError>

Removes the feature from the collection.

Source

pub fn get_feature_array( &self, feature_name: &str, ) -> Result<&Array1<f64>, DigiFiError>

Returns a feature as an Array1

Source

pub fn get_matrix(&self) -> Result<Array2<f64>, DigiFiError>

Returns a matrix that is composed of the features from the collection.

Source

pub fn into_matrix(self) -> Result<Array2<f64>, DigiFiError>

Source

pub fn ddof(&self) -> usize

Returns delta degrees of freedom.

Trait Implementations§

Source§

impl Debug for FeatureCollection

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for FeatureCollection

Source§

fn default() -> FeatureCollection

Returns the “default value” for a type. Read more
Source§

impl ErrorTitle for FeatureCollection

Source§

fn error_title() -> String

Returns the error title.

Auto Trait Implementations§

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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.