Cha*_*eae 11 python decision-tree feature-selection scikit-learn
我试图了解如何计算sci-kit学习中的决策树的特征重要性.之前已经问过这个问题,但我无法重现算法提供的结果.
例如:
from StringIO import StringIO
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree.export import export_graphviz
from sklearn.feature_selection import mutual_info_classif
X = [[1,0,0], [0,0,0], [0,0,1], [0,1,0]]
y = [1,0,1,1]
clf = DecisionTreeClassifier()
clf.fit(X, y)
feat_importance = clf.tree_.compute_feature_importances(normalize=False)
print("feat importance = " + str(feat_importance))
out = StringIO()
out = export_graphviz(clf, out_file='test/tree.dot')
Run Code Online (Sandbox Code Playgroud)
导致特征重要性:
feat importance = [0.25 0.08333333 0.04166667]
Run Code Online (Sandbox Code Playgroud)
并给出以下决策树:
现在,这个答案对一个类似问题建议的重要性计算公式为
其中G是节点杂质,在这种情况下是基尼杂质.据我所知,这是杂质减少.但是,对于功能1,这应该是:
这个答案表明重要性由到达节点的概率加权(通过到达该节点的样本的比例来近似).同样,对于功能1,这应该是:
两个公式都提供了错误的结果.如何正确计算特征重要性?
Sel*_*can 17
我认为功能重要性取决于实现,所以我们需要查看scikit-learn的文档.
功能重要性.功能越高,功能越重要.特征的重要性计算为该特征带来的标准的(标准化的)总减少量.它也被称为基尼的重要性
减少或加权信息增益定义为:
加权杂质减少方程式如下:
N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity)其中N是样本总数,N_t是当前节点的样本数,N_t_L是左子节点中的样本数,N_t_R是右子节点中的样本数.
由于每种特征在您的情况下使用一次,因此特征信息必须等于上面的等式.
对于X [2]:
feature_importance = (4 / 4) * (0.375 - (0.75 * 0.444)) = 0.042
对于X [1]:
feature_importance = (3 / 4) * (0.444 - (2/3 * 0.5)) = 0.083
对于X [0]:
feature_importance = (2 / 4) * (0.5) = 0.25
小智 6
可以在树的不同分支中使用单个特征,那么特征重要性就是它在减少杂质方面的总贡献。
feature_importance += number_of_samples_at_parent_where_feature_is_used\*impurity_at_parent-left_child_samples\*impurity_left-right_child_samples\*impurity_right
Run Code Online (Sandbox Code Playgroud)
杂质是基尼系数/熵值
normalized_importance = feature_importance/number_of_samples_root_node(total num of samples)
Run Code Online (Sandbox Code Playgroud)
在上面例如:
feature_2_importance = 0.375*4-0.444*3-0*1 = 0.16799 ,
normalized = 0.16799/4(total_num_of_samples) = 0.04199
Run Code Online (Sandbox Code Playgroud)
如果feature_2在其他分支中使用,则计算它在每个这样的父节点上的重要性并总结这些值。
当我们使用图中看到的截断值时,计算的特征重要性与库返回的特征重要性存在差异。
相反,我们可以使用分类器的“tree_”属性访问所有必需的数据,该属性可用于探测使用的特征、阈值、杂质、每个节点的样本数等。
例如:clf.tree_.feature给出使用的功能列表。负值表示它是叶节点。
同样clf.tree_.children_left/right给出clf.tree_.feature左右孩子的索引
使用上述遍历树并使用相同的索引clf.tree_.impurity & clf.tree_.weighted_n_node_samples来获取每个节点及其子节点的基尼/熵值和样本数。
def dt_feature_importance(model,normalize=True):
left_c = model.tree_.children_left
right_c = model.tree_.children_right
impurity = model.tree_.impurity
node_samples = model.tree_.weighted_n_node_samples
# Initialize the feature importance, those not used remain zero
feature_importance = np.zeros((model.tree_.n_features,))
for idx,node in enumerate(model.tree_.feature):
if node >= 0:
# Accumulate the feature importance over all the nodes where it's used
feature_importance[node]+=impurity[idx]*node_samples[idx]- \
impurity[left_c[idx]]*node_samples[left_c[idx]]-\
impurity[right_c[idx]]*node_samples[right_c[idx]]
# Number of samples at the root node
feature_importance/=node_samples[0]
if normalize:
normalizer = feature_importance.sum()
if normalizer > 0:
feature_importance/=normalizer
return feature_importance
Run Code Online (Sandbox Code Playgroud)
此函数将返回与所返回的完全相同的值 clf.tree_.compute_feature_importances(normalize=...)
根据特征的重要性对特征进行排序
features = clf.tree_.feature[clf.tree_.feature>=0] # Feature number should not be negative, indicates a leaf node
sorted(zip(features,dt_feature_importance(clf,False)[features]),key=lambda x:x[1],reverse=True)
Run Code Online (Sandbox Code Playgroud)