#182 Machine learning and data science 6.2 min
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.
Predict zero hours
Copy the code folder first. In run.py change both 6 values, in the by_hand line and in model.predict, to 0. Predict how the by hand number will compare with the intercept printed above it, then run python run.py to check. The printed label still says 6 hours; ignore it.
A hint
Put 0 into the line: intercept plus slope times hours.
Pass a flat list
In run.py change the line X = hours.reshape(-1, 1) to X = hours. Predict whether scikit-learn accepts it. Run the file and read the error message. Then put the reshape back. Why does scikit-learn want one row per student?
A hint
The video says it wants a table with one row per student and one column per input.
Switch the noise off
In data.py change the noise spread from 6 to 0. Now the scores sit exactly on the true line. Predict the slope, the intercept and R squared for both fits before you run python run.py. Compare with your prediction. Why was the slope not exactly the true one before?
A hint
With no noise nothing pulls the points off the line 40 plus 5 times hours, so a straight line can fit them exactly.
More students, other seeds
Change the number of students in make_data from 40 to a larger number, and run the file with three different seeds. Predict whether the fitted slope tends to land closer to the true slope of 5 with more students, then check your three runs. Keep the noise at 6.
A hint
Edit n and seed in the make_data definition in data.py. Random noise tends to average out with more points.
These tasks are for you to try on your own machine. Nothing is checked or saved.
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.
Up next in Machine learning and data science
More in Machine learning and data science
Related videos
- #186 Data Leakage: The Bug That Fakes Accuracy, Reproduced Machine learning and data science
- #187 Feature Engineering Beat 500 Trees, on Made-Up Data Machine learning and data science
- #193 Clustering: k-means and When It Fails, in Python Machine learning and data science
- #194 Anomaly Detection Basics: z-Score, IQR, Isolation Forest Machine learning and data science
