NavidReza Ghanbari
NavidReza Ghanbari

Reputation: 33

MinMaxScaler for a number of columns in a pandas DataFrame

I want to apply MinmaxScaler on a number of pandas DataFrame 'together'. Meaning that I want the scaler to perform on all data in those columns, not separately on each column.

My DataFrame has 20 columns. I want to apply the scaler on 12 of the columns at the same time. I have already read this. But it does not solve my problem since it acts on each column separately.

Upvotes: 3

Views: 1022

Answers (3)

upandacross
upandacross

Reputation: 417

Mustafa has a good solution save one needed change:

df[cols].to_numpy().min() gives a single minimum value across all columns. What you want is the minimum of each column. Same for maximum.

You do this by simply using pandas: min_val = df[cols].min(); max_val = df[cols].max(). At that point, the normalization occurs with appropriate min and max values for each column and calculates it in a vectorized manner just as Mustafa has it.

Upvotes: 0

Mustafa Aydın
Mustafa Aydın

Reputation: 18306

you can extract the "min" and "max" statistics from those columns and perform the scaling yourself:

# columns of interest
cols = [...]

# get the minimum and maximum values in that region
vals = df[cols].to_numpy()
min_val = vals.min()
max_val = vals.max()

# scale the region using them
df[cols] = df[cols].sub(min_val).div(max_val - min_val)

(sub is method way of doing "-" and div is for "/".)

Above, df is your training dataframe; to scale the testing dataframe, you replace df with that in the last line, e.g.,

test_df[cols] = test_df[cols].sub(min_val).div(max_val - min_val)

instead of extracting min/max of it separately which would leak information from the test set.

Upvotes: 1

Akshay Sehgal
Akshay Sehgal

Reputation: 19322

IIUC, you want the sklearn scaler to fit and transform multiple columns with the same criteria (in this case min and max definitions). Here is one way you can do this -

  1. You can save the initial shape of the columns and then transform the numpy array of those columns into a 1D array from a 2D array.
  2. Next you can fit your scaler and transform this 1D array
  3. Finally you can use the old shape to reshape the array back into the n columns you need and save them

The advantage of this approach is that this works with any of the sklearn scalers you need to use, MinMaxScaler, StandardScaler etc.

import pandas as pd
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()

dfTest = pd.DataFrame({'A':[14.00,90.20,90.95,96.27,91.21],
                       'B':[103.02,107.26,110.35,114.23,114.68],
                       'C':['big','small','big','small','small']})

cols = ['A','B']
old_shape = dfTest[cols].shape #(5,2)

dfTest[cols] = scaler.fit_transform(dfTest[cols].to_numpy().reshape(-1,1)).reshape(old_shape)
print(dfTest)
          A         B      C
0  0.000000  0.884188    big
1  0.756853  0.926301  small
2  0.764303  0.956992    big
3  0.817143  0.995530  small
4  0.766885  1.000000  small

Upvotes: 1

Related Questions