我必须使用决策树分类器对某些数据进行分类。但是,属性值是字符串,正如我在此处找到的那样,它说字符串不能用作输入。因此我对字符串使用了整数编码。在这篇文章中,我发现传递整数编码的数据可能会导致错误的答案,因为 sklearn 假设数据之间有顺序。所以,唯一的出路是使用OneHotEncoder模块。使用OneHotEncoder模块增加了特征的数量(例如,如果有一个带有值的属性“价格” ['high','med','low'],单热编码将导致包含与实际属性“价格”相关的 3 个属性;这些可以解释为['price-high','price-med', 'price-low']和属性值将是 1 或 0,具体取决于数据),这是我不想要的,因为我必须以某种格式打印决策树,这需要原始特征(例如,我需要“价格”)。有没有办法解决这个问题?
1 回答

精慕HU
TA贡献1845条经验 获得超8个赞
我认为pd.get_dummies这会很有用,因为您想在创建单热向量时跟踪原始特征名称。
例子:
df = pd.DataFrame({'price': ['high', 'medium', 'high', 'low'], 'some_feature': ['b', 'a', 'c','a']})
pd.get_dummies(df,columns=['price','some_feature'])
price_high price_low price_medium some_feature_a some_feature_b some_feature_c
0 1 0 0 0 1 0
1 0 0 1 1 0 0
2 1 0 0 0 0 1
3 0 1 0 1 0 0
当将此数据框提供给决策树时,您可以更好地理解!
添加回答
举报
0/150
提交
取消