The model as a Python function
Remember Week 8: a function takes an input and returns an output, just like a model. The function predict_score(hours) holds the model with w = 10 and b = 40 and returns w * hours + b. The calls give 60 for 2 hours, 40 for 0 hours and 90 for 5 hours. But who found the numbers 10 and 40? A person typed them. The computer did not learn them. In machine learning the computer adjusts w and b by itself: it makes a prediction, measures the mistake (prediction minus the real value), changes w and b a little and tries again. The table shows three tries for one invented data point, x = 2 with the real mark 65. The mistake gets smaller until it is 0. Real training uses many data points and does this very fast. A model works well only inside the range of its data: predict_score(20) gives 240 marks, which is impossible if the maximum mark is 100.
1def predict_score(hours): 2 w = 10 3 b = 40 4 return w * hours + b 5 6print(predict_score(2)) 7print(predict_score(0)) 8print(predict_score(5))
60 40 90
| Try | w | b | Prediction for x = 2 | Mistake (prediction - real 65) |
|---|---|---|---|---|
| 1 | 5 | 30 | 40 | -25 |
| 2 | 8 | 38 | 54 | -11 |
| 3 | 10 | 45 | 65 | 0 |
1def predict_score(hours): 2 w = 10 3 b = 40 4 return w * hours + b 5 6print(predict_score(20))
240
Write a function predict_height(days) for a plant with b = 25 and w = 4, and work out the results for 0, 2 and 10 days. Then say what is typed by a person here and what a trained model would find by itself.
1def predict_score(hours): 2 w = 10 3 b = 40 4 return w * hours + b 5 6print(predict_score(2)) 7print(predict_score(0)) 8print(predict_score(5))