feature_importances_ 在 ExtraTreesClassifier 中显示为 NoneType :TypeError: 'NoneType' 对象不可迭代

ekt*_*kta 1 machine-learning python-2.7 nonetype scikit-learn

我正在尝试为给定的数据集选择重要的特征(或至少了解哪些特征解释了更多的可变性)。为此,我同时使用了 ExtraTreesClassifier 和 GradientBoostingRegressor - 然后使用:-

clf = ExtraTreesClassifier(n_estimators=10,max_features='auto',random_state=0) # stops after 10 estimation passes, right ?
clf.fit(x_train, y_train)
feature_importance=clf.feature_importances_  # does NOT work - returns NoneType for feature_importance
Run Code Online (Sandbox Code Playgroud)

发布这个我真的很想绘制它们(用于视觉表示) - 甚至是初步的,只是查看重要性的相对顺序和相应的索引

# Both of these do not work as the feature_importance is of NoneType
feature_importance = 100.0 * (feature_importance / feature_importance.max())
indices = numpy.argsort(feature_importance)[::-1]
Run Code Online (Sandbox Code Playgroud)

我发现令人困惑的是 - 如果我要使用如下所示的 GradientBoostingRegressor,我确实得到了 feature_importance 及其索引。我究竟做错了什么 ?

#Works with GradientBoostingRegressor
params = {'n_estimators': 100, 'max_depth': 3, 'learning_rate': 0.1, 'loss': 'lad'}
clf = GradientBoostingRegressor(**params).fit(x_train, y_train)
clf.fit(x_train, y_train)
feature_importance=clf.feature_importances_
Run Code Online (Sandbox Code Playgroud)

其他信息:我有 12 个独立的 vars(x_train) 和一个标签 var(y_train)) 具有多个值(比如 4,5,7)和 type(x_train) 是和 type(feature_importance) 是

致谢:从这篇文章中借用了一些元素 http://www.tonicebrian.com/2012/11/05/training-gradient-boosting-trees-with-python/

YS-*_*S-L 5

初始化ExtraTreeClassifier 时,有一个compute_importances默认为None. 换句话说,您需要初始化ExtraTreeClassifier

clf = ExtraTreesClassifier(n_estimators=10,max_features='auto',random_state=0,compute_importances=True)
Run Code Online (Sandbox Code Playgroud)

以便它将计算特征重要性。

至于GradientBoostedRegressor,则没有这样的选项,并且将始终计算特征重要性。

  • 在确定“良好拟合”时,您可能希望监控验证集上的预测误差而不是训练集上的误差,以避免过度拟合。例如,您可以尝试增加迭代次数,训练误差应该不断减小——但是当验证误差开始增长时您就停止了。 (2认同)