Python Introduction to Machine Learning

Frame a prediction problem, choose features and a target, compare a model against a simple baseline and evaluate it on data it hasn't seen.

Lesson 90 of 107Advanced13 min read

Machine learning (ML) builds programs that learn patterns from examples instead of following hand-written rules. Given past data about bike rentals and the weather, a model can learn to estimate tomorrow's demand. In Python, the standard library for classical ML is scikit-learn (python -m pip install scikit-learn), built on NumPy and often used with pandas:

Python
from sklearn.linear_model import LinearRegression
X = [[18], [22], [25], [15]]          # feature: temperature in °C
y = [120, 150, 175, 95]               # target: rentals that day
model = LinearRegression().fit(X, y)
print(round(model.predict([[20]])[0]))  # estimated rentals at 20 °C

This lesson explains the ideas every later ML lesson relies on: features and targets, the main types of learning, why every model needs a baseline, and how held-out data shows whether a model will work on new cases. The examples predict demand for a bike rental scheme.

Features and targets

In supervised learning each example has:

  • Features (inputs, usually called X): information available at prediction time, such as temperature, rainfall, day of week, whether it's a holiday.
  • A target (output, y): what you want to predict, such as the number of rentals that day.
Python
import pandas as pd
days = pd.DataFrame({"temp": [18, 22, 9], "rain_mm": [0, 2, 11],
                     "weekend": [1, 0, 0], "rentals": [210, 160, 40]})
X = days[["temp", "rain_mm", "weekend"]]
y = days["rentals"]

X is a 2-D table, one row per example and one column per feature; y is a 1-D column with one target per row.

The most damaging mistake is leakage: a feature that wouldn't be known at prediction time. If "bikes returned late that day" is a feature, the model looks brilliant in testing, because that column is recorded after the rentals happen, and useless in practice. For every feature, ask: would I have this value when I need the prediction?

Learning types

TypeHas a target?Typical tasksExample
Supervised: regressionyes, a numberpredict quantitiesrentals tomorrow
Supervised: classificationyes, a categorypredict labelswill a bike need repair: yes/no
Unsupervisednofind structuregroup stations with similar usage
Reinforcementreward signallearn actionsrebalance bikes between stations over time

Supervised learning needs labelled history: past days with the actual rental counts. Unsupervised learning, such as k-means clustering, works without labels, but its results need human interpretation: the algorithm finds groups, not their meaning.

Different questions need different evaluation. Regression is judged by how far predictions are from actual numbers (for example, mean absolute error in rentals); classification by how often and in which direction labels are wrong (accuracy, precision, recall); clustering has no right answer to compare with, so it's judged by usefulness and stability.

Baselines

Before trusting any model, compare it with a baseline: the simplest sensible prediction. For rentals, "predict the average of past days" or "predict the same as the same weekday last week". If a complex model can't beat that, it isn't adding value.

Python
from statistics import mean
past = [120, 150, 175, 95, 160]
baseline = mean(past)                       # 140
errors = [abs(actual - baseline) for actual in [130, 170]]
print(baseline, mean(errors))               # 140 20

scikit-learn provides DummyRegressor and DummyClassifier for exactly this purpose. A baseline also exposes misleading metrics: if 95% of bikes never need repair, a "model" that always answers "no repair" scores 95% accuracy while catching no faults at all.

Report model results relative to the baseline: "mean error 18 rentals, against 35 for the average-of-history baseline" tells a reader far more than "mean error 18".

Generalization

A model is only useful if it works on new data. Fitting and evaluating on the same examples measures memory, not understanding: a flexible model can reproduce its training data almost perfectly and still predict new days badly. This is called overfitting.

The fix is to hold data back:

Python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)

Fit only on the training part, then measure error on the test part, which the model never saw. Every choice made while building the model, including which features to use and how to scale them, must be decided without looking at the test set, or it leaks information and makes results look better than they will be in practice.

For data with a time order, such as daily rentals, split by time: train on earlier days and test on later ones. Randomly mixing future days into training lets the model peek at the future. The training and testing and cross-validation lessons go into detail.

Independent practice and a worked check

Task: define a rental prediction question, available features, an outcome, a baseline and a suitable held-out evaluation. Write it as a short plan before any code:

Text
Question:  How many bikes will be rented at the Riverside station tomorrow?
Target:    total rentals at Riverside for the day (a number: regression)
Features:  forecast temperature, forecast rain, day of week, public holiday flag
           (all known the evening before; no same-day returns or sales data)
Baseline:  average rentals on the same weekday over the previous four weeks
Split:     train on January-September, test on October-December (time order)
Metric:    mean absolute error in bikes, reported against the baseline's error

Every feature passes the "known at prediction time" test, and the split respects time order. For the worked check, a learned model is compared with a baseline to check whether complexity improves useful performance.

Reference and next practice

The scikit-learn getting started guide and its common pitfalls page are excellent next reads. Continue with summary statistics for ML and linear regression. Practise with the Python exercises and Python quiz.

