pub struct FeatureMatrix { /* private fields */ }Expand description
A feature matrix optimized for column-wise access in machine learning.
FeatureMatrix stores data in column-major format using ndarray::Array2,
making feature extraction efficient for algorithms that process columns
repeatedly. This is particularly beneficial for decision trees during split
finding.
§Invariants
n_samples > 0 && n_features > 0feature_names.len() == n_features- All data values are finite (validated at construction)
§Thread Safety
Cloning creates a new copy of the data; the struct is not designed for shared mutable access. Use immutable references or ownership passing.
Implementations§
Source§impl FeatureMatrix
impl FeatureMatrix
Sourcepub fn new(data: Array2<f64>) -> Result<Self, FeatureMatrixError>
pub fn new(data: Array2<f64>) -> Result<Self, FeatureMatrixError>
Creates a new feature matrix with auto-generated names.
Generates feature names in the format “feature_0”, “feature_1”, etc.
§Parameters
data: 2D array with shape (n_samples, n_features)
§Returns
Ok(FeatureMatrix) if validation passes
§Errors
FeatureMatrixError::InvalidShapeif data has zero rows or columns
Sourcepub fn with_feature_names(
data: Array2<f64>,
feature_names: Vec<String>,
) -> Result<Self, FeatureMatrixError>
pub fn with_feature_names( data: Array2<f64>, feature_names: Vec<String>, ) -> Result<Self, FeatureMatrixError>
Creates a new feature matrix with custom feature names.
§Parameters
data: 2D array with shape (n_samples, n_features)feature_names: Vector of names, one per column
§Returns
Ok(FeatureMatrix) if validation passes
§Errors
FeatureMatrixError::InvalidShapeif data has zero rows or columnsFeatureMatrixError::InvalidShapeif feature_names length doesn’t match columns
Sourcepub fn n_features(&self) -> usize
pub fn n_features(&self) -> usize
Returns the number of features (columns) in the matrix.
Sourcepub fn feature_names(&self) -> &[String]
pub fn feature_names(&self) -> &[String]
Returns a slice of feature names.
Sourcepub fn get_feature(
&self,
feature_idx: usize,
) -> Result<ArrayView1<'_, f64>, FeatureMatrixError>
pub fn get_feature( &self, feature_idx: usize, ) -> Result<ArrayView1<'_, f64>, FeatureMatrixError>
Sourcepub fn get_sample(
&self,
sample_idx: usize,
) -> Result<ArrayView1<'_, f64>, FeatureMatrixError>
pub fn get_sample( &self, sample_idx: usize, ) -> Result<ArrayView1<'_, f64>, FeatureMatrixError>
Sourcepub fn get(
&self,
sample_idx: usize,
feature_idx: usize,
) -> Result<f64, FeatureMatrixError>
pub fn get( &self, sample_idx: usize, feature_idx: usize, ) -> Result<f64, FeatureMatrixError>
Gets a single element at (sample_idx, feature_idx).
§Parameters
sample_idx: Row indexfeature_idx: Column index
§Returns
The scalar value at that position
§Errors
FeatureMatrixError::SampleIndexOutOfBoundsif sample_idx >= n_samplesFeatureMatrixError::FeatureIndexOutOfBoundsif feature_idx >= n_features
Sourcepub fn view(&self) -> ArrayView2<'_, f64>
pub fn view(&self) -> ArrayView2<'_, f64>
Gets a view of the entire matrix.
Useful for passing to ndarray-based algorithms.
Sourcepub fn samples(&self) -> impl Iterator<Item = ArrayView1<'_, f64>> + '_
pub fn samples(&self) -> impl Iterator<Item = ArrayView1<'_, f64>> + '_
Returns an iterator over samples (rows).
Each iteration yields a view of shape (n_features,).
Sourcepub fn features(&self) -> impl Iterator<Item = ArrayView1<'_, f64>> + '_
pub fn features(&self) -> impl Iterator<Item = ArrayView1<'_, f64>> + '_
Returns an iterator over features (columns).
Each iteration yields a view of shape (n_samples,). This is cache-friendly due to column-major storage.
Sourcepub fn feature_ranges(&self) -> Vec<(f64, f64)>
pub fn feature_ranges(&self) -> Vec<(f64, f64)>
Computes the min and max range for each feature.
Useful for decision tree split finding and feature scaling.
§Returns
Vector of (min, max) tuples, one per feature
Sourcepub fn feature_means(&self) -> Vec<f64>
pub fn feature_means(&self) -> Vec<f64>
Computes the mean of each feature.
Useful for imputation and model initialization.
§Returns
Vector of means, one per feature
Sourcepub fn select_samples(
&self,
sample_indices: &[usize],
) -> Result<Self, FeatureMatrixError>
pub fn select_samples( &self, sample_indices: &[usize], ) -> Result<Self, FeatureMatrixError>
Sourcepub fn select_features(
&self,
feature_indices: &[usize],
) -> Result<Self, FeatureMatrixError>
pub fn select_features( &self, feature_indices: &[usize], ) -> Result<Self, FeatureMatrixError>
Sourcepub fn from_csv<P: AsRef<Path>>(path: P) -> Result<Self, FeatureMatrixError>
pub fn from_csv<P: AsRef<Path>>(path: P) -> Result<Self, FeatureMatrixError>
Loads a feature matrix from a CSV file.
§Parameters
path: Path to CSV file with headers
§Returns
FeatureMatrix with data and column names
§Errors
FeatureMatrixError::CsvErrorif CSV parsing failsFeatureMatrixError::IoErrorif file cannot be readFeatureMatrixError::InvalidShapeif file is empty
§CSV Format
- First row: column headers (become feature names)
- Subsequent rows: numeric data (NaN for missing values)
Trait Implementations§
Source§impl Clone for FeatureMatrix
impl Clone for FeatureMatrix
Source§fn clone(&self) -> FeatureMatrix
fn clone(&self) -> FeatureMatrix
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for FeatureMatrix
impl Debug for FeatureMatrix
Source§impl<'de> Deserialize<'de> for FeatureMatrix
impl<'de> Deserialize<'de> for FeatureMatrix
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for FeatureMatrix
Equality comparison for feature matrices.
impl PartialEq for FeatureMatrix
Equality comparison for feature matrices.
Two matrices are equal if they have:
- Identical underlying data (element-wise)
- Same feature names (in same order)
Auto Trait Implementations§
impl Freeze for FeatureMatrix
impl RefUnwindSafe for FeatureMatrix
impl Send for FeatureMatrix
impl Sync for FeatureMatrix
impl Unpin for FeatureMatrix
impl UnwindSafe for FeatureMatrix
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more