Question: Part 6 : Test Score We will now calculate the testing r - squared score for our model by repeating the steps from Part 5

Part 6: Test Score
We will now calculate the testing r-squared score for our model by repeating the steps from Part 5, but using the test set
rather than the training set.
Create a markdown cell with a level 2 header that reads "Part 6: Test Score".
In the same cell, add unformatted text explaining that in this section, we will be calculating the test r-squared score, and
that we will start by calculating estimated response values for the test set.
Use the variables beta_0 and beta_1 as well as the list x_test to calculate the estimated response values for the test
set. Store the results in a list named pred_y_test. You can accomplish this task using either a loop or a list
comprehension. This cell should not produce any output.
Create a markdown cell explaining that we will now calculate the residuals for the test set.
Use the values stored in y_test and pred_y_test to calculate the residuals for the test set. Store the results in a list
named error_y_test. You can accomplish this task using either a loop or a list comprehension. This cell should not
produce any output.
Before continuing on with our calculation of the r-squared score, we will display the true values, the predicted values,
and the residual for each of the first 10 observations in the test set.
Create a markdown cell explaining that we will be displaying the values mentioned above.
Use a code cell to print the first 10 values of each of the lists y_test, pred_y_test, and error_y_test. The output
should be formatted in the same way as what was requested for the training set in Part 5.
Create a markdown cell explaining that we will now calculate the sum of squared errors score for the test set.
Use the values stored in the list error_y_test to calculate the testing sum of squared errors score, storing the result in
a variable name sse_test. Display the result with text output as shown below, rounding the displayed value to 4
decimal places.
Test SSE = xxxx
Before calculating the test r-squared score, we must first calculate the value for the test set. We calculated this for the
training set in Part 3, but have not done so for the test set.
Create a markdown cell explaining that we will now calculate the value of on the test set, and will then use that and the
test sum of squared errors to calculate the test r-squared score.
Use the formula from Part 3 to calculate the value for the test set, storing the results in a variable named Syy_test.
Then calculate the test r-squared score, storing the result in a variable named r2_test. Display the result with text
output as shown below, rounding the displayed value to 4 decimal places.
Test r-Squared = xxxx
Create a markdown cell explaining that we will now create a plot to visualize the errors for the observations in the test set.
Copy the code below (without the indentation) into a code cell and then execute that cell.
plt.figure(figsize=[8,4])
plt.scatter(x_test, y_test, c='skyblue', edgecolor='k')
plt.plot([1500,5250],[beta_0+ beta_1*1500, beta_0+ beta_1*5250],
c='crimson', lw=3)
for i in range(n_test):
plt.plot([x_test[i], x_test[i]],[pred_y_test[i], y_test[i]],
c='coral', zorder=0)
plt.show()
Each vertical line shown in this plot represents th

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!