TeamHG-Memex / eli5

A library for debugging/inspecting machine learning classifiers and explaining their predictions
http://eli5.readthedocs.io
MIT License
2.76k stars 334 forks source link

explain_prediction_lightgbm doesnt work for microsoft lightgbm #415

Open CynthiaLong opened 2 years ago

CynthiaLong commented 2 years ago

When I call the function explain_prediction_lightgbm on my lightgbm booster model, eli5.lightgbm.explain_prediction_lightgbm(model1, doc=example, feature_names=list(example.index)) where model1 is <class 'lightgbm.basic.Booster'> , example is <class 'pandas.core.series.Series'>, and list(example.index) is a list of string of the data feature names

I encounter the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-199da8311346> in <module>()
----> 1 eli5.lightgbm.explain_prediction_lightgbm(model1, doc=example, feature_names=list(example.index))

/opt/xxx/software/anaconda3/lib/python3.6/site-packages/eli5/lightgbm.py in explain_prediction_lightgbm(lgb, doc, vec, top, top_targets, target_names, targets, feature_names, feature_re, feature_filter, vectorized)
    109     """
    110 
--> 111     vec, feature_names = handle_vec(lgb, doc, vec, vectorized, feature_names)
    112     if feature_names.bias_name is None:
    113         # LightGBM estimators do not have an intercept, but here we interpret

/opt/xxx/software/anaconda3/lib/python3.6/site-packages/eli5/sklearn/utils.py in handle_vec(clf, doc, vec, vectorized, feature_names, num_features)
    260         vec, feature_names, coef_scale=None, with_coef_scale=False)
    261     feature_names = get_feature_names(
--> 262         clf, vec, feature_names=feature_names, num_features=num_features)
    263     return vec, feature_names
    264 

/opt/xxx/software/anaconda3/lib/python3.6/site-packages/eli5/sklearn/utils.py in get_feature_names(clf, vec, bias_name, feature_names, num_features, estimator_feature_names)
     93             return FeatureNames(estimator_feature_names, bias_name=bias_name)
     94 
---> 95     num_features = num_features or get_num_features(clf)
     96     if isinstance(feature_names, FeatureNames):
     97         if feature_names.n_features != num_features:

/opt/xxx/software/anaconda3/lib/python3.6/site-packages/eli5/sklearn/utils.py in get_num_features(estimator)
    211     else:
    212         raise ValueError("Can't figure out feature vector size for %s" %
--> 213                          estimator)
    214 
    215 

ValueError: Can't figure out feature vector size for <lightgbm.basic.Booster object at 0x7fa5115826a0>
az0 commented 2 years ago

FWIW, it works for me

eli5.show_prediction(clf,this_row, feature_names=feature_names, show_feature_values=True, top=20)

clf=<class 'lightgbm.basic.Booster'> this_row=<class 'pandas.core.series.Series'> feature_names=<class 'list'>

LightGBM version: 3.3.3 eli5 version: 0.13.0