Scikit-Learn (Part 1)

Neil Haddley • October 12, 2026

Machine Learning with Python

AIPythonmachine-learningscikit-learnregressionk-nearest-neighbors

Scikit-Learn is a Python library for machine learning that simplifies building and training models. It provides tools for data preprocessing, model selection, and evaluation.

Key Points

Why Scikit-Learn?

  • Simple syntax with consistent API (application programming interface)
  • Built-in datasets and visualization tools
  • Comprehensive documentation and community support

Core Features:

  1. Preprocessing: Data cleaning, feature scaling, and encoding
  2. Models: Hundreds of algorithms (regression, classification, clustering)
  3. Evaluation: Cross-validation, metrics, and model comparison
  4. Pipeline: Easy workflow management

Quick Example

In the first chapter of the Hands-On Machine Learning book with Scikit-Learn, the authors use an example with GDP per capita data to predict life satisfaction scores:

PYTHON
1from sklearn.linear_model import LinearRegression
2
3# Create and train model
4model = LinearRegression()
5model.fit(X, y) 
6
7# Make prediction for Cyprus
8prediction = model.predict([[37_655.2]])
9print(prediction)

The same task can be accomplished with a different algorithm, such as k-Nearest Neighbors:

PYTHON
1from sklearn.neighbors import KNeighborsRegressor
2
3model = KNeighborsRegressor(n_neighbors=3)
4model.fit(X, y)
5prediction = model.predict([[37_655.2]])
6print(prediction)  

In both cases, Scikit-Learn gives the model the same two calls, fit and predict, so swapping one algorithm for a completely different one changes only two lines. The models here are simple, but the same two calls work for the much more complicated ones in later chapters.

This approach makes it easy to experiment with different algorithms and quickly evaluate their performance.

What training means here

Both examples call model.fit(X, y), which trains the model on the 27 countries in the data. The two models learn in completely different ways.

Linear regression learns two numbers. It works out the straight line that best fits the 27 countries, directly, in one calculation (least squares): an intercept of 3.749, and a slope of 0.0678, the extra life satisfaction for every extra $1,000 of GDP per capita. Predicting for Cyprus is then arithmetic: 3.749 + 0.0678 × 37.655 = 6.30.

k-Nearest Neighbors learns nothing. Its fit just stores the 27 countries. The work happens when it predicts: it finds the three countries whose GDP per capita is closest to Cyprus's $37,655, and averages their life satisfaction.

CountryGDP per capitaLife satisfaction
Israel$38,3417.2
Lithuania$36,7325.9
Slovenia$36,5485.9

(7.2 + 5.9 + 5.9) ÷ 3 = 6.33. The book calls these two kinds model-based learning and instance-based learning.

Neither one trains the way a neural network does. My MiniGPT model starts from 826,433 random numbers and nudges every one of them thousands of times, using backpropagation to find which way each should move. Here there is nothing to nudge: one model is worked out exactly, and the other just remembers.

Running the notebook in Jupyter

Both examples come from the chapter 1 notebook that goes with the book, 01_the_machine_learning_landscape.ipynb. I opened it in JupyterLab and ran the setup cells, then Code example 1-1 and the k-Nearest Neighbors version.

I ran Code example 1-1: it downloads the life satisfaction data, plots it, and trains a linear regression model

I ran Code example 1-1: it downloads the life satisfaction data, plots it, and trains a linear regression model

The scatter plot of GDP per capita against life satisfaction, and the linear model's prediction for Cyprus: 6.30

The scatter plot of GDP per capita against life satisfaction, and the linear model's prediction for Cyprus: 6.30

I swapped in k-Nearest Neighbors with k = 3, which predicts 6.33 for Cyprus

I swapped in k-Nearest Neighbors with k = 3, which predicts 6.33 for Cyprus

Next, Scikit-Learn (Part 2) works through chapter 2's end-to-end project: predicting California house prices, from raw data to a tested model.