我正在尝试为给定的数据集选择重要的功能(或至少了解哪些功能可以解释更多的可变性)。为此,我同时使用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


发布之后,我真的很想对它们进行绘图(以视觉表示)-甚至是初步的,只是查看重要性的相对顺序和相应的索引

# 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]


我感到困惑的是-如果我按如下方式使用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_


其他信息:我有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/借用了一些元素

最佳答案

初始化ExtraTreeClassifier时,会有一个compute_importances选项,默认为None。换句话说,您需要将ExtraTreeClassifier初始化为

clf = ExtraTreesClassifier(n_estimators=10,max_features='auto',random_state=0,compute_importances=True)


以便计算功能重要性。

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

08-25 04:49