eli5 explain_weights_xgboost KeyError: 'bias'

544 Views Asked by At

I am new to xgboost, I trained a model, that works pretty well. Now I am trying to use eli5 to see the weights and I get: KeyError: 'bias'


    ---------------------------------------------------------------------------
    KeyError                                  Traceback (most recent call last)
     in 
          3 clf6 = model6.named_steps['clf']
          4 vec6 = model6.named_steps['transformer']
    ----> 5 explain_weights_xgboost(clf6, vec=vec6)

    ~/dev/envs/env3.7/lib/python3.7/site-packages/eli5/xgboost.py in explain_weights_xgboost(xgb, vec, top, target_names, targets, feature_names, feature_re, feature_filter, importance_type)
         80         description=DESCRIPTION_XGBOOST,
         81         is_regression=is_regression,
    ---> 82         num_features=coef.shape[-1],
         83     )
         84 

    ~/dev/envs/env3.7/lib/python3.7/site-packages/eli5/_feature_importances.py in get_feature_importance_explanation(estimator, vec, coef, feature_names, feature_filter, feature_re, top, description, is_regression, estimator_feature_names, num_features, coef_std)
         35         feature_filter=feature_filter,
         36         feature_re=feature_re,
    ---> 37         num_features=num_features,
         38     )
         39     feature_importances = get_feature_importances_filtered(

    ~/dev/envs/env3.7/lib/python3.7/site-packages/eli5/sklearn/utils.py in get_feature_names_filtered(clf, vec, bias_name, feature_names, num_features, feature_filter, feature_re, estimator_feature_names)
        124         feature_names=feature_names,
        125         num_features=num_features,
    --> 126         estimator_feature_names=estimator_feature_names,
        127     )
        128     return feature_names.handle_filter(feature_filter, feature_re)

    ~/dev/envs/env3.7/lib/python3.7/site-packages/eli5/sklearn/utils.py in get_feature_names(clf, vec, bias_name, feature_names, num_features, estimator_feature_names)
         77     features are named x0, x1, x2, etc.
         78     """
    ---> 79     if not has_intercept(clf):
         80         bias_name = None
         81 

    ~/dev/envs/env3.7/lib/python3.7/site-packages/eli5/sklearn/utils.py in has_intercept(estimator)
         60     if hasattr(estimator, 'fit_intercept'):
         61         return estimator.fit_intercept
    ---> 62     if hasattr(estimator, 'intercept_'):
         63         if estimator.intercept_ is None:
         64             return False

    ~/dev/envs/env3.7/lib/python3.7/site-packages/xgboost/sklearn.py in intercept_(self)
        743                                  .format(self.booster))
        744         b = self.get_booster()
    --> 745         return np.array(json.loads(b.get_dump(dump_format='json')[0])['bias'])
        746 
        747 

    KeyError: 'bias'

Thank you!

1

There are 1 best solutions below

0
On

I had the same issue and fixed it by specifying explicitly the argument booster when creating the estimator:

clf = XGBClassifier(booster='gbtree')