Linear Regression and Gradient Descent
We examine the linear regression model, mean squared error and least squares. You will learn to find parameters with gradient descent and evaluate results with RMSE and R2.
All topics (2 of 3)
Chapter 1. Machine learning fundamentals
Chapter 2. Neural networks
With a single feature, linear regression approximates the data with a straight line: y = w * x + b, where w is the slope coefficient and b is the intercept. With several features the model takes the form y = w1 * x1 + w2 * x2 + b. The goal is to choose the parameters so that the predictions are as close as possible to the real values, for example to predict a house price from its area and number of rooms.
Closeness is measured by the mean squared error (MSE): MSE = (1/n) * sum((y_i - yhat_i)^2), where yhat_i is the prediction of the model. Because errors are squared, positive and negative deviations do not cancel each other, and large errors are penalized more. In the least squares method the parameters can be computed in a single step with the normal equation: w = (X^T * X)^(-1) * X^T * y; here a column of ones is added to the matrix X, so b also becomes an element of the vector w.
When there are many data points and features, the normal equation becomes expensive; then gradient descent is used. The algorithm computes the gradient of the loss function, that is, the vector of partial derivatives, and updates the parameters with a small step in the direction opposite to the gradient: w = w - lr * dMSE/dw. If the step lr (the learning rate) is too large the process diverges, and if it is too small convergence is slow. For linear regression MSE is a convex function, so there is no local minimum problem, and bringing the features to the same scale speeds up convergence.
Model quality is evaluated on the test set with RMSE (the square root of MSE, in the same units as y) and the coefficient of determination R2, which shows what share of the variance the model explains (the closer to 1, the better). Linear regression assumes that the relationship is roughly linear; outliers strongly affect the result, and correlated features (multicollinearity) make the coefficients unstable. A curved relationship can be modeled with polynomial features, and overfitting can be restrained with regularization (Ridge, Lasso).
Key takeaways
- Linear regression finds the parameters of the model
y = w * x + bfrom data. - Mean squared error (MSE) measures the difference between a prediction and the real value.
- The normal equation is used for small problems and gradient descent for large data.
- Choosing the learning rate
lrwell and scaling the features to the same range speed up convergence. - Quality is assessed with
RMSEandR2; outliers and multicollinearity can distort the result.
Practice tasks
- Calculate by hand: for the points (1, 2), (2, 4) and (3, 5) find
wandbwith the least squares method, then compute the MSE. - Write gradient descent yourself in NumPy: generate data with
x = np.random.rand(100)andy = 3 * x + 2plus a little noise, learnwandbover 1000 steps and compare the loss curves forlrequal to 0.001, 0.1 and 1.0. - On an open dataset (for example,
load_diabetes) train theLinearRegressionandRidgemodels fromscikit-learn, computeRMSEandR2on the test set and compare the results.
Review questions
Test yourself: think first, then reveal the answer.
1Why are errors squared in the mean squared error?Show answerHide answer
So that positive and negative deviations do not cancel out, large errors are penalized more, and the function is smooth (differentiable).2What happens if the learning rate `lr` is chosen too large?Show answerHide answer
The parameters jump around the minimum, the error does not decrease and may even grow (divergence).3What does an `R2` value close to 0 mean?Show answerHide answer
The model explains almost none of the variance: it is no better than simply predicting the mean.
0 of 3 topics completed
Sign in to mark