Linear Regression

Fit a straight-line relationship to predict a continuous number.

SyntaxLinearRegression().fit(X, y)

Linear regression is the classic starting model. It fits a line (or plane) y = w·x + b that best predicts a numeric target.

What it learns

The coefficients (slope per feature) and the intercept. Each coefficient says how much y changes per unit of that feature.

Example

Example · python
from sklearn.linear_model import LinearRegression
import numpy as np

# House size (sqm) -> price
X = np.array([[50], [70], [90], [110]])
y = np.array([150, 210, 265, 330])   # thousands

model = LinearRegression()
model.fit(X, y)

print(model.coef_)       # ~[2.98]  price per sqm
print(model.intercept_)  # ~ 0.5
print(model.predict([[100]]))  # ~[298.] thousand

When to use it

  • A real-estate analyst trains a linear regression on square footage and number of bedrooms to predict house prices in a new neighbourhood.
  • A sales team uses linear regression to model the relationship between advertising spend and monthly revenue, then reads the coefficient to quantify ROI.
  • An engineer uses linear regression as a simple baseline before trying more complex models, establishing a performance floor to beat.

More examples

Simple linear regression

Fits a simple one-feature linear regression on house sizes and prices, then reads the learned coefficient and makes a prediction.

Example · python
import numpy as np
from sklearn.linear_model import LinearRegression

X = np.array([[800], [1200], [1500], [2000], [2500]])  # sqft
y = np.array([150, 220, 275, 360, 450])                # price (K)

model = LinearRegression().fit(X, y)
print('Coefficient:', model.coef_[0].round(4))
print('Intercept:', model.intercept_.round(2))
print('Prediction for 1800 sqft:', model.predict([[1800]]))

Multiple linear regression

Extends to three features and evaluates with the R-squared score, showing how much variance in house price the model explains.

Example · python
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

df = pd.read_csv('housing.csv')
features = ['sqft', 'bedrooms', 'age']
X = df[features]; y = df['price']

X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)
model = LinearRegression().fit(X_tr, y_tr)
print('R2:', r2_score(y_te, model.predict(X_te)).round(3))

Ridge regularised regression

Uses Ridge regression (L2 regularisation) on a high-dimensional dataset to reduce overfitting and evaluates with RMSE.

Example · python
from sklearn.linear_model import Ridge
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import numpy as np

X, y = make_regression(n_samples=200, n_features=50, noise=15, random_state=1)
X_tr, X_te, y_tr, y_te = train_test_split(X, y)

model = Ridge(alpha=10).fit(X_tr, y_tr)
rmse = np.sqrt(mean_squared_error(y_te, model.predict(X_te)))
print('RMSE:', rmse.round(2))

Discussion

  • Be the first to comment on this lesson.