Salta al contenuto
Note per Studenti Linear Regression Report

Linear Regression

In questa pagina 5

Regression: the output yy is a real number (see Introduction to Machine Learning - Learning, Data and TasksMachine learning builds a hypothesis g that approximates an unknown target function f using only a set of historical examples (input, output). The course lecture frames learning as adaptive behaviour, shows through the credit-approval example the components of learning (input x, output y, target f, data, hypothesis g, hypothesis set, learning algorithm) and warns that a model is only as unbiased as its data. Tasks named in the course are classification and regression, supervised and non-supervised problems.Introduction to Machine Learning - Learning, Data and Tasks →). The hypothesis set is the set of linear functions of the features, and the error is the squared error of The Statistical Learning Framework and Empirical Risk MinimizationA hypothesis is judged by an error function computed pointwise on the data and averaged: the in-sample (empirical) error E_in(h) = (1/N) sum of e(f(x_i), h(x_i)); with squared differences it is the sum of squared errors. The learning algorithms of the course all search the hypothesis set for the h with small in-sample error, but what matters is the error on new inputs drawn from the same input distribution, so the data must cover the regions where the inputs live, and part of the data must be held back to check the result.The Statistical Learning Framework and Empirical Risk Minimization →. ADALINE and Gradient DescentADALINE is a perceptron whose activation function is the identity: during training the unit outputs w^T x and the in-sample error E_in = (1/N) sum (y_i - w^T x_i)^2 is minimised by gradient descent, w_new = w_old - eta * grad E_in, with partial derivatives dE_in/dw_j = (2/N) sum (y_i - w^T x_i)(-x_i^j) and matrix form grad E_in proportional to -X^T (Y - X w). The prediction is still the sign of w^T x. Learning rate eta sets the step size and a stopping rule ends the iteration.ADALINE and Gradient Descent → minimises the same error step by step; here it is minimised in one shot. The estimation point of view is in Least Squares Estimation - Linear and Weighted Least SquaresThe least squares estimator minimises the sum of squared deviations between observed and predicted outputs; for models linear in the parameters it has a closed form given by the normal equation, extended to weighted least squares and illustrated on a straight-line fit and on a DC motor.Least Squares Estimation - Linear and Weighted Least Squares →.

1. The problem

The first sketch draws a straight line (the target function seen through the data) and a few samples (xi,yi)(x_i,y_i) scattered around it, with vertical dotted segments from each point to the line. Given a new input xx, what is the output yy? The hypothesis hh is a line; the error of one sample is the squared vertical distance (h(xi)−yi)2\big(h(\mathbf{x}_i)-y_i\big)^2, so

Ein(h)=1N∑i=1N(h(xi)−yi)2(in-sample error).E_{\text{in}}(h)=\frac1N\sum_{i=1}^{N}\big(h(\mathbf{x}_i)-y_i\big)^2\qquad\text{(in-sample error).}

With the linear hypothesis h(x)=wTxh(\mathbf{x})=\mathbf{w}^T\mathbf{x} and the bias column x0=1x_0=1 (as in Halfspaces and the Perceptron AlgorithmA perceptron computes h(x) = sign(w^T x) where x includes a constant coordinate x0 = 1 that absorbs the threshold into the bias weight w0; its decision boundary is the hyperplane w^T x = 0 (a line in the plane, x2 = -(w1/w2) x1 - w0/w2). The Perceptron Learning Algorithm scans the data and, on every misclassified point, replaces w by w + y x. A single perceptron cannot separate the exclusive-or pattern; two perceptrons feeding a third give a multilayer network.Halfspaces and the Perceptron Algorithm →):

Ein(w)=1N∑i=1N(wTxi−yi)2,w=[w0w1⋮wd],  xi=[x0x1⋮xd],  yi∈R.E_{\text{in}}(\mathbf{w})=\frac1N\sum_{i=1}^{N}\big(\mathbf{w}^T\mathbf{x}_i-y_i\big)^2,\qquad \mathbf{w}=\begin{bmatrix}w_0\\w_1\\\vdots\\w_d\end{bmatrix},\; \mathbf{x}_i=\begin{bmatrix}x_0\\x_1\\\vdots\\x_d\end{bmatrix},\;y_i\in\mathbb R .

2. Matrix form of the error

Let di=wTxi−yid_i=\mathbf{w}^T\mathbf{x}_i-y_i be the error of sample ii and d=[d1,…,dN]T\mathbf{d}=[d_1,\dots,d_N]^T. The sum is

d12+d22+⋯+dN2=∥d∥2,since ∥d∥=d12+⋯+dN2.d_1^2+d_2^2+\dots+d_N^2=\|\mathbf{d}\|^2,\qquad\text{since }\|\mathbf{d}\|=\sqrt{d_1^2+\dots+d_N^2}.

Stack the examples as rows of the data matrix XX (row ii is xiT\mathbf{x}_i^T, columns are the features f0,f1,…,fdf_0,f_1,\dots,f_d) and the outputs in the column YY:

X=[x1Tx2T⋮xNT],Y=[y1y2⋮yN].X=\begin{bmatrix}\mathbf{x}_1^T\\\mathbf{x}_2^T\\\vdots\\\mathbf{x}_N^T\end{bmatrix},\qquad Y=\begin{bmatrix}y_1\\y_2\\\vdots\\y_N\end{bmatrix}.

