Scaling Data Range using Min Max Scaler

Suppose you have a dataset that has float values and all values in the range 0 to 1.

You want to change all values to integer with a range between 10 to 20.

In this post we will learn how to do this using MinMaxScaler

Data before scaling
Data before scaling

Now let us scale the data as below

# convert the data to a given range
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 10)) 
dfx[0] = scaler.fit_transform(dfx[0].values.reshape(-1, 1))
scaler = MinMaxScaler(feature_range=(20, 50))
dfx[1] = scaler.fit_transform(dfx[1].values.reshape(-1, 1))
dfx.head()

Data after Scaling is as below

Scaling the data using Min Max Scaler
Scaling the data using Min Max Scaler

You can also round all the float data above to nearest integer as below

dfx = dfx.round(0) # rounds to nearest integer
import numpy as np
dfx = dfx.applymap(np.int64)
Dataset rounded to nearest integer
Dataset rounded to nearest integer

I home you will find these code useful

Cheers !!

Advertisements

Leave a Reply