Data Science with Python

The Machine Learning Workflow

Machine Learning · Lesson 13 of 30

Machine learning builds a model from examples, in place of rules written by hand. You show an algorithm data where the answer is known, and it learns a pattern it can apply to new data.

Types of machine learning

Type You have Goal Examples
Supervised Inputs and known answers Predict the answer for new inputs Price prediction, spam detection
Unsupervised Inputs only Find structure Customer segments, anomaly detection
Reinforcement An environment that gives rewards Learn a sequence of actions Game playing, robotics

Supervised learning splits again. Regression predicts a number. Classification predicts a category.

Vocabulary

  • Features: the input columns, by convention called X.
  • Target or label: the column to predict, called y.
  • Training: finding the pattern, also called fitting.
  • Prediction: applying the model to new rows.

Your first model

The iris dataset has measurements of 150 flowers from three species. The first run downloads scikit-learn, which takes a little while.

import pandas as pd   # needed for as_frame=True
from sklearn.datasets import load_iris

iris = load_iris(as_frame=True)
X, y = iris.data, iris.target

print(X.head())
print("species:", list(iris.target_names))
print("rows, features:", X.shape)

Hold back a test set

A model must be judged on data it has not seen. Otherwise you measure how well it memorised, not how well it generalises.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y
)
print("training rows:", len(X_train), " test rows:", len(X_test))

stratify=y keeps the same share of each species in both parts. random_state makes the split repeatable.

Fit, predict, score

Every scikit-learn model has the same three methods.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y
)

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, y_train)

print("predictions:", model.predict(X_test[:5]))
print("actual:     ", y_test[:5])
print("accuracy on the test set:", round(model.score(X_test, y_test), 3))

A k-nearest neighbours model classifies a flower by looking at the five most similar flowers in the training data and taking the majority species.

The workflow

  1. Define the problem. What is predicted, and how will the prediction be used?
  2. Collect and explore the data.
  3. Prepare features: clean, encode and scale.
  4. Split into training and test sets.
  5. Train a simple model first, as a baseline.
  6. Evaluate on the test set with a metric that matches the goal.
  7. Improve: better features, other algorithms, tuning.
  8. Deploy and monitor, because data changes over time.

Start with a baseline

Always compare against something trivial, such as predicting the most common class. A model with 95% accuracy is useless if 95% of the rows belong to one class.

from sklearn.datasets import load_iris
from sklearn.dummy import DummyClassifier
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, stratify=y)

baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
print("baseline accuracy:", round(baseline.score(X_test, y_test), 3))

Practice

Change n_neighbors to 1 and to 50. How does the test accuracy change?