Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MinMaxScaler on all columns?

Right now, I have my data in a 2 by 2 numpy array. If I was to use MinMaxScaler fit_transform on the array, it will normalize it column by column, whereas I wish to normalize the entire np array all together. Is there anyway to do that?

like image 357
Chris Wang Avatar asked Oct 19 '25 11:10

Chris Wang


2 Answers

Why not just use the original MinMaxScaler API in a following way:

  1. reshape X numpy array to one-column array,
  2. Scale,
  3. Reshape results back to the shape of X array

    import numpy as np
    
    X = np.array([[-1, 2], [-0.5, 6]])
    scaler = MinMaxScaler()
    X_one_column = X.reshape([-1,1])
    result_one_column = scaler.fit_transform(X_one_column)
    result = result_one_column.reshape(X.shape)
    print(result)
    

Output

[[ 0.          0.42857143]
 [ 0.07142857  1.        ]]
like image 59
Maciek Avatar answered Oct 22 '25 04:10

Maciek


From the documentation it seems you cannot change the axis of the MinMaxScaler. One alternative is to define a scaling function based on the definition of the MinMaxScaler, from the documentation:

X_std = (X - X.min()) / (X.max() - X.min())
X_scaled = X_std * (max - min) + min

So you can do it like this:

import numpy  as np

X = np.array([[-1, 2], [-0.5, 6]])


def min_max_scale(X, range=(0, 1)):
    mi, ma = range
    X_std = (X - X.min()) / (X.max() - X.min())
    X_scaled = X_std * (ma - mi) + mi
    return X_scaled

print(min_max_scale(X))

Output

[[0.         0.42857143]
 [0.07142857 1.        ]]

Basically you need to drop the axis parameter, to consider the maximum and minimum from the whole array.

like image 26
Dani Mesejo Avatar answered Oct 22 '25 05:10

Dani Mesejo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!