Then the vector Xw−YX\mathbf{w}-Y (both N×1N\times1) has exactly the entries did_i, and

  Ein(w)=1N ∥Xw−Y∥2  \boxed{\;E_{\text{in}}(\mathbf{w})=\frac1N\,\|X\mathbf{w}-Y\|^2\;}

3. The closed-form solution

The page "Regression Problem" differentiates and sets the gradient to zero:

∇Ein(w)=1N 2 XT(Xw−Y)=0  ⟹  XT(Xw−Y)=0\nabla E_{\text{in}}(\mathbf{w})=\frac1N\,2\,X^T(X\mathbf{w}-Y)=0\;\Longrightarrow\;X^T(X\mathbf{w}-Y)=0

(the factor 2/N2/N is non-zero and can be dropped). Expand and rearrange:

XTX w−XTY=0  ⟹  XTX w=XTY(normal equations).X^TX\,\mathbf{w}-X^TY=0\;\Longrightarrow\;\boxed{X^TX\,\mathbf{w}=X^TY}\quad\text{(normal equations).}

Multiply both sides on the left by (XTX)−1(X^TX)^{-1}; since (XTX)−1XTX=I(X^TX)^{-1}X^TX=I,

  w=(XTX)−1XTY  \boxed{\;\mathbf{w}=(X^TX)^{-1}X^TY\;}

The matrix (XTX)−1XT(X^TX)^{-1}X^T is called the pseudo-inverse of XX: XX itself is N×(d+1)N\times(d+1), not square, and has no inverse X−1X^{-1}; the page notes this next to the formula ("inverse of XX: X−1X^{-1}"). The formula assumes that XTXX^TX is invertible.

Cost of the formula. The last page of the lecture takes a data matrix XX with about 100 000100\,000 rows and 1010 columns (the digits of the handwritten sizes are small and slightly uncertain). Then XTX^T is 10×100 00010\times100\,000 and

XTX:  (10×100 000) (100 000×10)=10×10,X^TX:\;(10\times100\,000)\,(100\,000\times10)=10\times10,

so the matrix to invert, (XTX)−1(X^TX)^{-1}, is only 10×1010\times10 however many samples there are. The number of rows NN only affects the cost of forming the product.

4. Closed form versus gradient descent

closed form ADALINE and Gradient DescentADALINE is a perceptron whose activation function is the identity: during training the unit outputs w^T x and the in-sample error E_in = (1/N) sum (y_i - w^T x_i)^2 is minimised by gradient descent, w_new = w_old - eta * grad E_in, with partial derivatives dE_in/dw_j = (2/N) sum (y_i - w^T x_i)(-x_i^j) and matrix form grad E_in proportional to -X^T (Y - X w). The prediction is still the sign of w^T x. Learning rate eta sets the step size and a stopping rule ends the iteration.ADALINE and Gradient Descent →
result exact minimiser of EinE_{\text{in}} (if XTXX^TX invertible) approaches it step by step
needs one matrix inversion of size (number of features) learning rate, stopping rule
same gradient 2NXT(Xw−Y)\frac2NX^T(X\mathbf{w}-Y) the update is −η-\eta times this (the sign convention of the ADALINE page is +ηXT(Y−XW)+\eta X^T(Y-XW))

5. Polynomial regression (from the exercises)

The exercises use the same formula for polynomial hypotheses. A quadratic hypothesis h(x)=w0+w1x+w2x2h(x)=w_0+w_1x+w_2x^2 is linear in the weights, so one builds a new data matrix with the columns 11, xx, x2x^2 (called X_reg) and applies w=(XTX)−1XTY\mathbf{w}=(X^TX)^{-1}X^TY unchanged. The exercises generate data from a cubic oracle aX3+bX2+cX+daX^3+bX^2+cX+d plus Gaussian noise, fit a quadratic and plot the curve; a final question asks why the fitted parabola "looks like a line" and suggests plotting it on a much wider range. The statements and solutions are in Python Notebooks - Exercises on Perceptron, Regression and Logistic RegressionThe exercise notebooks of the course: generating two- and three-class data with NumPy distributions, training an ADALINE/perceptron by mini-batch stochastic gradient descent and plotting the errors per epoch, polynomial regression by the closed form w = (X^T X)^{-1} X^T y on a data matrix with columns 1, x, x^2, and logistic regression trained by mini-batch gradient descent on the log loss. Statement and solution code are given, with the inconsistencies between statement and code pointed out.Python Notebooks - Exercises on Perceptron, Regression and Logistic Regression →. The course source has no handwritten lecture on polynomial regression, on the choice of the degree or on overfitting beyond the sketches of The Statistical Learning Framework and Empirical Risk MinimizationA hypothesis is judged by an error function computed pointwise on the data and averaged: the in-sample (empirical) error E_in(h) = (1/N) sum of e(f(x_i), h(x_i)); with squared differences it is the sum of squared errors. The learning algorithms of the course all search the hypothesis set for the h with small in-sample error, but what matters is the error on new inputs drawn from the same input distribution, so the data must cover the regions where the inputs live, and part of the data must be held back to check the result.The Statistical Learning Framework and Empirical Risk Minimization →.

Versione ripasso

Teoria collegata