Reading back a saved LGBMClassifier model

一笑奈何 提交于 2019-12-08 15:33:45

问题


I trained a LGBMClassifier model and saved in a file in this way:

clf = lgb.LGBMClassifier( ... )
clf.fit(X_train, y_train, **fit_params)
clf.booster_.save_model("model1.txt")

##Predictions
y_pred = clf.predict_proba(X_data, num_iteration=clf.best_iteration_)[:, 1]

Now what I want is to use the saved model for another prediction. But if I do this:

## new predictions:

    clf_fs = lgb.Booster(model_file='model1.txt')
    y_pred2 = clf_fs.predict_proba(X_data2, num_iteration=clf_fs.best_iteration_)[:, 1]

I got an error as

AttributeError: 'Booster' object has no attribute 'predict_proba'

I understand that cls_fs is an object of class Booster and not of a class LGBMClassifier, and that I can use clf_fs.predict(), but how I can get back a LGBMClassifier object from the saved booster file and all its specific attributes?


回答1:


A relevant Github thread on this question, How to create LGBMClassifier from booster?, concludes that (emphasis mine):

Unfortunately, it's impossible. You shouldn't mix up sklearn and standard train interfaces. Please read this issue #1217 and use joblib to save/load your sklearn model.

In the said (now closed) #1217 issue at Github on this, a Microsoft developer working on the project said that, in this case:

booster.predict() actually will return the probabilities

so you shouldn't be needing predict_proba.



来源:https://stackoverflow.com/questions/58698313/reading-back-a-saved-lgbmclassifier-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!