Simple Linear Regression
The model
Simple linear regression predicts a number from one feature:
ลท = wยทx + bลท = wยทx + b
wwis the slope (how much y changes per unit x)bbis the intercept (y when x = 0)
false
flowchart LR x[Single feature x] -->|w, b| yhat[Prediction ลท]
false
Intuition
If w = 200w = 200 and xx is house size in sqft:
- increasing size by 1 sqft increases predicted price by 200 (units of currency)
Scikit-learn example
Simple linear regression
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# X must be 2D in scikit-learn
X = np.array([500, 700, 800, 1000, 1200]).reshape(-1, 1)
y = np.array([100, 150, 170, 210, 240])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
pred = model.predict(X_test)
print("w (slope):", model.coef_[0])
print("b (intercept):", model.intercept_)
print("MSE:", mean_squared_error(y_test, pred))Simple linear regression
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# X must be 2D in scikit-learn
X = np.array([500, 700, 800, 1000, 1200]).reshape(-1, 1)
y = np.array([100, 150, 170, 210, 240])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression()
model.fit(X_train, y_train)
pred = model.predict(X_test)
print("w (slope):", model.coef_[0])
print("b (intercept):", model.intercept_)
print("MSE:", mean_squared_error(y_test, pred))Pitfalls
- outliers can strongly affect the line
- if the relationship is non-linear, the line underfits
Mini-checkpoint
Plot x vs y. Does it look roughly linear?
- If yes, start here.
- If no, consider polynomial regression or other models.
๐งช Try It Yourself
Exercise 1 โ Train-Test Split
Exercise 2 โ Fit a Linear Model
Exercise 3 โ Evaluate with MSE
If this helped you, consider buying me a coffee โ
Buy me a coffeeWas this page helpful?
Let us know how we did
