Implement multiple linear regression from scratch
Implement multiple linear regression from scratch
Comprehensively apply matrix operations + gradient descent + MSE to complete the entire process of "Data -> Model -> Training -> Prediction".
After learning this case study, you will understand that a working machine learning model is essentially a combination of linear algebra + calculus + optimization.
Real-life application
**Estimating housing prices—more than just looking at area
Housing prices depend not only on area, but also on the number of bedrooms, floor level, proximity to subways, age of the building, etc... each factor has its own weight.
Multiple linear regression is: assign a weight w to each factor, multiply all factors by their weights and sum them up, then add a base price b. Then, use historical transaction data to reverse-engineer these weights—this is training.
Intuitive understanding
The complete five-step machine learning loop:
Prepare data
X, y
Forward prediction
X*W+b
Calculate loss
MSE
Calculate gradient
X.T*error
Update parameters
W-=lr*dW
These five steps are executed in a loop during each iteration until the loss converges. The fit() method in PyTorch/TensorFlow is this loop.
Mathematical definition
[ \hat{y} = XW + b, \quad L = \frac{1}{n}\sum(\hat{y}_i - y_i)^2 ] [ \frac{\partial L}{\partial W} = \frac{2}{n} X^T (XW + b - y), \quad \frac{\partial L}{\partial b} = \frac{2}{n} \sum (XW + b - y) ]
Python Hands-on Practice
Example
import
numpy
as
np
np.
random
.
seed
(
0
)
class
LinearRegressionFromScratch:
"Implemented entirely with NumPy matrix operations + gradient descent"
def
init
(
self
,
lr
=
0.01
,
epochs
=
500
)
:
self
.
lr
=
lr
self
.
epochs
=
epochs
self
.
W
=
None
self
.
b
=
None
self
.
loss_history
=
[
]
def
fit
(
self
,
X
,
y
)
:
n_samples
,
n_features
=
X.
shape
self
.
W
=
np.
zeros
(
n_features
)
self
.
b
=
0.0
for
epoch
in
range
(
self
.
epochs
)
:
y_pred
=
X
@
self
.
W
self
.
b
error
=
y_pred - y
loss
=
np.
mean
(
error **
2
)
self
.
loss_history
.
append
(
loss
)
dW
=
(
2
/ n_samples
)
(
X.
T
@
error
)
db
=
(
2
/ n_samples
)
- np.
sum
(
error
)
self
.
W
=
self
.
lr
- dW
self
.
b
=
self
.
lr
- db
if
epoch %
100
==
0
:
(
f
"RUNOOB epoch {epoch:4d} loss={loss:.4f}"
)
def
predict
(
self
,
X
)
:
return
X
@
self
.
W
self
.
b
Construct data: y = 3x1 - 2x2 + 5 + noise
X
=
np.
random
.
uniform
(
5
,
5
,
size
=
(
200
,
2
)
)
true_W
=
np.
array
(
[
3.0
,
2.0
]
)
true_b
=
5.0
y
=
X
@
true_W + true_b + np.
random
.
randn
(
200
)
1.5
model
=
LinearRegressionFromScratch
(
lr
=
0.01
,
epochs
=
500
)
model.
fit
(
X
,
y
)
(
f
"RUNOOB learned: W={np.round(model.W, 3)}, b={round(model.b, 3)}"
)
(
f
"True: W={true_W}, b={true_b}"
)
(
f
"Loss: {model.loss_history[0]:.1f} -> {model.loss_history[-1]:.2f}"
)
Predict new sample
X_new
=
np.
array
(
[
[
1.0
,
1.0
]
,
[
2.0
,
3.0
]
]
)
(
f
"Prediction: {np.round(model.predict(X_new), 2)}"
)
Original prompt (Chinese, left untranslated)
RUNOOB epoch 0 loss=157.0828
RUNOOB epoch 100 loss=2.6062
RUNOOB epoch 200 loss=2.3077
RUNOOB epoch 300 loss=2.2581
RUNOOB epoch 400 loss=2.2475
RUNOOB 学到的: W=[ 3.014 -2.017], b=4.737
真实的: W=[ 3. -2.], b=5.0
损失: 157.1 -> 2.25
预测: [5.73 -4.18]
Application scenarios in AI
| Scenario | Connection to this case |
|---|---|
| Baseline model | The first version model for any regression task should be linear regression—simple and interpretable |
| The last layer of a neural network | The output layer of many networks is a linear layer—essentially linear regression |
| Feature importance analysis | After training, look at the size of W—features with larger absolute values have a greater impact on the result |