【发布时间】:2017-01-22 11:16:53
【问题描述】:
我在数据集中有一列具有分类值,我想将它们转换为数值。我正在尝试使用 LabelEncoder,但这样做会出错。
from sklearn.preprocessing import LabelEncoder
m = hsp_train["Alley"]
m_enc = LabelEncoder()
j = m_enc.fit_transform(m)
我收到一个错误:
不可排序的类型:float() > str()
列中的系列有 3 个值。我希望它们分别为 0、1、2,但我得到了那个错误。
我也试过这个:
l = hsp_train["Alley"]
l_enc = pd.factorize(l)
hsp_train["Alley"] = l_enc[0]
但这给了我值 -1、1、2。我不想从 1 得到它。
【问题讨论】:
-
据我所知,OneHotEncoder 对我认为的整数很有用,而 pandas.get_dummies 会增加我不想要的列数。
标签: python pandas machine-learning scikit-learn