Intermediate: machine learning · The ML workflow, regression and classification · topic 23 of 62 · 30 min
The ML workflow end to end
Explanation
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
- Define the problem: who will use the prediction, and what decision changes because of it?
- Choose a metric that matches the cost of mistakes, for example mean absolute error in minutes, or recall for fraud.
- Collect and clean data (module 4), and check that every feature exists at prediction time.
- Split the data into training and test sets before you look too closely (module 6 goes deeper).
- Build a baseline: the simplest possible prediction, such as "always predict the average" or "always predict the most common class".
- Train one or more models on the training set.
- Evaluate on the test set. Compare with the baseline. If you cannot beat it, stop and rethink.
- Improve: better features, more or cleaner data, tuned settings.
- Deploy: put the model where it makes predictions for real users or reports.
- 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).
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 minutesCommon 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
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)
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.
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Linear regression and least squaresThe ML workflow, regression and classification · same module
- Gradient descent: learning step by stepThe ML workflow, regression and classification · same module
- MLOps: from notebook to productionBuilding with generative AI, and MLOps
- Capstone: an end-to-end projectEthics, privacy and careers
- Feature selection, leakage and pipelinesClustering, dimensionality reduction and feature engineering
- Choosing and explaining a modelk-NN, decision trees and ensembles
Questions people ask about this
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.
Which prediction style fits "score every customer's churn risk each night"?Related
Answer: Batch prediction
Scheduled scoring of many rows is batch.
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.
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.
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
- 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.
- Module 5 exam: The ML workflow, regression and classification · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Evaluating models. 62 topics left in the course before the final exam.