Machine Learning course menu
The Machine Learning Workflow: From Problem to Deployed Model
The end-to-end machine learning workflow: framing the problem, collecting and exploring data, preparing features, choosing a baseline, training and evaluating models, and deploying and monitoring them, with the scikit-learn fit/predict API.
On this page
Building a useful model is much more than calling fit. Real projects follow a cycle, and skipping steps is the most common reason models disappoint.
The steps#
- Frame the problem. What decision will the model support? What exactly is predicted, and how will success be measured (fewer fraud losses, fewer missed deliveries)?
- Get the data. Collect examples with the right features (inputs) and labels (answers).
- Explore and clean. Understand the data with exploratory analysis; fix missing values, errors and duplicates.
- Split the data into training and test sets before going further, so the test set stays truly unseen.
- Prepare features. Encode categories, scale numbers, create useful new features.
- Start with a baseline. A trivially simple prediction gives a number every real model must beat.
- Train and compare models, using cross-validation on the training data.
- Evaluate once on the test set with the chosen metric.
- Deploy: save the model and serve its predictions.
- Monitor and retrain as new data arrives and the world changes.
The scikit-learn API#
Every scikit-learn model ("estimator") follows the same pattern, so once you know it you can use any algorithm:
X is a 2D table (rows × features), usually a NumPy array or DataFrame. y is a 1D array of labels, one per row.
Always start with a baseline#
A baseline is the score of the simplest possible prediction. Without one, "85% accuracy" means nothing: if 85% of emails are not spam, predicting "not spam" every time already scores 85%. scikit-learn's DummyClassifier and DummyRegressor give baselines:
from sklearn.datasets import load_breast_cancer
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
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.25, random_state=0, stratify=y)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X_train, y_train)
print("rows:", X.shape[0], "features:", X.shape[1])
print("baseline accuracy:", round(baseline.score(X_test, y_test), 3))
print("model accuracy: ", round(model.score(X_test, y_test), 3))rows: 569 features: 30
baseline accuracy: 0.629
model accuracy: 0.958The baseline simply predicts the most common class for every tumour. The real model must, and does, do far better. make_pipeline chains preparation and the model; you will learn it properly in Pipelines.
Data leakage#
Leakage happens when information about the answers sneaks into training, so the model looks great in testing but fails in real use. Common causes:
- Preparing data (for example, scaling or filling missing values) using statistics computed on the whole dataset, including the test set.
- Features that would not be available at prediction time ("refund issued" when predicting fraud at checkout).
- Duplicate rows that end up in both training and test sets.
The defence: split first, fit every preparation step on the training data only (pipelines make this automatic), and ask of each feature, "would I know this when making the prediction?"
Choosing a metric#
The metric must match the business goal. Accuracy is misleading when classes are imbalanced; for fraud detection you may care most about catching fraud (recall) without too many false alarms (precision). For prices, an average error in rupees is easier to explain than R². The metrics lessons cover the options.
Common mistakes#
Key takeaway#
Finished this lesson?