Normalizer
#include <Skigen/Preprocessing>
template <typename Scalar = double>
class Skigen::Normalizer(norm=Norm::L2)
Normalize samples individually to unit norm.
Each sample (i.e. each row of the data matrix) with at least one non-zero component is rescaled independently of other samples so that its norm (L1, L2, or max) equals one.
Mirrors sklearn.preprocessing.Normalizer.
Parameters:
- norm : Norm, default=Norm::L2
The norm to use (
Norm, defaultNorm::L2).
Attributes:
- norm : Norm The norm type in use.
Methods
fit(X)
Fit the Normalizer (stateless — only records n_features_in_).
Parameters:
- X : MatrixType Training data of shape (n_samples, n_features).
Returns:
- result : Normalizer
Reference to the fitted transformer (
*this).
transform(X)
Normalize each sample to unit norm.
Parameters:
- X : MatrixType Data matrix of shape (n_samples, n_features).
Returns:
- result : MatrixType Normalized data of same shape.
Throws:
std::runtime_error— if the model has not been fitted.
inverse_transform()
Not supported — normalization is not reversible.
Throws:
std::runtime_error— Always throws.
transform_inplace(X)
Normalize samples in-place to unit norm.
Parameters:
- X : Eigen::Ref< MatrixType > Data matrix of shape (n_samples, n_features), modified in place.
Throws:
std::runtime_error— if the model has not been fitted.
Example
// L2 normalization (default) — each row has unit Euclidean norm
Skigen::Normalizer<double> l2_norm(Skigen::Norm::L2);
Eigen::MatrixXd Z_l2 = l2_norm.fit_transform(X);
std::cout << "L2 normalized:\n" << Z_l2 << "\n";
std::cout << "Row norms: ";
for (Eigen::Index i = 0; i < Z_l2.rows(); ++i)
std::cout << Z_l2.row(i).norm() << " ";
std::cout << "\n\n";
// L1 normalization — each row sums to 1 (in absolute value)
Skigen::Normalizer<double> l1_norm(Skigen::Norm::L1);
Eigen::MatrixXd Z_l1 = l1_norm.fit_transform(X);
std::cout << "L1 normalized:\n" << Z_l1 << "\n";