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

MLflow Tutorial for Machine Learning in Python — Part 5: Autologging and Hyperparameter Tuning with Nested Runs

· 38 min read
Jagdish Kumawat
Founder @ Dewiride

So far you've logged every setting and score by hand. MLflow can do most of that logging itself: turn on autologging, and every fit records its settings, scores and charts. It also records a whole hyperparameter search as one parent run, with a child run for each set of settings it tried. In this part you'll start the series' second project, a model that predicts which bank customers sign up for a term deposit, and let MLflow record its search for the best settings.

How to Log Your Own Metrics in an MLflow Autologged Run

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Autologging records what it knows: the model's settings and its scores on the training data. Your own numbers, such as a score on a test set, and your own labels still need a log call, and they belong in the same run. The way to get them there is to start the run yourself, so autologging and your calls share it. This guide adds a test score and a tag to an autologged run, with real output from MLflow 3.18.0.

How to Stop MLflow Autologging from Saving Models

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

By default, autologging saves the trained model with every fit: its file, its environment files and its input format. That's what you want for the model you'll deploy, but not for every experiment along the way, and a hyperparameter search saves two models per search. mlflow.sklearn.autolog(log_models=False) keeps everything else and leaves the models out. This guide runs the same training with and without it, with real output from MLflow 3.18.0.

How to Log Test Metrics Automatically with MLflow Autologging

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Autologging's own metrics are measured on the training data. What matters is the score on data the model never saw, and MLflow can log that by itself too: with autologging on, when you call a sklearn.metrics function on a model's predictions, MLflow logs the result to the model's run. MLflow calls these post training metrics. This guide logs a test error without a log call, then shows the import order that silently breaks it, with real output from MLflow 3.18.0.

How to Log Every Candidate of a Grid Search in MLflow with max_tuning_runs

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

When autologging records a GridSearchCV search, it creates child runs only for the 5 best candidates by default, ranked by cross-validation score. The others still appear in the parent's cv_results.csv, but not as runs you can sort and compare. mlflow.sklearn.autolog(max_tuning_runs=...) sets that number, or removes the limit with None. This guide runs the same 8-candidate search both ways, with real output from MLflow 3.18.0.

How to Log a GridSearchCV Search in MLflow with Parent and Child Runs

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A hyperparameter search trains the same model many times, once per combination of settings. With autologging on, MLflow records a GridSearchCV search as one tidy group: a parent run for the search as a whole, with the best settings, and a child run for each candidate, with its cross-validation scores. This guide records a four-candidate search, with real output from MLflow 3.18.0.

How to Turn On Autologging in MLflow

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

Writing mlflow.log_param and mlflow.log_metric for every setting and score gets long. MLflow can do it for you: turn on autologging before you train, and every fit records its parameters, training metrics, tags, dataset and model on its own. This guide turns it on for a scikit-learn model, then looks at what it recorded, with real output from MLflow 3.18.0.

How to Calculate ROC AUC in scikit-learn

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

ROC AUC measures how well a model ranks: the chance that it gives a randomly picked positive row a higher score than a randomly picked negative one. 1.0 is a perfect ranking and 0.5 is no better than a coin. Because it judges the ranking, not a yes/no cut-off, it suits problems where positives are rare. scikit-learn computes it with roc_auc_score; this guide scores a tiny example and a real model, with real output from scikit-learn 1.9.1.

How to Keep the Class Balance in a Train-Test Split with stratify

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

When only 1 row in 10 is a "yes", a random split can put 3 of them in the test set, or none at all, and the test score then says more about luck than about the model. train_test_split(..., stratify=y) splits so that both parts keep the same share of each class. This guide splits 100 rows with 10 yeses five times each way, with real output from scikit-learn 1.9.1.

How to Train a Gradient Boosting Classifier with HistGradientBoostingClassifier

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Gradient boosting builds many small decision trees one after another, each correcting the mistakes of the trees before it, and it's among the strongest models for tables of data. HistGradientBoostingClassifier is scikit-learn's fast version: it groups each column's values into bins first, so it trains quickly even on large tables. This guide trains it on a medical yes/no problem, with real output from scikit-learn 1.9.1.