Machine Learning for Beginners with Python
Chapter 3 / 7· 22 min read· 0 cards

Your First Model: Linear Regression

Build a model that predicts numbers — the friendly entry point to supervised learning.

Predicting numbers

Time to build a real model. We'll start with linear regression, the friendliest ML algorithm and a perfect first model. It tackles regression problems — predicting a continuous number, like a price, a temperature, or a salary. Despite the fancy name, the idea is one you already know from school: fitting a straight line through points.

The intuition: a line of best fit

Imagine plotting house size against price on a scatter plot — the points trend upward (bigger houses cost more). Linear regression finds the straight line that best fits those points. Once you have that line, you can predict the price of any house size by reading off the line. That's the whole idea: find the best line through your data, then use it to predict.

# A line has the form:  y = m*x + b
#   y = what we predict (price)
#   x = the feature (size)
#   m = the slope (how much price rises per unit of size)
#   b = the intercept (the baseline)
# Linear regression LEARNS the best m and b from your data.

The model's job during training is to find the slope and intercept that make the line fit the data as closely as possible — minimising the total error between the line and the actual points. You don't compute this by hand; scikit-learn does it for you.


Building it with scikit-learn

Let's build a complete, working linear regression model. Watch the create-fit-predict pattern in action — this is real, runnable ML:

import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# 1. Load and prepare data
df = pd.read_csv("houses.csv")
X = df[["size"]]        # feature (note: double brackets keep it 2D)
y = df["price"]         # label

# 2. Split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

# 3. Create and train the model
model = LinearRegression()
model.fit(X_train, y_train)

# 4. Make predictions
predictions = model.predict(X_test)

# Predict the price of a brand-new 1500 sq ft house
new_house = [[1500]]
print(model.predict(new_house))   # e.g. [5200000]

That's a complete machine learning model in a handful of lines! We loaded data, split it, created a model, trained it with fit(), and predicted with predict(). The model learned the relationship between size and price, and can now estimate the price of any house given its size.


Looking inside the model

Unlike many ML models, linear regression is wonderfully interpretable — you can see exactly what it learned (the slope and intercept):

print(model.coef_)        # the slope(s) — price increase per sq ft
print(model.intercept_)   # the baseline price

# So the learned formula is roughly:
# price = coef_ * size + intercept_

The coef_ tells you how much the price rises for each extra square foot, and intercept_ is the baseline. This transparency is valuable — you can explain why the model predicts what it does, which matters in fields like finance and medicine where decisions must be justified.


Multiple features

Real predictions rarely depend on just one factor. House price depends on size, and bedrooms, and location, and more. Linear regression handles many features easily — you just include more columns in X:

# Use several features instead of one
X = df[["size", "bedrooms", "age", "distance_to_city"]]
y = df["price"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = LinearRegression()
model.fit(X_train, y_train)

# Each feature gets its own coefficient
print(model.coef_)   # [coef_size, coef_bedrooms, coef_age, coef_distance]

With multiple features, the model learns a coefficient for each — how much each factor influences the price, holding the others constant. This is called multiple linear regression, and it's far more realistic and powerful. The code barely changes; you just feed more feature columns.


How good is it? A first look at evaluation

You always want to know how well your model performs. For regression, score() gives the R² value — roughly, the fraction of the variation in the data the model explains, from 0 (useless) to 1 (perfect):

print(model.score(X_test, y_test))   # e.g. 0.78 -- explains 78% of variation

# A more intuitive error: average prediction error in actual units
from sklearn.metrics import mean_absolute_error
preds = model.predict(X_test)
print(mean_absolute_error(y_test, preds))  # e.g. 320000 -- avg off by 3.2 lakh

An R² of 0.78 means the model captures most but not all of what drives prices — decent. The mean absolute error is even more intuitive: it tells you, in real rupees, how far off your predictions are on average. Crucially, we measure these on the test set — the unseen data — for an honest assessment. You've now built and evaluated a real predictive model. Next, we'll predict categories instead of numbers with classification.

Reading mode · scroll to read at your own pace

Finished "Your First Model: Linear Regression"?

Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.

Was this chapter clear?

Try it yourself — open the Code Playground15+ languages — Python, JavaScript, Java, C++, SQL & more — full IDE-style editor, instant run. Your code is auto-saved per language.