Linear RegressionCode with Python
Time to write it in Python. Most machine learning in Python uses a free library called scikit-learn, which does the training maths for you. Here is Riya's model in a few lines (the Run button shows what Python prints; to run it yourself, open a free notebook such as Google Colab).
tempsis a list of lists: each inner list holds one day's features. Here there is one feature, so each list has one number.model.fit(temps, sales)is the training step — it finds the best slope and intercept.model.predict(...)is the prediction step.
Run the program, then pick a different temperature and run it again.
The Formula, Without a Library
Libraries are handy, but there is no magic inside. This program is the formula shortcut from earlier, written with a plain loop. Change the number of kulfis sold on the 40 °C day — 200 is a festival day, 60 is a power cut — and see how one unusual day moves m and c.
Gradient Descent as a Loop
And here is the error valley as code. The outer loop takes the steps; the inner loop adds up the slope of the valley from every day's error. Try the learning rates from the valley widget.
With a rate of 0.01 the slope settles at 6 in a few dozen steps; with 0.05 it explodes, exactly like the ball that flew out of the valley.