Kake er godt
Kake er godt

Reputation: 73

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

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.]]

Upvotes: 6

Views: 2316

Answers (3)

user1844086
user1844086

Reputation:

One dumb thing that might be frustrating you (it burnt me) -- it can happen that your column number format may be set to display numbers as int, so that even if you've correctly done your scaling it will still show as 1,0.

You can guard against this by forcing the float format to display on your data frames like so:

pd.options.display.float_format = '{:,.2f}'.format 

Upvotes: 0

Venkatachalam
Venkatachalam

Reputation: 16966

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.

Upvotes: 0

Roim
Roim

Reputation: 3066

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

Upvotes: 3

Related Questions