python如何实现均方误差和均方根误差?

Python (229) 2023-05-25 21:35:50

一、python实现均方误差

均方误差是各数据偏离真实值的距离平方和的平均数,也即误差平方和的平均数。

用法:一般被用在机器学习的预测值与真实值之间的距离。均方误差对应的是最小二乘法。

#-*-coding:utf-8-*-
importmath

defget_mse(records_real,records_predict):
"""
均方误差
"""
iflen(records_real)==len(records_predict):
returnsum([(x-y)**2forx,yinzip(records_real,records_predict)])/len(records_real)
else:
returnNone

二、python实现均方根误差

均方根误差亦称标准误差,是均方误差的算术平方根。

#-*-coding:utf-8-*-
importmath

defget_rmse(records_real,records_predict):
"""
均方根误差
"""
mse=get_mse(records_real,records_predict)
ifmse:
returnmath.sqrt(mse)
else:
returnNone
THE END

发表回复