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
- 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));- 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!((¶ms - &results).map(|v| v.abs() ).sum() < TEST_ACCURACY);Fields§
§features: Vec<Array1<f64>>§feature_names: Vec<String>§add_constant: boolImplementations§
Source§impl FeatureCollection
impl FeatureCollection
Sourcepub fn feature_size(&self) -> Option<usize>
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.
Sourcepub fn get_feature_index(
&self,
feature_name: &str,
) -> Result<usize, DigiFiError>
pub fn get_feature_index( &self, feature_name: &str, ) -> Result<usize, DigiFiError>
Returns the index of the feature in the collection.
Sourcepub fn add_feature<T, I>(
&mut self,
feature: T,
feature_name: &str,
) -> Result<(), DigiFiError>
pub fn add_feature<T, I>( &mut self, feature: T, feature_name: &str, ) -> Result<(), DigiFiError>
Adds the feature to the collection.
Sourcepub fn remove_feature(&mut self, feature_name: &str) -> Result<(), DigiFiError>
pub fn remove_feature(&mut self, feature_name: &str) -> Result<(), DigiFiError>
Removes the feature from the collection.
Sourcepub fn get_feature_array(
&self,
feature_name: &str,
) -> Result<&Array1<f64>, DigiFiError>
pub fn get_feature_array( &self, feature_name: &str, ) -> Result<&Array1<f64>, DigiFiError>
Returns a feature as an Array1
Sourcepub fn get_matrix(&self) -> Result<Array2<f64>, DigiFiError>
pub fn get_matrix(&self) -> Result<Array2<f64>, DigiFiError>
Returns a matrix that is composed of the features from the collection.
pub fn into_matrix(self) -> Result<Array2<f64>, DigiFiError>
Trait Implementations§
Source§impl Debug for FeatureCollection
impl Debug for FeatureCollection
Source§impl Default for FeatureCollection
impl Default for FeatureCollection
Source§fn default() -> FeatureCollection
fn default() -> FeatureCollection
Returns the “default value” for a type. Read more
Source§impl ErrorTitle for FeatureCollection
impl ErrorTitle for FeatureCollection
Source§fn error_title() -> String
fn error_title() -> String
Returns the error title.
Auto Trait Implementations§
impl Freeze for FeatureCollection
impl RefUnwindSafe for FeatureCollection
impl Send for FeatureCollection
impl Sync for FeatureCollection
impl Unpin for FeatureCollection
impl UnsafeUnpin for FeatureCollection
impl UnwindSafe for FeatureCollection
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
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.