Skip to content

Commit d305406

Browse files
authored
Implementation of Standard scaler (#143)
* docs: Fix typo in doc for categorical transformer. * feat: Add option to take a column from Matrix. I created the method `Matrix::take_column` that uses the `Matrix::take`-interface to extract a single column from a matrix. I need that feature in the implementation of `StandardScaler`. * feat: Add `StandardScaler`. Authored-by: titoeb <timtoebrock@googlemail.com>
1 parent 3d2f4f7 commit d305406

File tree

3 files changed

+428
-1
lines changed

3 files changed

+428
-1
lines changed

src/linalg/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,10 @@ pub trait BaseMatrix<T: RealNumber>: Clone + Debug {
651651

652652
result
653653
}
654+
/// Take an individual column from the matrix.
655+
fn take_column(&self, column_index: usize) -> Self {
656+
self.take(&[column_index], 1)
657+
}
654658
}
655659

656660
/// Generic matrix with additional mixins like various factorization methods.
@@ -761,4 +765,21 @@ mod tests {
761765
assert_eq!(m.take(&vec!(1, 1, 3), 0), expected_0);
762766
assert_eq!(m.take(&vec!(1, 0), 1), expected_1);
763767
}
768+
769+
#[test]
770+
fn take_second_column_from_matrix() {
771+
let four_columns: DenseMatrix<f64> = DenseMatrix::from_2d_array(&[
772+
&[0.0, 1.0, 2.0, 3.0],
773+
&[0.0, 1.0, 2.0, 3.0],
774+
&[0.0, 1.0, 2.0, 3.0],
775+
&[0.0, 1.0, 2.0, 3.0],
776+
]);
777+
778+
let second_column = four_columns.take_column(1);
779+
assert_eq!(
780+
second_column,
781+
DenseMatrix::from_2d_array(&[&[1.0], &[1.0], &[1.0], &[1.0]]),
782+
"The second column was not extracted correctly"
783+
);
784+
}
764785
}

src/preprocessing/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
/// Transform a data matrix by replaceing all categorical variables with their one-hot vector equivalents
1+
/// Transform a data matrix by replacing all categorical variables with their one-hot vector equivalents
22
pub mod categorical;
33
mod data_traits;
4+
/// Preprocess numerical matrices.
5+
pub mod numerical;
46
/// Encode a series (column, array) of categorical variables as one-hot vectors
57
pub mod series_encoder;

0 commit comments

Comments
 (0)