maxabs_scale#

sklearn.preprocessing.maxabs_scale(X,*,axis=0,copy=True)[source]#

Scale each feature to the [-1, 1] range without breaking the sparsity.

This estimator scales each feature individually suchthat the maximal absolute value of each feature in thetraining set will be 1.0.

This scaler can also be applied to sparse CSR or CSC matrices.

Parameters:
X{array-like, sparse matrix} of shape (n_samples, n_features)

The data.

axis{0, 1}, default=0

Axis used to scale along. If 0, independently scale each feature,otherwise (if 1) scale each sample.

copybool, default=True

If False, try to avoid a copy and scale in place.This is not guaranteed to always work in place; e.g. if the data isa numpy array with an int dtype, a copy will be returned even withcopy=False.

Returns:
X_tr{ndarray, sparse matrix} of shape (n_samples, n_features)

The transformed data.

Warning

Risk of data leakDo not usemaxabs_scale unless you knowwhat you are doing. A common mistake is to apply it to the entire databefore splitting into training and test sets. This will bias themodel evaluation because information would have leaked from the testset to the training set.In general, we recommend usingMaxAbsScaler within aPipeline in order to prevent most risks of dataleaking:pipe=make_pipeline(MaxAbsScaler(),LogisticRegression()).

See also

MaxAbsScaler

Performs scaling to the [-1, 1] range using the Transformer API (e.g. as part of a preprocessingPipeline).

Notes

NaNs are treated as missing values: disregarded to compute the statistics,and maintained during the data transformation.

For a comparison of the different scalers, transformers, and normalizers,see:Compare the effect of different scalers on data with outliers.

Examples

>>>fromsklearn.preprocessingimportmaxabs_scale>>>X=[[-2,1,2],[-1,0,1]]>>>maxabs_scale(X,axis=0)# scale each column independentlyarray([[-1. ,  1. ,  1. ],       [-0.5,  0. ,  0.5]])>>>maxabs_scale(X,axis=1)# scale each row independentlyarray([[-1. ,  0.5,  1. ],       [-1. ,  0. ,  1. ]])
On this page

This Page