New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XGBClassifier has no attribute get_fscore #757
Comments
Hi! Today I ran into the same issue and this is the workaround I made to get the class XGBFeatureImportances(XGBClassifier):
"""A custom XGBClassifier with feature importances computation.
This class implements XGBClassifier and also computes feature importances
based on the fscores. Implementing feature_importances_ property allow us
to use `SelectFromModel` with XGBClassifier.
"""
def __init__(self, n_features, *args, **kwargs):
"""Init method adding n_features."""
super(XGBFeatureImportances, self).__init__(*args, **kwargs)
self._n_features = n_features
@property
def n_features(self):
"""Number of classes to predict."""
return self._n_features
@n_features.setter
def n_features(self, value):
self._n_features = value
@property
def feature_importances_(self):
"""Return the feature importances.
Returns
-------
feature_importances_ : array, shape = [n_features]
"""
booster = self.booster()
fscores = booster.get_fscore()
importances = np.zeros(self.n_features)
for k, v in fscores.iteritems():
importances[int(k[1:])] = v
return importances I didn't figured out how to get the number of initial features(this would simplify this child class) without implicitly passing it to the class constructor. With this class you can run I would be pleased to add this to |
@davidgasquez Thanks for the code, but I'm not sure how to use this. Could you kindly attach some example usage code as well. Thanks! |
Of course! The idea is to fit a model but clf = XGBFeatureImportances(n_features, ...)
clf.fit(X, y)
importances = clf.feature_importances_ Once you have your classifier working, you can use |
@davidgasquez . Thanks for your code, I tried the way you described and I defined the model in this way fitting it like But i'm getting this run time error Can you help me out with this. |
Hi @krishnateja614, Quick question, which version of Python, Pandas and Sklearn are you using? |
python 2.7.11 |
It seems like one of the latest If you want to get the features importances from your model, I'll advise to just use: fimp_xgb_model = XGBClassifier(base_score=0.5, colsample_bytree=0.5,
gamma=0.017,learning_rate=0.15, max_delta_step=0,
max_depth=8, min_child_weight=3, n_estimators=300,
nthread=-1, objective='binary:logistic', seed=0,
silent=True, subsample=0.9)
fimp_xgb_model.fit(x_train,y_train)
fscore = fimp_xgb_model.booster().get_fscore()
importances = np.zeros(158)
for k, v in fscores.iteritems():
importances[int(k[1:])] = v |
Thank you,will try it out and let you know. |
Hi, @davidgasquez, could you please explain what is 158 means in the above code snippet? |
Hey there @hminle! The line The number 158 is just an example of the number of features for the example specific model. This array will later contain the relative importance of each feature. To get the length of this array, you could use the number of columns in the train set (usually the same as the number of features). |
Is it possible to implement the |
Sorry,I suffer from the error:
in () TypeError: 'str' object is not callable |
Hey @wwwxmu! The best thing to do here would be to print |
TypeError : str object is not callable feat_imp_temp = pd.Series(alg.booster().get_fscore()).sort_values(ascending=False) |
@davidgasquez Thank you! The problem has been solved.I reinstall xgboost, and there are no error any more |
In case anyone stumbles across this from google, you can get the values from: |
Based on @arnehuang´s fantastic contribution. For anyone trying to fix Analytics Vidhya XGBoost lines of code which are wrong you should replace: |
It looks like XGBClassifier in xgboost.sklearn does not have get_fscore, and it does not have feature_importances_ like other sklearn functions do. I think that some kind of feature importance metric should be incorporated into this model, or if it does exist, should be better documented.
The text was updated successfully, but these errors were encountered: