Module 12 · Scikit-learn: Building Real Models · Lesson 39 of 51
The scikit-learn pattern
Every model in scikit-learn works the same way: create, fit, predict, score. Split with train_test_split, fit a linear regression on daily sales, and measure MAE, RMSE and R².
In this lesson · about 35 minutes
- Use the create → fit → predict pattern on any estimator
- Split data with train_test_split and a fixed random_state
- Report MAE, RMSE and R² for a regression model
The pattern
from sklearn.linear_model import LinearRegression
model = LinearRegression() # 1. create (choose settings here)
model.fit(X_train, y_train) # 2. learn from training data
pred = model.predict(X_test) # 3. predict new rows
Transformers (scalers, encoders, imputers) follow the same idea with fit and transform. random_state fixes the randomness so results repeat.
Regression metrics
| Metric | Meaning | Read as |
|---|---|---|
| MAE | Average absolute error | "Off by $85 on a typical day" |
| RMSE | Square root of the average squared error | Like MAE but punishes big misses more |
| R² | Share of variation explained (1 is perfect, 0 is no better than the mean) | "Explains 62% of day-to-day swings" |
Predicting daily sales
Exercise
Your first scikit-learn model
Fit a LinearRegression predicting customers from temp_c, weekend and promo, using train_test_split(…, test_size=0.2, random_state=1). Set mae and r2 on the test set, rounded to 3 decimals, and promo_effect to the promo coefficient rounded to 2 decimals.
Code that runs before yours
import pandas as pd
import numpy as np
daily = pd.read_csv("kc_daily.csv")
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_scoreCheck your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 1
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.