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?
Why not just use the original MinMaxScaler API in a following way:
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. ]]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With