开始使用免费开始使用

扩展到多条数据点

您已经看到,不同的权重在单次预测上的准确性会不同。但在实际中,通常需要在多条数据上评估模型的准确性。现在,您将编写代码,比较两组不同权重的模型准确性,这两组权重分别存储在 weights_0weights_1 中。

input_data 是一个数组列表。该列表的每个元素都包含一次单独预测所需的数据。 target_actuals 是一个数字列表。列表中的每个元素都是我们要预测的真实值。

在本练习中,您将使用 sklearn.metrics 中的 mean_squared_error() 函数。它以真实值和预测值作为参数。

您还将使用预加载的 predict_with_network() 函数。该函数以数据数组作为第一个参数,以权重作为第二个参数。

本练习是课程的一部分

Python 深度学习入门

查看课程

练习说明

  • sklearn.metrics 导入 mean_squared_error
  • 使用 for 循环遍历 input_data 的每一行:
    • 使用 predict_with_network()weights_0 对每一行进行预测,并将结果追加到 model_output_0
    • 使用相同的方法对 weights_1 进行预测,并将结果追加到 model_output_1
  • 使用 mean_squared_error() 分别计算 model_output_0model_output_1 的均方误差。第一个参数应为真实值(target_actuals),第二个参数应为预测值(model_output_0model_output_1)。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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)
编辑并运行代码