-1

Here's the code

from numpy import asarray
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler(feature_range=(0,1))
rescaledX = scaler.fit_transform(X)

and it always resulted in

ValueError: could not convert string to float: 'female'

How can I make this work?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • It appears as if you set x to 'female' at some point. Where do you get the value of x from? You might also want to take a look at this: https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int – Christian Hoffmann Dec 03 '21 at 09:09
  • 2
    Please provide a [mcve], something people can run to reproduce the error. For example, what is `X`? – Gino Mempin Dec 03 '21 at 09:09

1 Answers1

2

MinMaxScaler works only with numerical features.

it seems that your feature is categorical, therefore you should use OrdinalEncoder or OneHotEncoder as follows:

from numpy import asarray
from sklearn.preprocessing import OrdinalEncoder

X = np.reshape(['male', 'male', 'female'], (-1, 1))

OrdinalEncoder().fit_transform(X)

Output:

array([[1.],
       [1.],
       [0.]])
Antoine Dubuis
  • 4,974
  • 1
  • 15
  • 29