RobustScaler
#include <Skigen/Preprocessing>
template <typename Scalar = double>
class Skigen::RobustScaler(with_centering=true, with_scaling=true, quantile_range={25, 75})
Scale features using statistics that are robust to outliers.
This Scaler removes the median and scales the data according to the quantile range (defaults to IQR: Interquartile Range). The IQR is the range between the 1st quartile (25th quantile) and the 3rd quartile (75th quantile).
Centering and scaling happen independently on each feature.
Mirrors sklearn.preprocessing.RobustScaler.
Parameters:
-
with_centering : bool, default=true If
true, center the data before scaling by subtracting the median (bool, defaulttrue). -
with_scaling : bool, default=true If
true, scale the data to interquartile range (bool, defaulttrue). -
quantile_range : std::pair< Scalar, Scalar >, default={25, 75} Quantile range for computing
scale_(std::pair<Scalar,Scalar>, default{25, 75}).
Attributes:
-
with_centering : bool Whether centering is enabled.
-
with_scaling : bool Whether scaling is enabled.
-
center : RowVectorType Per-feature median (1 × n_features).
-
scale : RowVectorType Per-feature interquartile range (1 × n_features).
Methods
fit(X)
Compute median and quantile range for later scaling.
Parameters:
- X : MatrixType Training data of shape (n_samples, n_features).
Returns:
- result : RobustScaler
Reference to the fitted transformer (
*this).
transform(X)
Center and scale the data.
Parameters:
- X : MatrixType Data matrix of shape (n_samples, n_features).
Returns:
- result : MatrixType Transformed data of same shape.
Throws:
std::runtime_error— if the model has not been fitted.
inverse_transform(X)
Scale back the data to the original representation.
Parameters:
- X : MatrixType Transformed data of shape (n_samples, n_features).
Returns:
- result : MatrixType Un-transformed data of same shape.
Throws:
std::runtime_error— if the model has not been fitted.
transform_inplace(X)
Transform features in-place using robust statistics.
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.
inverse_transform_inplace(X)
Inverse-transform features in-place to original scale.
Parameters:
- X : Eigen::Ref< MatrixType > Scaled data matrix, modified in place.
Throws:
std::runtime_error— if the model has not been fitted.
Example
// RobustScaler uses median and IQR — resistant to outliers
Skigen::RobustScaler<double> robust;
Eigen::MatrixXd Z_robust = robust.fit_transform(X);
std::cout << "Robust-scaled:\n" << Z_robust << "\n\n";
std::cout << "Center (median): " << robust.center() << "\n";
std::cout << "Scale (IQR): " << robust.scale() << "\n\n";