【问题标题】:LabelEncoder is not converting the strings into numericals (0,1,2)LabelEncoder 没有将字符串转换为数字 (0,1,2)
【发布时间】:2019-09-24 19:46:48
【问题描述】:

我有如下数据,我需要对变量进行编码,但是 LabelEncoder 没有对字符串进行编码

我的数据如下所示

Delivery_class
First Class
Same Day
Second Class
Standard Class

X=filtered_df.iloc[:, 1]
labelencoder_X = LabelEncoder()
X.values[:,1] = labelencoder_X.fit_transform(X.values[:,1].astype(str))

即使在运行 abovr 代码之后,字符串仍然保持不变。

请指教,我是 XGBoost 的初学者

【问题讨论】:

  • X.values[:, 1].astype(str) 到底是什么?你能提供print(X)的结果吗?
  • @Chris,附上X的截图

标签: python python-3.x machine-learning xgboost


【解决方案1】:

不要分配回X.values。使用X.iloc

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X.iloc[:, 1] = le.fit_transform(X.values[:, 1].astype(str))

输出:

   Index  Ship_Mode
0      0          0
1      1          0
2      2          1
3      3          1
4      4          0
5      5          2

【讨论】:

    猜你喜欢
    • 2020-03-04
    • 2015-01-06
    • 2023-03-19
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2014-02-25
    • 2023-03-25
    相关资源
    最近更新 更多