AI, machine learning and deep learning · Introductory

First steps with scikit-learn

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

Scikit-learn is a prominent open-source Python library for machine learning that provides a consistent API for preprocessing, training, and evaluating models. The typical workflow involves five steps: feature selection, metric choice, classifier selection, evaluation, and hyperparameter tuning. Effective implementation requires careful data preparation, including splitting data and standardizing features using parameters estimated from the training set.

Intuition

Think of scikit-learn as a standardized toolkit that simplifies the complex process of machine learning. Before you can train a model, you must define the problem clearly to avoid building complex solutions that don't address the right question. The library relies on a strict workflow where data is first prepared—split into training and test sets to ensure you evaluate on unseen data—and then standardized. This standardization is critical because many algorithms, especially those using gradient descent, perform optimally only when features are scaled. By estimating the mean and standard deviation from the training data and applying them to both sets, you ensure the model learns from comparable distributions. Finally, tuning parameters like the learning rate ensures the model converges efficiently without overshooting or moving too slowly.

Key equations

μ$and$σ
(1)

The sample mean and standard deviation estimated by StandardScaler from the training data to standardize features.

Derivation

  1. Define the problem and ensure data can answer the question.
  2. Split the dataset into training and test sets (e.g., 70/30) to evaluate performance on unseen data.
  3. Standardize features using the training data's mean (μ) and standard deviation (σ).
  4. Apply these same parameters to the test set to maintain comparability.
  5. Select a classifier and set hyperparameters like learning rate (η0) and iterations.
  6. Train the model and evaluate performance by comparing predictions to actual labels.

Worked example

Problem

A student is training a Perceptron model using scikit-learn. They calculate the mean and standard deviation from the test set and use these to scale both the training and test data. They also set the learning rate very high. What are the issues?

  1. Using test set statistics for scaling makes the training and test datasets incomparable, violating the principle that the test set must remain unseen during preparation.
  2. A learning rate that is too large causes the algorithm to overshoot the global cost minimum, preventing convergence.
  3. The correct approach is to fit the scaler only on the training data and apply it to both sets, and to tune the learning rate to balance convergence speed and stability.

Answer The student made two common mistakes: using different scaling parameters for the test set than for the training set, and setting the learning rate too large, causing overshooting.

Common mistakes

  • Building complex machine learning solutions without first ensuring they solve the right problem or that the data can answer the question.
  • Using different scaling parameters for the test set than for the training set, which makes the datasets incomparable.
  • Setting the learning rate too large, causing the algorithm to overshoot the global cost minimum, or too small, causing slow convergence.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.