Skip to main content

60 posts tagged with "scikit-learn"

scikit-learn, the Python Library for Machine Learning Models, Data Splits and Metrics

View All Tags

How to Save a scikit-learn Model in the skops Format with MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

MLflow can save a scikit-learn model in three formats: pickle, cloudpickle and skops. Only skops checks what it builds when the model is loaded, which is why scikit-learn and MLflow recommend it. For most models it needs nothing from you; for models that hold a type skops doesn't trust by default, you list that type once, when logging. This guide logs such a model, with real output from MLflow 3.18.0.

How to Make an MLflow Model Return Probabilities

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Load a scikit-learn classifier from MLflow as a Python function and predict gives you labels: 0, 1, 2. For ranking, alerts or your own cut-off you usually want the probabilities. log_model(pyfunc_predict_fn="predict_proba") makes the logged model's predict return them, wherever it's loaded or served. This guide does it, with real output from MLflow 3.18.0.

How to Add an Input Example to an MLflow Model

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

An input example is a few rows of real input, saved with the model. Anyone who loads the model later sees exactly what it expects, MLflow infers the signature from it, and it writes a ready-made request body for the model's REST API. One argument to log_model does all three. This guide adds one, with real output and files from MLflow 3.18.0.

How to Add a Signature to an MLflow Model

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A model's signature is its contract: the columns it expects, their types, and what it returns. MLflow saves it with the model and checks every prediction's input against it, so wrong data is refused with a clear message instead of a wrong answer. mlflow.models.infer_signature builds one from your data. This guide adds one to a scikit-learn model, with real output from MLflow 3.18.0.

How to Log a scikit-learn Model in MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Metrics tell you how good a model was; logging the model itself keeps the model, so you can load it, compare it and deploy it later. mlflow.sklearn.log_model saves a trained scikit-learn model to MLflow, and in MLflow 3 every logged model gets its own ID and its own page. This guide logs one, with real output from MLflow 3.18.0.

How to Get Class Probabilities with predict_proba in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A classifier's predict gives you one answer per sample, the class it picks. predict_proba tells you how sure it is: a probability for every class. That's what you need to rank customers by how likely they are to buy, or to pick your own cut-off. This guide compares the two on three flowers, with real output from scikit-learn 1.9.1.

How to Find the Untrusted Types in a scikit-learn Model with skops

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

skops won't load a model file that holds a type it doesn't trust by default, and some scikit-learn models hold one. skops.io.get_untrusted_types lists those types before you load anything, so you can review each one and pass only those you trust to load. This guide does it with a gradient-boosted tree model, with real output from skops 0.16.0 and scikit-learn 1.9.1.

How to Save and Load a scikit-learn Model with skops

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A trained model lives in memory. To use it tomorrow, or on another computer, you save it to a file. Python's usual way, pickle, can run code hidden in the file while loading it. skops saves scikit-learn models in its own format and only builds objects it trusts when it loads them. This guide saves a model and loads it back, with real output from skops 0.16.0 and scikit-learn 1.9.1.

Fixed: MLflow Signature Says long for a predict_proba Model

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

I logged a classifier that returns probabilities, with pyfunc_predict_fn="predict_proba" and an input example, and MLflow gave it a signature whose output is [long (required)]: one whole number per row, a class label. The model returns three probabilities per row. MLflow infers a scikit-learn model's signature from predict, whatever pyfunc_predict_fn says. Inferring the signature myself, from predict_proba, and passing it to log_model fixed it.

Fixed: "MlflowException: The saved sklearn model references untrusted types"

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

I logged a gradient-boosted scikit-learn model with mlflow.sklearn.log_model, a call that had worked for a linear regression, and it failed with mlflow.exceptions.MlflowException: The saved sklearn model references untrusted types. MLflow 3.18.0 saves scikit-learn models in the skops format by default, then loads the file back to make sure it can. This model holds a type skops doesn't trust unless you say so. Passing that type, reviewed, in skops_trusted_types fixed it.