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 Set the Parameters of a Pipeline Step in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Once a model sits inside a Pipeline, its settings have longer names: the step's name, two underscores, then the setting. model__C is the C setting of the step named model. The same names work to read, change and tune settings. This guide lists them, changes one, and tunes one with GridSearchCV, with real output from scikit-learn 1.9.1.

How to Chain Preprocessing and a Model with a Pipeline in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A model rarely takes raw data: text has to be encoded and numbers sometimes scaled, the same way for training and for every prediction afterwards. A scikit-learn Pipeline joins those preparation steps and the model into one object, so fit and predict can't drift apart. This guide builds a tiny fruit classifier that takes raw rows, with real output from scikit-learn 1.9.1.

How to Transform Only Some Columns with ColumnTransformer in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Real tables mix columns that need work, such as words to encode, with columns that are fine as they are, such as counts. ColumnTransformer applies each step to the columns you name and, with remainder="passthrough", keeps the rest untouched. This guide one-hot encodes the city of three flats and passes their number of rooms through, with real output from scikit-learn 1.9.1.

How to One-Hot Encode a Column with OneHotEncoder in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Models do arithmetic, so a column of words such as red, green and blue has to become numbers first. One-hot encoding gives every word its own column, holding 1 where a row has that word and 0 elsewhere. scikit-learn's OneHotEncoder does it, and remembers the words it saw so new data is encoded the same way. This guide encodes a small column of colors, with real output from scikit-learn 1.9.1.

How to Tune Hyperparameters with GridSearchCV in scikit-learn

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A model's settings, its hyperparameters, change how well it learns, and the best values are rarely obvious. GridSearchCV tries every combination you list, scores each one with cross-validation, and keeps the best. This guide tunes the number of neighbors of a k-nearest-neighbors classifier on the iris flowers, with real output from scikit-learn 1.9.1.

MLflow Tutorial for Machine Learning in Python — Part 4: Search and Filter Runs

· 26 min read
Jagdish Kumawat
Founder @ Dewiride

After three parts, the house price experiment holds a dozen runs, and the questions change from "did it log?" to "which one is best, and why?". MLflow answers them with search: filters on metrics, parameters, status and datasets, in the UI and from Python. In this part you'll search the experiment's runs, add a command that ranks the finished runs by validation error, let the server find runs by name, and clear out a run that never finished.

MLflow Tutorial for Machine Learning in Python — Part 3: Log Artifacts and Datasets

· 29 min read
Jagdish Kumawat
Founder @ Dewiride

Part 2's runs record numbers: scores at every step and the computer's load. Two questions those numbers can't answer: which data did this run learn from, and what did it produce besides scores? In this part every run records its training, validation and test data as MLflow datasets, and saves three files with itself: which features the forest relies on, every setting it used, and every prediction it got wrong.

How to See Every Setting of a scikit-learn Model with get_params

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

You usually set two or three of a model's settings and leave the rest at their defaults, so the code doesn't show the full picture. get_params() does: it returns every setting of a scikit-learn model, chosen or default, as a dictionary you can print, check or save. This guide lists all 18 settings of a random forest, with real output from scikit-learn 1.9.1.

How to Get Feature Importances from a Random Forest in scikit-learn

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A trained random forest can tell you which inputs it leaned on most. feature_importances_ holds one number per input, and feature_names_in_ holds the inputs' names in the same order. This guide trains a forest on scikit-learn's California Housing data, reads both, and turns them into a sorted table, with real output from scikit-learn 1.9.1.

MLflow Tutorial for Machine Learning in Python — Part 2: Log Metrics Over Time and System Metrics

· 38 min read
Jagdish Kumawat
Founder @ Dewiride

Part 1 logged each score once, after training was over. That hides the question every model raises: was more training still helping? In this part the forest grows in stages, MLflow records its score after every stage, and the curves show where extra trees stop paying off. MLflow also records how hard the computer worked while it trained.