Get startedGet started for free

Scaling up to multiple data points

You've seen how different weights will have different accuracies on a single prediction. But usually, you'll want to measure model accuracy on many points. You'll now write code to compare model accuracies for two different sets of weights, which have been stored as weights_0 and weights_1.

input_data is a list of arrays. Each item in that list contains the data to make a single prediction. target_actuals is a list of numbers. Each item in that list is the actual value we are trying to predict.

In this exercise, you'll use the mean_squared_error() function from sklearn.metrics. It takes the true values and the predicted values as arguments.

You'll also use the preloaded predict_with_network() function, which takes an array of data as the first argument, and weights as the second argument.

This exercise is part of the course

Introduction to Deep Learning in Python

View Course

Exercise instructions

  • Import mean_squared_error from sklearn.metrics.
  • Using a for loop to iterate over each row of input_data:
    • Make predictions for each row with weights_0 using the predict_with_network() function and append it to model_output_0.
    • Do the same for weights_1, appending the predictions to model_output_1.
  • Calculate the mean squared error of model_output_0 and then model_output_1 using the mean_squared_error() function. The first argument should be the actual values (target_actuals), and the second argument should be the predicted values (model_output_0 or model_output_1).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from sklearn.metrics import mean_squared_error

# Create model_output_0 
model_output_0 = []
# Create model_output_1
model_output_1 = []

# Loop over input_data
for row in input_data:
    # Append prediction to model_output_0
    model_output_0.append(____)
    
    # Append prediction to model_output_1
    model_output_1.append(____)

# Calculate the mean squared error for model_output_0: mse_0
mse_0 = ____

# Calculate the mean squared error for model_output_1: mse_1
mse_1 = ____

# Print mse_0 and mse_1
print("Mean squared error with weights_0: %f" %mse_0)
print("Mean squared error with weights_1: %f" %mse_1)
Edit and Run Code