LightGBM - Estimator not fitted error after joblib.load()

Viewed 1056

I fitted a LGBMRegressor() model and even predicted some values. I decided to save it (after fit of course) to use later, but when I try to load the model I'm getting this exception:

Estimator not fitted, call fit before exploiting the model.

I tried to save in 3 different ways:

  • dump(model, 'model.txt')
  • dump(model, 'model.pkl')
  • dump(model, 'model.joblib')

Then I close the IDE and tried to load like this:

  • model = joblib.load('model.allExtensionsMentionedBefore')

When I print(model) I can see the entire model and also it's hyperparameters:

LGBMRegressor(colsample_bytree=0.9596645565436184,
              learning_rate=0.025825537313443326, min_child_samples=72,
              num_leaves=32, random_state=0, silent=True,
              subsample=0.9311181768429686, subsample_freq=1)

However when I try model.predict(X) I'm getting the exception saying that it's not fitted.

What I am doing wrong?

PS: I was able to do to this exact process (save and load) using sklearn MLPRegressor() and it worked perfectly.

2 Answers

lightgbm supports saving and loading models using joblib.

Here's a minimal, reproducible example demonstrating how to do that. I tested this code using Python 3.3.8, lightgbm==3.3.1, joblib==1.0.1, numpy==1.21.0 and scikit-learn==0.24.1.

In a new Python session:

import joblib
import lightgbm as lgb
from sklearn.datasets import make_regression

X, y = make_regression(n_samples=10_000, n_features=10)
reg = lgb.LGBMRegressor(
    n_estimators=10,
    verbose=-1,
)

reg.fit(X, y)
joblib.dump(reg, 'lgb.pkl')

Then, in a separate Python session run from the same working directory:

import joblib
from sklearn.datasets import make_regression

X, y = make_regression(n_samples=1_000, n_features=10)
reg = joblib.load('lgb.pkl')
preds = reg.predict(X)

I tried this same code using lightgbm==3.0.0 (from August 2020) and it worked as well.

I had a similar issue. In my case, the problem stemmed from me saving the models in one conda environment but loading in another. The version differences in lightgbm caused problems. If you are using environments, make sure that you save and load your models from the same environment.

Related