krzjoa / scikit-gbm

scikit-learn compatible tools to work with GBM models
https://scikit-gbm.readthedocs.io
MIT License
2 stars 1 forks source link

GBMDiscretizer fails when estimator is XGBClassifier #17

Open kyle-clara opened 2 months ago

kyle-clara commented 2 months ago

I get the following error with xgboost==1.6.2 and scikit-gbm==0.2.1:

    109 def xgboost_trees_to_dataframe(obj):
--> 110     trees_df = obj.get_booster().trees_to_dataframe()
    112     # Rename columns
    113     trees_df = \
    114         trees_df.rename(columns={
    115             'Tree'     : 'tree_index',
   (...)
    124             'Category' : 'category' 
    125         })

AttributeError: '_XgboostWrapper' object has no attribute 'get_booster'

Reproducible example:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np

iris = load_iris()
data = pd.DataFrame(
    data= np.c_[iris['data'], iris['target']],
    columns= iris['feature_names'] + ['target']
)
data.columns = data.columns.str[:-5]
data.columns = data.columns.str.replace(' ', '_')

X, y = data.iloc[:, :4], data.iloc[:, 4:]
X_train, X_test, y_train, y_test = \
    train_test_split(X, y, test_size=0.3, random_state=0)
X_cols = X.columns.tolist()

gbm_discretizer = GBMDiscretizer(XGBClassifier(verbose=-1), X_cols, one_hot=False)
gbm_discretizer.fit_transform(X_train, y_train)
madMathematician971 commented 6 days ago

I am facing the same issue as @kyle-clara. Also, if I try to use GradientBoostingRegressor\Classifier instead of XGBoost I get a different error:

image

I believe this library was written for an earlier version of XGBoost and sklearn as I found multiple posts on StackOverflow mentioning same errors with get_booster attr missing which were resolved by downgrading xgboost.

@krzjoa Could you please specify versions of sklearn and xgboost libraries this package was written for and (preferably) update the code to work with the latest versions? Thank you

madMathematician971 commented 6 days ago

@kyle-clara My fix in #18 is verified, you can install the forked version from git until the author merges my MR

image