Skip to main content

How to Load a scikit-learn Model from MLflow with mlflow.sklearn.load_model

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

mlflow.pyfunc.load_model gives every model the same plain interface. When you need the scikit-learn model itself, to read its coefficients, call predict_proba or train it further, mlflow.sklearn.load_model loads it back as exactly the object you logged. This guide does it, with real output from MLflow 3.18.0.

Syntax​

Syntax
model = mlflow.sklearn.load_model(model_uri)
PartWhat it means
model_uriWhere the model is, such as models:/<model ID>
modelThe scikit-learn estimator or pipeline that was logged, with all its methods and attributes

Prerequisites​

  • An MLflow tracking server at http://127.0.0.1:8080. MLflow's tracking server guide shows how to start one.
  • mlflow and scikit-learn in your project, for example with uv add mlflow scikit-learn pandas.

Step 1: Log a Model to Load​

This script logs a linear regression on scikit-learn's diabetes data into the experiment named on the command line, and prints its URI:

log_diabetes_model.py
import sys

import mlflow
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment(sys.argv[1])

X, y = load_diabetes(return_X_y=True, as_frame=True)
model = LinearRegression().fit(X, y)

with mlflow.start_run():
model_info = mlflow.sklearn.log_model(model, name="diabetes-model", input_example=X.head(3))

print("Model URI:", model_info.model_uri)
Terminal
uv run python log_diabetes_model.py sklearn-load-demo
Output (last lines)
🏃 View run unique-cat-286 at: http://127.0.0.1:8080/#/experiments/13/runs/71a5514f407946449891ff731274a879
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/13
Model URI: models:/m-2c5c5398baf04025b733cbcce83ef5e0

Step 2: Load It as a scikit-learn Model​

load_sklearn.py loads the model and uses something only the scikit-learn object has, its coef_ attribute, before predicting:

The load_sklearn.py script in a macOS-style code window: it loads the model with mlflow.sklearn.load_model from the URI on the command line, prints the class name, the coefficient for the bmi column and predictions for three rows.

load_sklearn.py
import sys

import mlflow
from sklearn.datasets import load_diabetes

mlflow.set_tracking_uri("http://127.0.0.1:8080")

model = mlflow.sklearn.load_model(sys.argv[1])

X, _ = load_diabetes(return_X_y=True, as_frame=True)
print(type(model).__name__)
print("bmi coefficient:", model.coef_[list(X.columns).index("bmi")].round(1))
print(model.predict(X.head(3)).round(1))
Terminal
uv run python load_sklearn.py models:/m-2c5c5398baf04025b733cbcce83ef5e0
Output
LinearRegression
bmi coefficient: 519.8
[206.1 68.1 176.9]

The output of load_sklearn.py in a terminal: LinearRegression, the bmi coefficient 519.8, and the predictions 206.1, 68.1 and 176.9.

The loaded object is a LinearRegression, with its learned coefficients: the body mass index weighs 519.8 in this model. Its predictions match what the same model gives as a PyFuncModel.

Which Loader to Use​

You want toUse
Predict, in code that shouldn't care which library trained the modelmlflow.pyfunc.load_model
Have the input checked against the model's signature firstmlflow.pyfunc.load_model
Read attributes such as coef_ or feature_importances_, or call predict_probamlflow.sklearn.load_model

The scikit-learn object is the model itself, so MLflow's signature check doesn't run on its predict.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting MLflow's signature check from the scikit-learn modelscikit-learn answers instead, with its own error for a missing column: ValueError: The feature names should match those that were passed during fit. Feature names seen at fit time, yet now missing: - bmiUse mlflow.pyfunc.load_model when you want MLflow's check
Passing the run's artifact path in MLflow 3The model isn't stored with the run's filesmodels:/<model ID>

Troubleshooting​

SymptomLikely causeFix
Deserializing this model is disallowed...MLFLOW_ALLOW_MODEL_CODE_EXECUTION is falseHow to Refuse to Load Untrusted Models in MLflow

FAQ​

Does it need the trusted types of a skops model?​

No: MLflow reads them from the model's MLmodel file, as How to Save a scikit-learn Model in the skops Format with MLflow shows.

Can I keep training the loaded model?​

It's an ordinary scikit-learn object, so calling fit on it works as usual; log the result as a new model.

Conclusion​

mlflow.sklearn.load_model("models:/m-…") returned the logged LinearRegression itself: its coefficients were there, and it predicted the same values. Use it when you need the scikit-learn object, and mlflow.pyfunc.load_model when you only predict.

Additional Resources​