Linear Regression
In questa pagina 5
Regression: the output 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 scattered around it, with vertical dotted segments from each point to the line. Given a new input , what is the output ? The hypothesis is a line; the error of one sample is the squared vertical distance , so
2. Matrix form of the error
Let be the error of sample and . The sum is
Stack the examples as rows of the data matrix (row is , columns are the features ) and the outputs in the column :
Then the vector (both ) has exactly the entries , and
3. The closed-form solution
The page "Regression Problem" differentiates and sets the gradient to zero:
(the factor is non-zero and can be dropped). Expand and rearrange:
Multiply both sides on the left by ; since ,
The matrix is called the pseudo-inverse of : itself is , not square, and has no inverse ; the page notes this next to the formula ("inverse of : "). The formula assumes that is invertible.
Cost of the formula. The last page of the lecture takes a data matrix with about rows and columns (the digits of the handwritten sizes are small and slightly uncertain). Then is and
so the matrix to invert, , is only however many samples there are. The number of rows 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 (if invertible) | approaches it step by step |
| needs | one matrix inversion of size (number of features) | learning rate, stopping rule |
| same gradient | the update is times this (the sign convention of the ADALINE page is ) |
5. Polynomial regression (from the exercises)
The exercises use the same formula for polynomial hypotheses. A quadratic hypothesis is linear in the weights, so one builds a new data matrix with the columns , , (called X_reg) and applies unchanged. The exercises generate data from a cubic oracle 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
- Hypothesis with ; output a real number. In-sample error (squared vertical distance of each sample from the line).
- Data matrix : row is (); is . Since with : .
- Gradient gives the normal equations , hence
- is the pseudo-inverse of ( is not square, so does not exist); needs invertible.
- Cost: with rows and features, is ; only that matrix is inverted.
- Closed form gives the exact minimiser; 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 → reaches it step by step.
- First sketch: a straight line and samples around it, with vertical dotted segments from each point to the line; the error of a sample is the squared vertical distance.
- Table: closed form needs one inversion of size (number of features); gradient descent needs and a stopping rule. The gradient is the same, .
- Polynomial regression: is linear in the weights; use the columns (
X_reg) in the same formula. - Typical errors: forgetting the bias column; wrong shapes (the result is ); calling the inverse of a non-square matrix.
- Details of the exercises: 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 →.