问题
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