我试图准确了解 scikit-learn 的 RandomForestClassifier 中的 feature_importances 是如何工作的。我设法找到解释大部分过程的有用链接:https ://towardsdatascience.com/the-mathematics-of-decision-trees-random-forest-and-feature-importance-in-scikit-learn-and-spark -f2861df67e3
我只有两个关于 ni_j 方程的问题(节点重要性方程,特征重要性部分的第一个方程):
- 到达节点 j 的加权样本数是什么意思?他们是否通过树运行所有训练样本并计算有多少到达节点 j?如果是这样,为什么需要“加权”这个词?
- 我们能在数学上确定 ni_j >= 0 吗?
编辑:查看 forest.py 的代码,我看到了这个相当神秘的功能:
def feature_importances_(self):
"""Return the feature importances (the higher, the more important the
feature).
Returns
-------
feature_importances_ : array, shape = [n_features]
"""
check_is_fitted(self, 'estimators_')
all_importances = Parallel(n_jobs=self.n_jobs,
backend="threading")(
delayed(getattr)(tree, 'feature_importances_')
for tree in self.estimators_)
return sum(all_importances) / len(self.estimators_)
我可以看到它正在计算每棵树的特征重要性,然后对所有树取平均值。但是它是如何在不调用任何函数来计算样本或计算杂质的情况下计算树上的特征重要性的,我不明白。