Sub topic 1
What Regression Predicts and Why We Use It
Prerequisites
This chapter assumes you know basic Python (functions and imports), NumPy arrays and their shapes, and what a pandas DataFrame is. You should also remember high-school algebra: the equation of a line, the mean, and the variance. You do not need to know scikit-learn yet.
Regression as supervised learning
Regression is the task of predicting a continuous number from other measured values. Examples are the price of a house, the minutes a delivery will take, or the temperature tomorrow. If the answer is a category such as "spam" or "not spam", that is classification, which this tutorial does not cover.
Regression is a kind of supervised learning. In supervised learning you have examples where the correct answer is already known. The model studies those examples and learns a rule that maps inputs to the answer. You then apply the rule to new inputs where the answer is unknown.
The vocabulary you will see
- A feature is one input column, such as floor area.
- The target is the number you want to predict, such as price.
- A sample is one row: one house, one delivery, one day.
- Training means letting the model learn from rows where the target is known.
In scikit-learn, the features are stored in a 2-D array called X with shape (n_samples, n_features). The target is stored in a 1-D array called y with shape (n_samples,). Getting these shapes right avoids many early errors.
import numpy as np
X = np.array([[50], [80], [120], [65]]) # area in square meters
y = np.array([150, 260, 330, 190]) # price in thousands
print("X shape:", X.shape)
print("y shape:", y.shape)X has four samples and one feature, so its shape is (4, 1). y has four values, so its shape is (4,).
Why use regression
Regression has two common purposes.
- Prediction. You want accurate numbers for new cases, such as a price estimate for a house that has not sold.
- Explanation. You want to know how the target changes when a feature changes, such as how much each extra square meter adds to the price.
The two goals pull in different directions. A model built only for prediction can be complicated, as long as it is accurate on new data. A model built for explanation needs coefficients you can read and trust. Linear regression is popular because it is fast, simple, and good at both.
The workflow ahead
Every chapter in this tutorial follows the same loop. You pick a model, fit it on training data, and measure its error on data it has not seen. Then you inspect the mistakes and improve the model.
The key idea is the third step. A model that only works on the rows it learned from is not useful. Everything later in this tutorial, from train/test splits to cross-validation, exists to measure performance on data the model has not seen.
Exercise
You want to predict whether a customer will cancel a subscription (yes or no), and separately how many dollars they will spend next month. Which task is regression, and what shape should y have if you have 500 customers?
Show answer
Predicting dollars spent is regression, because the target is a continuous number. Predicting cancellation is classification. For 500 customers, y has shape (500,).