要计算均方根误差(RMSE),您可以使用以下代码:

import numpy as np

def rmse(predictions, targets):
    return np.sqrt(((predictions - targets) ** 2).mean())

predictions = [1, 2, 3, 4, 5]
targets = [2, 3, 4, 5, 6]

print(rmse(np.array(predictions), np.array(targets)))

在上述代码中,predictions是您的模型给出的预测值列表,targets是对应的目标值列表。函数rmse使用了NumPy库来计算平方差的平均值,并对其进行开方,得到RMSE值。

在这个例子中,预测值为[1, 2, 3, 4, 5],目标值为[2, 3, 4, 5, 6]。运行上述代码将输出RMSE值。

Python RMSE计算:代码示例及详解

原文地址: https://www.cveoy.top/t/topic/pp11 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录