MinMaxScaler in Pandas

MinMaxScaler is a transformation class from scikit-learn that scales and translates each feature individually such that it is in the given range on the training set, e.g. between zero and one. It is a popular scaling technique used in machine learning to normalize features before training a model.

To use MinMaxScaler in Pandas, we can first import the necessary libraries:

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

Once we have imported the necessary libraries, we can create a MinMaxScaler object and fit it to the data:

scaler = MinMaxScaler()

scaler.fit(df)

Once the scaler has been fitted to the data, we can use it to transform the data:

scaled_df = scaler.transform(df)

The scaled data will be returned as a NumPy array. We can then convert the NumPy array back to a Pandas DataFrame if needed:

scaled_df = pd.DataFrame(scaled_df, columns=df.columns)

The following example shows how to use MinMaxScaler to scale the features in a Pandas DataFrame:

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

df = pd.DataFrame({'age': [25, 30, 35, 40], 'height': [165, 170, 175, 180]})

scaler = MinMaxScaler()

scaler.fit(df)

scaled_df = scaler.transform(df)

print(scaled_df)

The scaled data is in the range 0 to 1.

MinMaxScaler is a popular scaling technique used in machine learning to normalize features before training a model. It is easy to use and effective in normalizing features with different ranges.

Leave a Reply