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.
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
- Confirm
fitruns (or a fitted model loads) before anypredict/transform. - If you persist models, restore the artifact in CI before inference.
- Add a test that asserts
check_is_fittedpasses after setup.
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.
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_fittedin tests after the fit step. - Avoid conditional branches that skip fit in some CI paths.