字符串的整数编码并将其用作决策树(sklearn)的输入是否会使拆分属性离散或连续?

Does integer encoding of strings and using this as an input to decision tree (sklearn) makes the splitting attributes discrete or continuous?

我必须使用决策树分类器对某些数据进行分类。但是,属性值是字符串,正如我发现here,它说字符串不能用作输入。因此我对字符串使用了整数编码。

文章中,我发现传递整数编码的数据可能会导致错误的答案,因为 sklearn 假定数据之间的顺序。所以,唯一的出路是使用 OneHotEncoder 模块。

使用 OneHotEncoder 模块增加了特征的数量(例如,如果有一个属性 'price' 的值为 ['high','med','low'],one-hot-encoding 将导致包含 3 个相关的属性到实际属性 'price';这些可以解释为 ['price-high','price-med', 'price-low'] 并且属性值将是 1 或 0,具体取决于数据),我不想要,因为我必须打印决策树以某种需要原始功能的格式(例如,我需要 'price')。

有解决办法吗?

我认为 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

将此数据框输入决策树时,您可以更好地理解!