Skip to content

Intermediate: machine learning · The ML workflow, regression and classification · topic 23 of 62 · 30 min

The ML workflow end to end

Explanation

Every ML project follows the same loop: define the problem and metric, get and clean data, split it, build a simple baseline, train models, evaluate on held-out data, improve, deploy and monitor. scikit-learn gives every model the same simple interface: fit to learn, predict to use.

Why a workflow?

Beginners often jump straight to "which algorithm is best?". In real projects the algorithm is a small part of the work. Most failures come from a vague problem, a wrong metric, leaked data or a model nobody monitors. A fixed workflow protects you from these.

The steps

  1. Define the problem: who will use the prediction, and what decision changes because of it?
  2. Choose a metric that matches the cost of mistakes, for example mean absolute error in minutes, or recall for fraud.
  3. Collect and clean data (module 4), and check that every feature exists at prediction time.
  4. Split the data into training and test sets before you look too closely (module 6 goes deeper).
  5. Build a baseline: the simplest possible prediction, such as "always predict the average" or "always predict the most common class".
  6. Train one or more models on the training set.
  7. Evaluate on the test set. Compare with the baseline. If you cannot beat it, stop and rethink.
  8. Improve: better features, more or cleaner data, tuned settings.
  9. Deploy: put the model where it makes predictions for real users or reports.
  10. Monitor: watch accuracy and input data over time, and retrain when the world changes.

Baselines: your first model

A baseline is a deliberately simple prediction that sets the bar. For regression, predict the mean (or median) of the training labels for everyone. For classification, predict the majority class. If 95% of loans are repaid, a "model" that always says "repaid" is 95% accurate. A real model that reaches 95.5% is barely better, which you would never notice without the baseline.

The scikit-learn interface

scikit-learn (imported as sklearn) is the standard Python library for classic ML. Every model, called an estimator, has the same methods: fit(X, y) learns from training data, predict(X) makes predictions, and score(X, y) gives a default metric (R² for regressors, accuracy for classifiers).

python
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

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

print(mean_absolute_error(y_test, baseline.predict(X_test)))   # e.g. 9.8 minutes
print(mean_absolute_error(y_test, model.predict(X_test)))      # e.g. 4.1 minutes
The same fit / predict pattern works for every model. Numbers are illustrative. Shown only: the tasks build these ideas in plain Python.
Think of it like this: The workflow is like cooking for a wedding. You decide the menu and the number of guests first (problem and metric), buy and wash the vegetables (data and cleaning), taste a simple dish before trying fancy ones (baseline), and keep tasting during service (monitoring). Nobody starts by buying the most expensive pan.

Common mistakes

  • No baseline, so a weak model looks impressive. Fix: always report the baseline next to the model.
  • Tuning on the test set until it looks good. Fix: keep the test set for the final check only.
  • Deploying and forgetting. Data changes (new cities, festivals, prices). Fix: monitor and retrain.
  • A metric that does not match the business. 99% accuracy on fraud can still miss every fraud case.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

The ML workflow end to end · 1/7

The ML workflow

This lesson walks through the steps of every machine learning project.

Prefer a video by a teacher? Find free videos on “machine learning project workflow scikit-learn fit predict baseline” on YouTube ↗

In real life: Forecasting demand for a cloud kitchen

A cloud kitchen wants to predict how many biryanis to prepare each evening. The team first defines the metric (wasted plus missed plates), builds a baseline ("same as the same weekday last week"), then trains a model with features like weekday, weather, cricket-match days and festivals. The model goes live only after it beats the baseline for four straight weeks on held-out data, and its errors are reviewed every Monday.

Practice (0/3 done)

Task 1 · Code · Python

Write majority_class(labels) that returns the most common label (the classification baseline). If two or more labels tie, return the smallest one (sorted order). Return None for an empty list.

Task 2 · Code · Python

Write baseline_accuracy(train_labels, test_labels): find the majority class of the TRAINING labels (ties: smallest label), predict it for every test example, and return the accuracy on the test labels, rounded to 4 decimal places. Return 0.0 if there are no test labels.

Task 3 · Code · Python

Write mean_baseline(train_y, n) that returns a list of n predictions, each equal to the mean of the training targets, rounded to 2 decimal places (the regression baseline). Example: mean_baseline([20, 30, 40], 2) returns [30.0, 30.0].

Quick quiz

Question 1

1.What is a baseline model?

Question 2

2.97% of transactions are not fraud. A model has 97% accuracy. What should you conclude?

Question 3

3.In scikit-learn, which method learns from training data?

Question 4

4.Why monitor a model after deployment?

Question 5

5.Which step should come FIRST?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

What is a baseline model?

Answer: A deliberately simple prediction that sets the bar to beat

Baselines like "predict the mean" tell you whether a real model adds value.

97% of transactions are not fraud. A model has 97% accuracy. What should you conclude?

Answer: It may be no better than always predicting "not fraud"

The majority-class baseline already scores 97%.

In scikit-learn, which method learns from training data?

Answer: fit

fit(X, y) trains the model; predict uses it.

Why monitor a model after deployment?

Answer: Because the data and the world change, so accuracy can drop over time

New cities, prices or user habits can make an old model wrong.

Which step should come FIRST?

Answer: Defining the problem and the metric

Without a clear problem and metric, you cannot judge any model.

When is ML a poor choice?Related

Answer: When a simple exact formula already solves the problem

A formula like GST needs no model; ML adds cost and risk for no gain.

From What machine learning is

Which prediction style fits "score every customer's churn risk each night"?Related

Answer: Batch prediction

Scheduled scoring of many rows is batch.

From MLOps: from notebook to production

Your model is only slightly better than the simple rule. What should the write-up say?Related

Answer: Report both honestly and discuss whether the extra complexity is worth it

Honest comparison is a strength in a portfolio.

From Capstone: an end-to-end project

What is the main benefit of a scikit-learn Pipeline in cross-validation?Related

Answer: Preprocessing is refitted on each training fold, preventing leakage

Each fold's scaler and imputer see only that fold's training data.

From Feature selection, leakage and pipelines

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “The ML workflow, regression and classification” before its exam: Linear regression and least squares, Gradient descent: learning step by step, Logistic regression and classification.
  2. Module 5 exam: The ML workflow, regression and classification · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Evaluating models. 62 topics left in the course before the final exam.

← Previous: What machine learning is