Introduction to Machine Learning Summary

  • Features and targets: Features are inputs and a target is the quantity or label a supervised model tries to predict.
  • Learning types: Supervised learning uses targets while unsupervised learning explores structure without those labels.
  • Baselines: A simple baseline gives a model result a useful comparison.
  • Generalization: A model should be assessed on appropriate observations not used for its fitting choices.

Introduction to Machine Learning Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Introduction to Machine Learning in a small program

The simplest possible baseline: predict the average of past days.

Python
from statistics import mean
past_rentals = [12, 18, 15, 19]
baseline = mean(past_rentals)
print("Baseline demand:", baseline)
Output
Baseline demand: 16
Line-by-line explanation
  1. Lines 1 to 3The baseline is the mean of four past rental counts: 64 / 4 = 16.
  2. Line 4Any model for this data should be compared against this number.
Example 2

A first model against a baseline

Python
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error

# features: temperature (°C), rain (mm); target: rentals
X = [[18, 0], [22, 2], [9, 11], [25, 0], [14, 6], [20, 1], [11, 9], [23, 0],
     [16, 3], [27, 0], [8, 14], [19, 2]]
y = [210, 220, 45, 300, 120, 230, 70, 280, 160, 330, 30, 215]

X_train, y_train = X[:8], y[:8]          # earlier days
X_test, y_test = X[8:], y[8:]            # later days, never used for fitting

baseline = DummyRegressor(strategy="mean").fit(X_train, y_train)
model = LinearRegression().fit(X_train, y_train)

for name, est in [("baseline", baseline), ("linear model", model)]:
    error = mean_absolute_error(y_test, est.predict(X_test))
    print(f"{name:<13} mean absolute error: {error:.0f} rentals")
Output
baseline      mean absolute error: 89 rentals
linear model  mean absolute error: 14 rentals
Line-by-line explanation
  1. Lines 10 to 11A time-ordered split: the model is tested on the last four days only.
  2. Lines 13 to 14The baseline always predicts the training mean; the linear model learns from temperature and rain.
  3. Lines 16 to 18Both are scored on the same unseen days, so the comparison is fair.

Common Introduction to Machine Learning Mistakes

Including information from after the event

Don't
X = days[["temp", "rain_mm", "late_returns"]]   # late_returns is recorded after the day ends
Do
X = days[["temp", "rain_mm", "weekend"]]        # all known before the day starts

A leaked feature makes test scores look excellent and real predictions fail.

Evaluating on the training data

Don't
model.fit(X, y)
print(mean_absolute_error(y, model.predict(X)))   # measures memorisation
Do
model.fit(X_train, y_train)
print(mean_absolute_error(y_test, model.predict(X_test)))

Introduction to Machine Learning in Real Projects

Machine learning in practice

Bike-share operators forecast demand to move bikes before stations empty, retailers forecast sales, banks flag unusual transactions and email services filter spam. In each case most of the work is framing the question, collecting reliable data, choosing features available at prediction time and setting up honest evaluation; the model-fitting call is a single line.

scikit-learn covers classical ML for tabular data. Deep learning libraries such as PyTorch and TensorFlow handle images, audio and text at scale, and gradient-boosting libraries such as XGBoost and LightGBM often win on structured data. The principles in this lesson apply to all of them.

Introduction to Machine Learning FAQ

Is Python good for machine learning?

Yes, it's the most widely used language for ML. scikit-learn, pandas and NumPy cover classical ML, while PyTorch and TensorFlow cover deep learning, all with large communities and documentation.

What is the difference between supervised and unsupervised learning?

Supervised learning trains on examples with known answers (targets) to predict answers for new examples. Unsupervised learning has no targets and looks for structure, such as groups or unusual points, in the features alone.

What maths do I need for machine learning with Python?

To use scikit-learn effectively you need basic statistics (mean, spread, distributions), a feel for linear relationships and how to read error metrics. Linear algebra, calculus and probability become important when you study how models work internally or move into deep learning.

What is overfitting?

A model that fits its training data too closely, including its noise, so it performs well on training examples but poorly on new ones. Detect it by comparing training and test error, and reduce it with simpler models, more data or regularisation.

Why do I need a test set?

Because performance on the data a model was trained on overstates how it will do on new data. A held-out test set, untouched while building the model, gives an honest estimate of real-world performance.

Introduction to Machine Learning Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Multiple choiceeasy

Why compare a learned model with a baseline?

Multiple choicemedium

Which review concern is valid for introduction to machine learning?

Introduction to Machine Learning Quiz

2 questions to check what you learned.

  1. Why compare a learned model with a baseline?

    • To avoid all evaluation
    • To check whether complexity improves useful performance
    • Python ignores the distinction
  2. Which statement correctly describes baselines?

    • A simple baseline gives a model result a useful comparison.
    • Only the example output matters, not the rule
    • Every operation has the same behavior