Linear Regression From Scratch, Then scikit-learn
By hand and with scikit-learn, the slope came out at 5.77 both times. The true slope was 5.
Video #182 ยท Machine learning and data science
Steps in the video
Files
- data.py (10 lines)
- run.py (32 lines)
- scratch.py (13 lines)
Tested with
Python 3.12.15 | numpy==2.5.4, scikit-learn==1.9.1, scipy==1.18.1, joblib==1.6.0, narwhals==2.27.1, threadpoolctl==3.7.0, cloudpickle==3.1.2
What this does not show
Made-up data with one input and a truly straight line, scored on the same data it was fitted to.
Sources
- https://en.wikipedia.org/wiki/Simple_linear_regression
- https://numpy.org/doc/stable/reference/generated/numpy.linalg.lstsq.html
- https://pypi.org/pypi/numpy/json
- https://pypi.org/pypi/scikit-learn/json
- https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html
- https://scikit-learn.org/stable/modules/linear_model.html
Learning IT by Example.