Learning from Data

In traditional programming, a developer writes explicit rules:

IF temperature > 38°C AND symptoms INCLUDE cough
  THEN flag as potential respiratory illness

In machine learning, we show the system thousands of examples and let it discover the rules itself.

flowchart LR

        direction LR
        R["Rules"] --> P1["Program"]
        D1["Data"] --> P1
        P1 --> O1["Output"]
 
        direction LR
        D2["Data"] --> M["ML Algorithm"]
        O2["Output\n(labels)"] --> M
        M --> Mo["Model\n(learned rules)"]
        

Traditional software follows explicit rules written by programmers. ML systems learn rules from data.

Training with Data

The ML workflow works in four steps:

  1. Choose the appropriate model
  2. Feed the algorithm examples
  3. Adjust internal parameters to reduce prediction errors
  4. Evaluate on unseen data — does it generalise?
Code
# Load libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Create synthetic data
np.random.seed(42)
hours = np.random.uniform(1, 10, 40).reshape(-1, 1)
scores = 5 * hours.flatten() + np.random.normal(0, 5, 40) + 30

# Fit a linear regression model
model = LinearRegression()
model.fit(hours, scores)
x_line = np.linspace(1, 10, 100).reshape(-1, 1)

# Plot the data and the learned model
fig, ax = plt.subplots(figsize=(7, 4))
ax.scatter(hours, scores, alpha=0.7, color="#4C72B0", label="Training examples")
ax.plot(x_line, model.predict(x_line), color="#C44E52", linewidth=2, label="Learned model")
ax.set_xlabel("Hours studied")
ax.set_ylabel("Exam score")
ax.set_title("ML learns a relationship from examples")
ax.legend()
plt.tight_layout()
plt.show()

A simple linear model fit to data. The red line is the model’s learned rule. It wasn’t programmed — it was inferred from data