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?
2 Answers
Why not just use the original MinMaxScaler API in a following way:
- reshape X numpy array to one-column array,
- Scale,
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.
-
1Well that's exactly the same as what MinMaxScaler already does isn't it, scale the data column by column. Instead what I'm looking for from your example would be for -1 to be scaled to 0 and 6 to be scaled to 1 since -1 is the smallest value in the np array and 6 is the largest Sep 7, 2018 at 16:15