sklearn.preprocessing.MinMaxScaler() only returns 0 or 1 and not float

Viewed 1242

For whatever reason this only returns 0 or 1 instead of float between them.

from sklearn import preprocessing

X = [[1.3, 1.6, 1.4, 1.45, 12.3, 63.01,],
    [1.9, 0.01, 4.3, 45.4, 3.01, 63.01]]

minmaxscaler = preprocessing.MinMaxScaler()
X_scale = minmaxscaler.fit_transform(X)

print(X_scale) # returns [[0. 1. 0. 0. 1. 0.] [1. 0. 1. 1. 0. 0.]]
2 Answers

Minmax Scaler can not work with list of lists, it needs to work with numpy array for example (or dataframes).

You can convert to numpy array. It will result 6 features with 2 samples, which I guess is not what you means so you need also reshape.

import numpy

X = numpy.array([[1.3, 1.6, 1.4, 1.45, 12.3, 63.01,],
    [1.9, 0.01, 4.3, 45.4, 3.01, 63.01]]).reshape(-1,1)

Results after MinMax Scaler:

[[0.02047619]
 [0.0252381 ]
 [0.02206349]
 [0.02285714]
 [0.19507937]
 [1.        ]
 [0.03      ]
 [0.        ]
 [0.06809524]
 [0.72047619]
 [0.04761905]
 [1.        ]]

Not exactly sure if you want to minimax each list separatly or all together

The answer which you have got from MinMaxScaler is the expected answer.

When you have only two datapoints, you will get only 0s and 1s. See the example here for three datapoints scenario.

You need to understand that it will convert the lowest value as 0 and highest values as 1 for each column. When you have more datapoints, the remaining ones would calculation based on the range (Max-min). see the formula here.

Also, MinMaxScaler accepts 2D data, which means lists of list is acceptable. Thats the reason why you did not got any error.

Related