Python Tutorial
Introduction to Machine Learning
In ordinary programming you write the rules: if the amount is over 50,000 and the country is new, flag the payment. In machine learning (ML) you give the computer examples — thousands of past payments labelled "fraud" or "not fraud" — and an algorithm learns the rules from the data, then applies them to new cases it has never seen.
ML powers spam filters, recommendations, price estimates, medical image screening, speech recognition and large language models. This lesson explains the core ideas and vocabulary, the types of machine learning, the standard workflow, and trains your first models with scikit-learn, Python's most widely used library for classical ML (pip install scikit-learn).
Key Vocabulary
- Dataset — a table of examples (rows). Features (
X) are the input columns; the target or label (y) is what we want to predict. - Model — a mathematical function with adjustable parameters. Training (
fit) adjusts them to match the examples; prediction (predict) applies the model to new inputs. - Training set vs test set — we train on one part of the data and measure on data the model has never seen, to estimate real-world performance.
- Overfitting — memorising the training data (great training score, poor test score). Underfitting — a model too simple to capture the pattern.
Types of Machine Learning
- Supervised learning — learn from labelled examples. Regression predicts a number (house price, delivery time); classification predicts a category (spam/not spam, disease type).
- Unsupervised learning — find structure in unlabelled data: clustering (customer segments), dimensionality reduction, anomaly detection.
- Reinforcement learning — an agent learns by trial and reward (games, robotics).
- Deep learning — neural networks with many layers (PyTorch, TensorFlow) for images, audio and text; large language models are deep learning at huge scale.
The ML Workflow and scikit-learn
A typical project: define the problem and metric → collect and explore data → clean and prepare features → split into training and test sets → train candidate models → evaluate and tune → deploy and monitor. scikit-learn gives every algorithm the same interface: create an estimator (model = LinearRegression()), model.fit(X_train, y_train), then model.predict(X_new) and model.score(X_test, y_test). Learn the pattern once and you can try dozens of algorithms by changing one line.
Examples
Your first model: predicting marks from study hours
# pip install scikit-learn
import numpy as np
from sklearn.linear_model import LinearRegression
hours = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]) # features: 2-D (rows x columns)
marks = np.array([35, 45, 50, 58, 65, 72, 80, 88]) # target: 1-D
model = LinearRegression()
model.fit(hours, marks) # learn from examples
print(f"learned rule: marks = {model.coef_[0]:.2f} * hours + {model.intercept_:.2f}")
print("predictions for 2.5, 6 and 9 hours:", model.predict([[2.5], [6], [9]]).round(1))
print("R^2 on the training data:", round(model.score(hours, marks), 3))
learned rule: marks = 7.37 * hours + 28.46
predictions for 2.5, 6 and 9 hours: [46.9 72.7 94.8]
R^2 on the training data: 0.998
Train/test split and a first classifier on the Iris dataset
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris(as_frame=True) # 150 flowers, 4 measurements, 3 species
X, y = iris.data, iris.target
print(X.shape, iris.target_names.tolist())
print(X.head(3))
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42, stratify=y) # keep class balance in both parts
print("train:", X_train.shape[0], "test:", X_test.shape[0])
model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)
print("test accuracy:", round(model.score(X_test, y_test), 3))
new_flower = [[5.9, 3.0, 5.1, 1.8]] # sepal/petal length and width in cm
prediction = model.predict(pd.DataFrame(new_flower, columns=X.columns))[0]
print("predicted species:", iris.target_names[prediction])
(150, 4) ['setosa', 'versicolor', 'virginica']
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
train: 112 test: 38
test accuracy: 0.974
predicted species: virginica
Overfitting in action: training vs test accuracy
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
print("depth train test")
for depth in [1, 2, 3, 5, 10, None]:
tree = DecisionTreeClassifier(max_depth=depth, random_state=0).fit(X_train, y_train)
print(f"{str(depth):>5} {tree.score(X_train, y_train):.3f} {tree.score(X_test, y_test):.3f}")
# An unlimited tree memorises the training data (1.000) but does not do better on new data.
depth train test
1 0.932 0.889
2 0.942 0.906
3 0.980 0.901
5 0.997 0.912
10 1.000 0.906
None 1.000 0.906
Common Mistakes
- Evaluating a model on the same data it was trained on and trusting the score.
- Passing a 1-D array as X — scikit-learn expects a 2-D table of shape (samples, features).
- Forgetting random_state, so splits and results change on every run.
- Reaching for complex models before trying a simple baseline.
- Treating ML as magic: bad or biased data produces bad or biased models.
Key Points to Remember
- ML learns rules from examples: features X, target y.
- Supervised (regression, classification) vs unsupervised (clustering) learning.
- Always hold out a test set; train_test_split with random_state and stratify.
- Every scikit-learn estimator uses fit, predict and score.
- Watch for overfitting: a big gap between training and test scores.
Practice the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Use your local project environment for these examples. Codelab currently runs Python and HTML/CSS/JavaScript; framework examples may need project dependencies.