Session Ready
Exercise

Testing on linear data

The model_test() function, which measures how well the model fits unseen data, returns a quantity called \(r^2\) which is very difficult to compute in the general case. Therefore, you need to find special testing sets where computing \(r^2\) is easy.

One important special case is when the model fits the testing set perfectly. This happens when the testing set is perfectly linear. One such testing set is printed out in the IPython console for you.

In this special case, model_test() should return 1.0 if the model's slope and intercept match the testing set, because 1.0 is usually the highest possible value that \(r^2\) can take.

Remember that for data points \((x_n, y_n)\), the slope is \(\frac{y_2 - y_1}{x_2 -x_1}\) and the intercept is \(y_1 - \textrm{slope} \times x_1\).

Instructions
100 XP
  • Assign the variable test_argument to a NumPy array holding the perfectly linear testing data printed out in the IPython console.
  • Assign the variable expected to the expected value of \(r^2\) in the special case of a perfect fit.
  • Fill in with the model's slope and intercept that matches the testing set.
  • Remembering that actual is a float, complete the assert statement to check if actual returned by model_test() is equal to the expected return value expected.