learning-it by example

#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.

1. On the 40 made-up students, what did the by-hand fit and the scikit-learn fit give for the slope and R squared?
2. The true line had a slope of 5. Why did the fit find 5.77?
3. What happened when the intercept was switched off, forcing the line through zero?
4. Why does the R squared of 0.929 say nothing about new students?
5. Which of these does the video list as a mistake to avoid?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. 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.

  2. 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.

  3. 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.

  4. 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

All Machine learning and data science videos

Related videos