Why we are using the MinMaxScaler() and what does it do?
scaler = MinMaxScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
model = LogisticRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test) 1 3 Answers
Core of the method
A way to normalize the input features/variables is the Min-Max scaler. By doing so, all features will be transformed into the range [0,1] meaning that the minimum and maximum value of a feature/variable is going to be 0 and 1, respectively.
Why to normalize prior to model fitting?
The main idea behind normalization/standardization is always the same. Variables that are measured at different scales do not contribute equally to the model fitting & model learned function and might end up creating a bias. Thus, to deal with this potential problem feature-wise normalization such as MinMax Scaling is usually used prior to model fitting.
More here:
Essentially, the code is scaling the independent variables so that they lie in the range of 0 and 1. This is important because few variable values might be in thousands and few might be in small ranges. Hence to handle such cases scaling is important. Logistic regression is sensitive to such high values. More about min max is here:
The methodology that MinMaxScaler Follow:
X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
X_scaled = X_std * (max - min) + minExample:
>>> from sklearn.preprocessing import MinMaxScaler
>>> data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
>>> scaler = MinMaxScaler()
>>> print(scaler.fit(data))
MinMaxScaler()
>>> print(scaler.data_max_)
[ 1. 18.]
>>> print(scaler.transform(data))
[[0. 0. ] [0.25 0.25] [0.5 0.5 ] [1. 1. ]]
>>> print(scaler.transform([[2, 2]]))
[[1.5 0. ]]