onehotencoding中的ValueError

时间:2018-03-26 10:08:58

标签: python-3.x scikit-learn one-hot-encoding

我无法对此列进行编码

Sex
male
female
female
female
male
male
male
male
female
female

from sklearn.preprocessing import LabelEncoder,OneHotEncoder
labelencoder=LabelEncoder()
X[:,2]=labelencoder.fit_transform(X[:,2])
ohe=OneHotEncoder(categorical_features=X[2])
ohe.fit_transform(X)

我收到此错误。

  

无法将字符串转换为浮点数:'男性'

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

演示:

In [6]: df
Out[6]:
      Sex
0    male
1  female
2  female
3  female
4    male
5    male
6    male
7    male
8  female
9  female

In [7]: le = LabelEncoder()

In [8]: df['Sex'] = le.fit_transform(df['Sex'])

In [9]: df
Out[9]:
   Sex
0    1
1    0
2    0
3    0
4    1
5    1
6    1
7    1
8    0
9    0

In [10]: df.dtypes
Out[10]:
Sex    int64
dtype: object