How to Use MinMax Scaler in Python
Python is a high-level, general-purpose and interpreted programming language. It is known for its ease of use, powerful standard library and dynamic semantics. Python is widely used in various sectors including machine learning, artificial intelligence, data analysis, web development and many more. Its simple, easy-to-learn syntax emphasizes readability and therefore reduces the cost of program maintenance.
What is a MinMaxScaler?
In Python, the MinMaxScaler
is a preprocessing utility in the sklearn.preprocessing
module of the scikit-learn
library. It scales each feature (i.e., column in your data) individually such that it is in the given range on the training set, typically between zero and one.
The transformation is given by:
where min, max = feature_range
, X.min(axis=0)
is the minimum feature value, and X.max(axis=0)
is the maximum feature value.
This transformation is often used as an alternative to zero mean, unit variance scaling. While MinMaxScaler
doesn’t reduce the effect of outliers, it linearly scales them down into a fixed range.
The problem we will be solving?
This is Rain in Australia dataset in which on the basis of a number of parameters we decide whether on a given day it will rain or not. The shape of data being (145460, 23).
RIAUS.info()
RIAUS is our DataFrame storing the data having 23 different columns shown by RIAUS.info()
We will first extract all the numeric columns in our DataFrame. Now numeric columns are all those columns which have Dtype of int64 or float64. Then we will use MinMaxScaler on the numeric columns.
1.Using list comprehension to extract numeric columns:
num_cols = [cols for cols in RIAUS.columns if RIAUS[cols].dtype in ['int64', 'float64']]
So, from the above code the num_cols
will be a list of the names of all numerical columns in the DataFrame RIAUS
. To learn more about how this code works refer to this link. It’s short and easy.
2.Using MinMaxScaler for the numeric columns:
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
for cols in num_cols:
k = np.array(RIAUS[cols])
k = k.reshape(-1, 1)
k = scaler.fit_transform(k)
RIAUS[cols] = k
scaler = MinMaxScaler()
: This line creates an instance of theMinMaxScaler
class.for cols in num_cols:
: This line starts a loop that will iterate over each column name in the listnum_cols
.k = np.array(RIAUS[cols])
: This line converts the data in the current column of the DataFrameRIAUS
into a numpy array and assigns it to the variablek
.k = k.reshape(-1, 1)
: This line reshapes the arrayk
into a 2D array with one column. The-1
in the reshape function means that the size in that dimension is inferred from the length of the array and the remaining dimensions.k = scaler.fit_transform(k)
: This line fits the scaler to the data ink
(i.e., it computes the minimum and maximum values), and then it transformsk
by scaling it to the range [0, 1]. The transformed data is assigned back tok
.RIAUS[cols] = k
: This line replaces the original data in the current column ofRIAUS
with the scaled data ink
.
So, in summary, this code scales the data in each numerical column of the DataFrame RIAUS
to the range [0, 1] using the MinMaxScaler
.
With this we are done now you can use the scaled numeric columns for your preferred ML algorithms.
To learn how to use StandardScaler in python refer to this link.
To learn how to use Ordinal encoding on python refer to this link.
To extract categorical columns in python refer to this link.
To learn how to use OneHotEncoder in python refer to this link.
To learn how to use PCA in python refer to this link.
To read more stories like this you can follow me with this link.
References:
- https://www.geeksforgeeks.org/what-is-python/
- https://www.python.org/doc/essays/blurb/
- https://www.britannica.com/technology/Python-computer-language
- https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html
Stackademic 🎓
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us X | LinkedIn | YouTube | Discord
- Visit our other platforms: In Plain English | CoFeed | Venture | Cubed
- More content at Stackademic.com