Skip to main content
Informatik.uz
Master’s · Year 1Chapter 1. Machine learning fundamentals

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.

Topic 2 of 33 min readUpdated October 27, 2026
All topics (2 of 3)

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 + b from 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 lr well and scaling the features to the same range speed up convergence.
  • Quality is assessed with RMSE and R2; outliers and multicollinearity can distort the result.

Practice tasks

  1. Calculate by hand: for the points (1, 2), (2, 4) and (3, 5) find w and b with the least squares method, then compute the MSE.
  2. Write gradient descent yourself in NumPy: generate data with x = np.random.rand(100) and y = 3 * x + 2 plus a little noise, learn w and b over 1000 steps and compare the loss curves for lr equal to 0.001, 0.1 and 1.0.
  3. On an open dataset (for example, load_diabetes) train the LinearRegression and Ridge models from scikit-learn, compute RMSE and R2 on the test set and compare the results.

Review questions

Test yourself: think first, then reveal the answer.

  1. 1Why are errors squared in the mean squared error?
    So that positive and negative deviations do not cancel out, large errors are penalized more, and the function is smooth (differentiable).
  2. 2What happens if the learning rate `lr` is chosen too large?
    The parameters jump around the minimum, the error does not decrease and may even grow (divergence).
  3. 3What does an `R2` value close to 0 mean?
    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