Skip to content
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

Using XGBClassifier.Predict after load_model causes 'XGBClassifier' object has no attribute '_le' #2073

Closed
paulbir opened this issue Feb 28, 2017 · 7 comments

Comments

@paulbir
Copy link

paulbir commented Feb 28, 2017

For bugs or installation issues, please provide the following information.
The more information you provide, the more easily we will be able to offer
help and advice.

Environment info

Operating System: Windows 10

Compiler: Unofficial windows binaries

Package used (python/R/jvm/C++): python

xgboost version used: 0.6

If you are using python package, please provide

  1. The python version and distribution: version - 2.7.12. distribution - Anaconda
  2. The command to install xgboost if you are not installing from source: python setup.py install

Steps to reproduce

    clf = xgb.XGBClassifier()
    booster = xgb.Booster()
    booster.load_model('bin_model')
    clf._Booster = booster

y_pred = clf.predict(X_test)
3.
AttributeError: 'XGBClassifier' object has no attribute '_le'
in "predict" function on line 485:
return self._le.inverse_transform(column_indexes)

This happens because _le attribute is set in fit function:
self._le = XGBLabelEncoder().fit(y)

If you don't call fit, _le is not set, so exception is reaised.

@mangolzy
Copy link

Environment info

Operating System: linux based on docker

Package used (python/R/jvm/C++): python

xgboost version used: 0.6

If you are using python package, please provide

The python version and distribution: version - 2.7.5

Steps to reproduce

clf = xgb.XGBClassifier()
booster = xgb.Booster()
booster.load_model('bin_model')
clf._Booster = booster

y_pred = clf.predict(X_test)
3.
AttributeError: 'XGBClassifier' object has no attribute '_le'
in "predict" function on line 485:
return self._le.inverse_transform(column_indexes)

This happens because _le attribute is set in fit function:
self._le = XGBLabelEncoder().fit(y)

so what's the solution, basically, if we call load_model, there is no need to call fit, and according to the former post, that's the cause....

@paulbir
Copy link
Author

paulbir commented May 16, 2017

so what's the solution, basically, if we call load_model, there is no need to call fit, and according to the former post, that's the cause

Maybe check if self._le is instantiated in both fit and predict. If not, then create it.

@mangolzy
Copy link

oh, but how to instantiate the parameter inside xgboost predefined function..

I think the problem is because we use XGBClassifier which is sklearn API in the first place. but save_model and dump_model and load_model is actually xgboost's native function.. they are not compatible, and in fact we don't have the corresponding model saving model inside sklearn...... so some use Pickle etc. the third party packages

@paulbir
Copy link
Author

paulbir commented May 16, 2017

I had to use a workaround.

import xgboost as xgb
from sklearn.preprocessing import LabelEncoder

booster = xgb.Booster()
booster.load_model('bin_model')
clf._Booster = booster
clf._le = LabelEncoder().fit(y_test)
y_pred_proba = clf.predict_proba(X_test)
y_pred = clf.predict(X_test)

But assigning _le can be done inside predict function. And it would help avoiding misleading exceptions.

@mangolzy
Copy link

mangolzy commented May 18, 2017

but what if we don't have y_test, the normal scene when we do real test....?
does it mean that i could use some fake y_test value just to complete the initialization schedule?

@paulbir
Copy link
Author

paulbir commented May 18, 2017

Can expected classed be extracted from the booster?

@ghost
Copy link

ghost commented Jun 3, 2017

@mangolzy If you don't have the y_test, you just need the classes:

clf._le = LabelEncoder().fit(['label1', 'label2'])

@tqchen tqchen closed this as completed Jul 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Oct 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants