Supervised LearningCore· 40 min read

Linear Regression: Predict a Number

When the answer is a number, fit a straight line through the data and read off predictions.

What you will learn

  • Know when to use regression
  • Train LinearRegression with scikit-learn
  • Predict a value for new input

When the answer is a number

Some questions have a number as the answer: how much will this house cost? how many ice creams will I sell? Predicting a number is called regression.

The simplest kind is linear regression: it draws the best straight line through your data points. To predict, you just read the line at your input.

A worked example: ice cream sales

A shop notices it sells more ice cream on hotter days. We have the temperature and the cups sold for a few days, and we want to predict sales for a new day.

Fitting a straight line to predict a number
from sklearn.linear_model import LinearRegression

# Feature: temperature (°C). Label: cups of ice cream sold.
X = [[20], [25], [30], [35]]
y = [ 100,  150,  200,  250]

model = LinearRegression()
model.fit(X, y)                 # learn the best straight line

# Predict sales on a 28°C day
pred = model.predict([[28]])
print('Predicted cups at 28C:', round(pred[0]))

Note: Output: Predicted cups at 28C: 180 The model learned that each extra degree adds about 10 cups. At 28°C it predicts ~180 cups — sitting neatly between the 25°C (150) and 30°C (200) days.

What the line actually learned

A straight line is just y = slope × x + intercept. scikit-learn finds the best slope and intercept for your data. You can peek at them:

Reading the slope and intercept the model found
print('Slope (cups per degree):', round(model.coef_[0], 1))
print('Intercept:', round(model.intercept_, 1))

Note: Output: Slope (cups per degree): 10.0 Intercept: -100.0 So the learned rule is cups = 10 × temp − 100. At 28°C: 10 × 28 − 100 = 180. The model discovered this formula from the data — you did not write it.

Watch out: Regression predicts a number, not a category. If your answer is a yes/no or a class name, you need classification instead (next lesson).

Tip: Linear regression assumes the relationship is roughly a straight line. If your data curves, the straight line will fit poorly — a sign to try a different model.

Q. Which task is a regression problem?

Answer: Predicting a number (temperature) is regression. The others predict categories or find groups.

✍️ Practice

  1. Predict ice cream sales at 22°C using the model and check it sits between the 20°C and 25°C values.
  2. Add a 40°C / 300-cups day to X and y, re-fit, and predict 28°C again.

🏠 Homework

  1. Build a tiny linear regression that predicts a person’s weekly spending from their weekly income. Make up 4 rows of data and predict one new value.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →