https://github.com/danferns/linear-regression-visualizer
Visualize Gradient Descent applied on Linear Regression
https://github.com/danferns/linear-regression-visualizer
data-visualization geogebra linear-regression
Last synced: 5 months ago
JSON representation
Visualize Gradient Descent applied on Linear Regression
- Host: GitHub
- URL: https://github.com/danferns/linear-regression-visualizer
- Owner: danferns
- Created: 2022-10-30T14:35:25.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-29T09:36:44.000Z (about 2 years ago)
- Last Synced: 2025-06-12T07:48:11.060Z (about 1 year ago)
- Topics: data-visualization, geogebra, linear-regression
- Language: HTML
- Homepage: https://danferns.github.io/linear-regression-visualizer/
- Size: 15.8 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Linear Regression Visualizer
This project demonstrates how the Gradient Descent algorithm can find the line of best fit for a given set of points.

Each time you open the page, some points are randomly generated (marked by `×`) which vaguely resemble a line.
We also have an actual line (in green) defined by slope `M` and intercept `B`:
```math
y = Mx + B
```
The goal of the algorithm is to get the line to match the points as closely as possible.
## Slope-Intercept Plane
On the top left view, you can see a plane with the `M` and `B` axes. Since a line can be defined solely by these two values,
every point on this plane corresponds to a unique line. You can click and drag the `M`, `B`, and `Line` points in this view
to see how the line changes as you change these values.
## Error Function
Linear Regression uses a cost/error function to find out how far the line is from the points. The function takes the
vertical distances from the line to each of the points (indicated by the brown arrows), squares them individually, and then
sums up the squares:
```math
Error(M, B) = \sum_{i} (y(x_i) - y_i) ^ 2
```
Intuitively, we know that if we move the line, we also change the error value. The 3D plot on the bottom left shows us how
the cost varies for the lines represented by the points on the Slope-Intercept plane.
## Gradient Descent
We can see that the cost function has a bowl-like shape to it, and the lowest point of this bowl is where the error value
is minimum. The `M` and `B` values at this point define the line of best fit (the line with the least error).
The Gradient Descent algorithm starts out with any values for `M` and `B`, and then adjusts them in the direction in which the
error will be reduced the most. It does this adjustment many times, descending down the error function until it reaches close
enough to the local minima.
It knows the direction to move towards by finding the
[Gradient](https://www.khanacademy.org/math/multivariable-calculus/multivariable-derivatives/gradient-and-directional-derivatives/v/gradient)
of the error function, and moving in the direction that is opposite to the gradient vector at the current point.