通过 DecisionTreeClassifier sklearn 分箱数据?

use*_*396 5 python entropy decision-tree binning scikit-learn

假设我有一个数据集:

    X     y
   20     0
   22     0
   24     1
   27     0
   30     1
   40     1
   20     0
   ...
Run Code Online (Sandbox Code Playgroud)

我尝试通过最小化熵将 X 离散化为几个容器。所以我做了以下事情:

clf = tree.DecisionTreeClassifier(criterion = 'entropy',max_depth = 4)
clf.fit(X.values.reshape(-1,1),y.values)

threshold = clf.tree_.threshold[clf.tree_.threshold>-2]
threshold = np.sort(threshold)
Run Code Online (Sandbox Code Playgroud)

“阈值”应该给出分割点,这是数据分箱的正确方法吗?

有什么建议么?

Yar*_*ron 2

首先,你的做法是正确的。

有多种方法可以对数据进行分类:

  1. 基于列的值(例如:在列值的最小值和最大值之间将列分为 10 个相等的组)。
  2. 基于列值的分布,例如,基于列的十分位数,它可能是 10 组(最好使用 pandas.qcut )
  3. 基于目标,就像你一样。我发现这个博客与您相关,我认为您寻找最佳分割的方法效果很好https://towardsdatascience.com/discretising-using-decision-trees-21910483fa4b