Python中随机森林回归的特征重要性

时间:2016-08-29 21:46:39

标签: python regression random-forest feature-selection

我正在尝试找出哪些功能对我的预测模型最重要。

目前我正在使用sklearn的内置属性

Model = Model.fit(Train_Features, Labels_Train)
print(Model.feature_importances_)

只是它更像是一种黑盒式方法,我不知道它用什么方法来强调对功能的重要性。有没有更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:2)

在决策树方面,特征重要性不是黑盒子。来自DecisionTreeRegressor的文档:

  

特征的重要性计算为(标准化)总和   减少该特征带来的标准。它也是众所周知的   作为基尼的重要性。

对于森林而言,它只是平均在森林中的不同树木上。查看source code

def feature_importances_(self):
    """Return the feature importances (the higher, the more important the
       feature).
    Returns
    -------
    feature_importances_ : array, shape = [n_features]
    """
    if self.estimators_ is None or len(self.estimators_) == 0:
        raise NotFittedError("Estimator not fitted, "
                             "call `fit` before `feature_importances_`.")

    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_)
相关问题