gpt4 book ai didi

python - 为什么我不能从子类访问 XGBClassifier feature_importances_?

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:30 25 4
gpt4 key购买 nike

我一直在为 XGBClassifier 的这种怪异行为而烦恼,它应该像 RandomForestClassifier 那样表现得很好:

import xgboost as xgb 
from sklearn.ensemble import RandomForestClassifier

class my_rf(RandomForestClassifier):
def important_features(self, X):
return super(RandomForestClassifier, self).feature_importances_

class my_xgb(xgb.XGBClassifier):
def important_features(self, X):
return super(xgb.XGBClassifier, self).feature_importances_

c1 = my_rf()
c1.fit(X,y)
c1.important_features(X) #works

虽然这段代码失败了:(

c2 = my_xgb()
c2.fit(X,y)
c2.important_features(X) #fails with AttributeError: 'super' object has no attribute 'feature_importances_'

我盯着两个代码位看,它们在我看来是一样的!我错过了什么??对不起,如果这是菜鸟,python OOP 之谜就在我之外。

rf-code

xgb-code

编辑:

如果我使用 vanilla xgb,没有继承,一切都很好:

import xgboost as xgb
print "version:", xgb.__version__
c = xgb.XGBClassifier()
c.fit(X_train.as_matrix(), y_train.label)
print c.feature_importances_[:5]

version: 0.4
[ 0.4039548 0.05932203 0.06779661 0.00847458 0. ]

最佳答案

据我所知,feature_importances_ 未在 XGBoost 中实现。您可以使用诸如排列特征重要性之类的东西来推出自己的东西:

import random
from sklearn.cross_validation import cross_val_score

def feature_importances(clf, X, y):
score = np.mean(cross_val_score(clf, X,y,scoring='roc_auc'))
importances = {}
for i in range(X.shape[1]):
X_perm = X.copy()
X_perm[:,i] = random.sample(X[:,i].tolist(), X.shape[0])
perm_score = np.mean(cross_val_score(clf, X_perm , y, scoring='roc_auc'))
importances[i] = score - perm_score

return importances

关于python - 为什么我不能从子类访问 XGBClassifier feature_importances_?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36844083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com