How do you do linear regression?
By Forinfos - 26/03/2026 - 0 comments
Linear regression is a curve-fitting strategy that approximates a function from given data. It plots a straight line through a curve without necessarily touching the data points. Since the approximated function is linear, it follows the formula: y = mx + b.
- Calculate the mean of both x and y
It is necessary to first find the averages, or means, of both sets of data points. In order to do this, add the value of all the data points under x together, then divide the result by the actual number of data points. The same is done for the y data points.
- Find the slope
To find the slope, employ the following equation: m = ((n * sum(x, y)) - (sum(x) * sum(y))) / ((n * sum(x^2)) - (sum(x))^2). N is the actual number of plotted coordinates on the curve.
- Find the y intercept
The following equation determines the y-intercept: b = y_bar - m * x_bar. M is the calculated slope, x_bar is the average of the x coordinates and y_bar is the average of the y coordinates.
- Determine the approximated function
Use the calculated values to determine the function obtained by linear regression: f(x) = mx + b.

Comments
Write a comment