Skip to content
Latchkey

scikit-learn "NotFittedError" in CI

scikit-learn ran check_is_fitted and found none of the learned attributes set, so it raised NotFittedError. The estimator reached predict/transform without a successful fit in this process.

What this error means

A test fails with "sklearn.exceptions.NotFittedError: This estimator is not fitted yet. Call 'fit' with appropriate arguments before using this estimator." Often the model was expected to load already-fitted.

python
sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet.
Call 'fit' with appropriate arguments before using this estimator.

Common causes

predict/transform runs before fit in CI

A code path or test skipped training (a guarded branch, a cached artifact that was not produced) so the estimator was never fitted in this run.

A fitted model was never persisted or loaded

CI expected to load a pre-trained model from an artifact that is missing, leaving an unfitted instance.

How to fix it

Fit before predicting, or load a real fitted model

  1. Confirm fit runs (or a fitted model loads) before any predict/transform.
  2. If you persist models, restore the artifact in CI before inference.
  3. Add a test that asserts check_is_fitted passes after setup.
python
from sklearn.utils.validation import check_is_fitted
clf.fit(X_train, y_train)
check_is_fitted(clf)
clf.predict(X_test)

Restore the persisted estimator

Load the fitted estimator from the artifact your training job produced instead of constructing a fresh one.

python
import joblib
clf = joblib.load("artifacts/model.joblib")

How to prevent it

  • Persist fitted models as CI artifacts and load them for inference jobs.
  • Assert check_is_fitted in tests after the fit step.
  • Avoid conditional branches that skip fit in some CI paths.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →