L3 Linear Regression
L3 Linear Regression
Khoat Than
School of Information and Communication Technology
Hanoi University of Science and Technology
2021
2
Contents
¡ Introduction to Machine Learning & Data Mining
¡ Supervised learning
¨ Linear regression
¡ Unsupervised learning
¡ Practical advice
3
Linear regression: introduction
¡ Regression problem: learn a function y = f(x) from a given
training data D = {(x1, y1), (x2, y2), …, (xM, yM)} such that
yi ≅ f(xi) for every i
¨ Each observation of x is represented by a vector in an n-dimensional
space, e.g., xi = (xi1, xi2, …, xin)T. Each dimension represents an
attribute/feature/variate.
¨ Bold characters denote vectors.
¡ Linear model: if f(x) is assumed to be of linear form
f(x) = w0 + w1x1 + … + wnxn
¨ w0, w1, …, wn are the regression coefficients/weights. w0 sometimes is
called “bias”.
¡ Note: learning a linear function is equivalent to learning the
coefficient vector w = (w0, w1, …, wn)T.
4
Linear regression: example
¡ What is the best function?
x y 𝑓(𝑥)
0.13 -0.91
1.02 -0.17
3.17 1.61
-2.76 -3.31
1.44 0.18
5.28 3.36
-1.74 -2.46 𝑥
7.93 5.56
... ...
5
Prediction
¡ For each observation x = (x1, x2, …, xn)T
¨ The true output: cx
(but unknown for future data)
¨ Prediction by our system:
yx = w0 + w1x1 + … + wnxn
¨ We often expect yx ≅ cx.
¡ Prediction for a future observation z = (z1, z2, …, zn)T
¨ Use the learned function to make prediction
f(z) = w0 + w1z1 + … + wnzn
6
Learning a regression function
¡ Learning goal: learn a function f* such that its prediction in the
future is the best.
¨ Its generalization is the best.
¡ Difficulty: infinite number of functions 𝑓(𝑥)
¨ How can we learn?
¨ Is function f better than g?
¡ Use a measure
¨ Loss function is often used to guide learning.
𝑥
7
Loss function
¡ Definition:
¨ The error/loss of the prediction for an abservation x = (x1, x2, …, xn)T
r(x) = [cx – f(x)]2 = (cx – w0 – w1x1 -… - wnxn)2
¨ The expected loss of f over the whole space:
E = Ex[r(x)] = Ex[cx – f(x)]2 Cost, risk
(Ex is the expectation over x)
¡ The goal of learning is to find f* that minimizes the expected loss:
f ⇤ = arg minf 2H E x [r(x)]
¨ H is the space of functions of linear form.
¡ But, we cannot work directly with this problem during the
learning phase. (why?)
Hàm lỗi thực nghiệm 8
Empirical loss
Ta chỉ quan sát được một tập 𝑫 = 𝒙 , 𝑦 , … , (𝒙 , 𝑦 ) .
Cần học hàm 𝑓 từ đó.
¡ We can only observe a set of training data D = {(x1, y1), (x2, y2),
…, Lỗi thực nghiệm (empirical
(xM, yM)}, and have to learnloss; residual
f from D. sum of squares)
Là một xấp xỉ của 𝐸𝒙 𝑟(𝒙) trên tập học D
𝑅𝑆𝑆 𝑓 = (𝑦 − 𝑓(𝒙 )) = (𝑦 − 𝑤 − 𝑤 𝑥 − ⋯ − 𝑤 𝑥 )
¨
!Nhiều phương pháp học thường gắn với 𝑅𝑆𝑆.
𝑅𝑆𝑆𝑓 is an approximation to Ex[r(x)].
"
%
¡ 𝑅𝑆𝑆 𝑓 − 𝑬𝑥 𝑟 𝒙 is often known as generalization error of f.
&
(lỗi tổng quát hoá)
¡ Many learning algorithms base on this RSS and its variants.
8
9
Bình phương tối thiểu
Methods: ordinary least squares (OLS)
Bình phương tối thiểu
¡ Given D, we find f* that minimizesmà có 𝑅𝑆𝑆
Cho trước 𝑫, ta đi tìm hàm 𝑓 RSS: nhỏ nhất.
∗
𝑓 = arg min
Cho trước 𝑫, ta đi tìm hàm 𝑓 𝑅𝑆𝑆(𝑓) nhỏ nhất.
mà có 𝑅𝑆𝑆
∈𝑯
∗
𝑓 = arg min 𝑅𝑆𝑆(𝑓)
∈𝑯
∗
⇔ 𝒘 = arg min (𝑦 − 𝑤 − 𝑤 𝑥 − ⋯ − 𝑤 𝑥 ) (1)
𝒘
⇔ 𝒘∗ = arg min (𝑦 − 𝑤 − 𝑤 𝑥 − ⋯ − 𝑤 𝑥 )
𝒘
¡ This Đây được gọi là
method is often known bình phương tối thiểu
as ordinary least (least squares).
squares (OLS, bình
phương tối thiểu). bình phương tối thiểu
Đây được gọi là
Tìm nghiệm 𝒘 ∗ (least squares).
bằng cách lấy đạo hàm của 𝑅𝑆𝑆 và giải
¡ Find phương trình 𝑅𝑆𝑆′
w* by taking
Tìm nghiệm 𝒘 = 0. Thu được:
∗ bằng cách lấy đạo hàm của 𝑅𝑆𝑆
the gradient of RSS and the solving the
và giải
equation RSS’=0. We=have:
phương trình 𝑅𝑆𝑆′ 𝒘∗ = 𝑨 𝑨 𝑨 𝒚
0. Thu được:
𝒘∗ = 𝑨 𝑨 𝑨 ×
Trong đó 𝑨 là ma trận dữ liệu cỡ 𝑀 𝒚 (𝑛 + 1) mà hàng thứ 𝑖 là
(1, 𝑥 , … là ma trận dữ liệu cỡ 𝑀
Trong đó 𝑨 , 𝑥 ); 𝑩 là ma trận nghịch đảo;; 𝒚 = 𝑦 , … , 𝑦 là
× (𝑛 + 1) mà hàng thứ 𝑖 .
¨ Where A is the data matrix
Chú ý: giả thuyết 𝑨 𝑨 of size Mx(n+1), whose the ith row is
tồn tại nghịch đảo.
(1, 𝑥 , … , 𝑥 ); 𝑩 là ma trận nghịch đảo;; 𝒚 = 𝑦 , … , 𝑦 .
Ai = (1, xi1, xi2, …, xin); B-1 is the inversion of matrix B; y = (y1, y2, …, yM)T.
Chú ý: giả thuyết 𝑨 𝑨 tồn tại nghịch đảo.
¨ Note: we assume that ATA is invertible (ma trận ATA khả nghịch).
Cho trước 𝑫, ta đi tìm hàm 𝑓 mà có 𝑅𝑆𝑆 nhỏ nhất.
𝑓 ∗ = arg min 𝑅𝑆𝑆(𝑓)
∈𝑯 10
Methods: OLS
Bình phương tối thiểu: thuật toán
∗
⇔ 𝒘 = arg min (𝑦 − 𝑤 − 𝑤 𝑥 − ⋯ − 𝑤 𝑥 )
𝒘
¡ Input: D = {(x1, y1), (x2, y2), …, (xM, yM)}
Đây được gọi là
Input: 𝑫 = 𝒙bình phương tối thiểu
, 𝑦 , … , (𝒙 , 𝑦 ) (least squares).
¡ Output: w*
Output: 𝒘∗ ∗ bằng cách lấy đạo hàm của 𝑅𝑆𝑆 và giải
Tìm nghiệm 𝒘
¡ Learning: compute
phương trình 𝑅𝑆𝑆′
∗ = 0. Thu được:
Học 𝒘 bằng cách tính:
𝒘∗ 𝒘
=∗ =𝑨 𝑨𝑨 𝑨 𝑨 𝒚𝑨 𝒚
Trong đó 𝑨
¨ Where A
Trong đó 𝑨 the data matrix of ×
là ma trận dữ liệu cỡ 𝑀
is là ma trận dữ liệu cỡ 𝑀 (𝑛×M
size +(𝑛x1)
+mà hàng thứ 𝑖
(n+1), whose the là ith là
1) mà hàng thứ 𝑖 row is
(1, 𝑥A , … , 𝑥 ); 𝑩 là ma trận nghịch đảo;; 𝒚 =of𝑦matrix
, … , 𝑦 B; .y = (y=, y , …, y )T.
i = (1, xi1, xi2,=…,
một véctơ 𝑨 (1,x𝑥in);, …
B-1, 𝑥is the
), 𝑩inversion
là ma trận nghịch đảo, 𝒚 1 2 M
Chú ý: giả thuyết 𝑨
𝑦 ,…,𝑦 . 𝑨 tồn tại nghịch đảo.
¨ Note: we assume that A A is invertible.
T
Chú ý: giả thuyết 𝑨 𝑨 tồn tại nghịch đảo.
¡ Prediction for a new x:
Phán đoán cho quan sát mới 𝒙: 9
𝑦 = 𝑤 ∗ + 𝑤 ∗𝑥 + ⋯ + 𝑤 ∗𝑥
11
Methods: OLS example
6
x y
0.13 -1
4 f*
1.02 -0.17
3 1.61
2
-2.5 -2
1.44 0.1
0
5 3.36
-1.74 -2.46
-2
7.5 5.56
-4
-4 -2 0 2 4 6 8
f*(x) = 0.81x – 0.78
12
Methods: limitations of OLS
¡ OLS cannot work if ATA is not invertible
¨ If some columns (attributes/features) of A are dependent, then A will
be singular and therefore ATA is not invertible.
¡ OLS requires considerable computation due to the need of
computing a matrix inversion.
¨ Intractable for the very high dimensional problems.
¡ OLS very likely tends to overfitting, because the learning phase
just focuses on minimizing errors on the training data.
Ridge regression (1) 13
Methods: Ridge regression (1)
Cho trước 𝑫 = 𝒙 , 𝑦 , … , (𝒙 , 𝑦 ) , ta đi giải bài toán:
¡ Given D = {(x1, y1), (x2, y2), …, (xM, yM)}, we solve for:
𝑓 ∗ = arg min 𝑅𝑆𝑆 𝑓 + 𝜆 𝒘
∈𝑯
13
14
Methods: Ridge regression (2)
¡ Problem (2) is equivalent to the following:
"
𝑤 ∗ = arg min . 𝑦& − 𝑨& 𝒘 # (3)
𝒘
&'!
¡ Equivalently:
&
𝑤 ∗ = arg min 0 𝑦) − 𝑨) 𝒘 + +𝜆 𝒘 (4)
%
𝒘
)*%
¡ This problem is non-differentiable à the training algorithm should
be more complex than Ridge.
20
LASSO: regularization role
¡ The regularization types lead to different domains for w.
¡ LASSO often produces sparse solutions, i.e., many components
of w are zero.
¨ Shinkage and selection at the same time