使用OneHotEncoder和拟合d时收到值错误

2024-09-21 03:17:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个作业,我们正在使用scikit中的OneHotEncoder来学习将所有类别打印出来。以下是数据示例和我用于转换它的代码:

      grade sub_grade  short_emp  emp_length_num home_ownership        term
0          B        B2          0              11           RENT   36 months
1          C        C4          1               1           RENT   60 months
2          C        C5          0              11           RENT   36 months
3          C        C1          0              11           RENT   36 months
4          A        A4          0               4           RENT   36 months
5          E        E1          0              10           RENT   36 months

代码:

^{pr2}$

我收到的错误出现在术语列上:

ValueError: could not convert string to float: ' 36 months'

Tags: 数据代码示例home作业scikit类别length
2条回答

scikit learn的OneHotEncoder支持0.20.0中的字符串。在

OneHotEncoder不支持字符串功能。您必须先将它们转换为整数,例如使用LabelEncoder。另一个选择是对所有列使用LabelBinarizer。在

How to do Onehotencoding in Sklearn Pipeline。在

相关问题 更多 >

    热门问题