'Can someone explain to me how MinMaxScaler() works? [closed]
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)
Solution 1:[1]
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: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html
Solution 2:[2]
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) + min
Example:
>>> 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. ]]
https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | praneeth |
| Solution 2 | Shamsul Masum |
