Scaler

class Scaler(self, algorithm: Literal['standard', 'minmax', 'maxabs', 'robust', 'normalizer', 'power', 'quantile', 'quantile_normal'] = 'standard', **kwargs)

Scaler Wrapper class - parent class Data

Parameters

algorithm{“standard”, “minmax”, “maxabs”, “robust”, “normalizer”, “power”, “quantile”, “quantile_normal”}, default=”standard”

which scaling algorithm to use: - ‘standard’: StandardScaler - ‘minmax’: MinMaxScaler - ‘maxabs’: MaxAbsScaler - ‘robust’: RobustScaler - ‘normalizer’: Normalizer - ‘power’: PowerTransformer with method=”yeo-johnson” - ‘quantile’: QuantileTransformer (default of QuantileTransformer) - ‘quantile_normal’: QuantileTransformer with output_distribution=”normal” (gaussian pdf)

**kwargs:

additional parameters for scaler

Attributes

algorithmstr

name of the used algorithm

transformertransformer instance

transformer instance (e.g. StandardScaler)

Example

>>> from sam_ml.data.preprocessing import Scaler
>>>
>>> model = Scaler()
>>> print(model)
Scaler()

Methods

Method

Description

get_params

Function to get the parameter from the transformer instance

params

Function to get the possible parameter values for the class

scale

Function to scale/normalise data

set_params

Function to set the parameter of the transformer instance

Scaler.get_params(deep: bool = True) dict

Function to get the parameter from the transformer instance

Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained sub-objects that are estimators

Returns

params: dict

parameter names mapped to their values

static Scaler.params() dict

Function to get the possible parameter values for the class

Returns

paramdict

possible values for the parameter “algorithm”

Examples

>>> # get possible parameters
>>> from sam_ml.data.preprocessing import Scaler
>>>
>>> # first way without class object
>>> params1 = Scaler.params()
>>> print(params1)
{"algorithm": ["standard", ...]}
>>> # second way with class object
>>> model = Scaler()
>>> params2 = model.params()
>>> print(params2)
{"algorithm": ["standard", ...]}
Scaler.scale(data: DataFrame, train_on: bool = True) DataFrame

Function to scale/normalise data

Parameters

datapd.DataFrame

data that shall be scaled

train_onbool, defautl=True

If True, the estimator instance will fit_transform. Otherwise, just transform

Returns

scaled_dfpd.DataFrame

Dataframe with scaled data

Examples

>>> # load data (replace with own data)
>>> import pandas as pd
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> df = load_iris()
>>> X, y = pd.DataFrame(df.data, columns=df.feature_names), pd.Series(df.target)
>>> x_train, x_test, y_train, y_test = train_test_split(X,y, train_size=0.80, random_state=42)
>>>
>>> # scale data
>>> from sam_ml.data.preprocessing import Scaler
>>>
>>> model = Scaler()
>>> x_train_scaled = model.scale(x_train) # train scaler
>>> x_test_scaled = model.scale(x_test, train_on=False) # scale test data
>>> print("before scaling:")
>>> print(x_train.iloc[0])
>>> print()
>>> print("after scaling:")
>>> print(x_train_scaled.iloc[0])
before scaling:
sepal length (cm)    4.6
sepal width (cm)     3.6
petal length (cm)    1.0
petal width (cm)     0.2
Name: 22, dtype: float64

after scaling:
sepal length (cm)   -1.473937
sepal width (cm)     1.203658
petal length (cm)   -1.562535
petal width (cm)    -1.312603
Name: 0, dtype: float64
Scaler.set_params(**params)

Function to set the parameter of the transformer instance

Parameters

**paramsdict

Estimator parameters

Returns

selfestimator instance

Estimator instance