Python编程:方差、标准差、均方差、均方根值、均方误差、均方根误差
【摘要】 缩写英文中文RMSRRoot Mean Squared Error均方根误差MAEMean Absolute Error平均绝对误差
python实现代码
# -*- coding: utf-8 -*-
import math
def get_average(records): """ 平均值 """ return sum(records) / len(reco...
缩写 | 英文 | 中文 |
---|---|---|
RMSR | Root Mean Squared Error | 均方根误差 |
MAE | Mean Absolute Error | 平均绝对误差 |
python实现代码
# -*- coding: utf-8 -*-
import math
def get_average(records): """ 平均值 """ return sum(records) / len(records)
def get_variance(records): """ 方差 反映一个数据集的离散程度 """ average = get_average(records) return sum([(x - average) ** 2 for x in records]) / len(records)
def get_standard_deviation(records): """ 标准差 == 均方差 反映一个数据集的离散程度 """ variance = get_variance(records) return math.sqrt(variance)
def get_rms(records): """ 均方根值 反映的是有效值而不是平均值 """ return math.sqrt(sum([x ** 2 for x in records]) / len(records))
def get_mse(records_real, records_predict): """ 均方误差 估计值与真值 偏差 """ if len(records_real) == len(records_predict): return sum([(x - y) ** 2 for x, y in zip(records_real, records_predict)]) / len(records_real) else: return None
def get_rmse(records_real, records_predict): """ 均方根误差:是均方误差的算术平方根 """ mse = get_mse(records_real, records_predict) if mse: return math.sqrt(mse) else: return None
def get_mae(records_real, records_predict): """ 平均绝对误差 """ if len(records_real) == len(records_predict): return sum([abs(x - y) for x, y in zip(records_real, records_predict)]) / len(records_real) else: return None
if __name__ == '__main__': records1 = [3, 4, 5] records2 = [2, 4, 6] # 平均值 average1 = get_average(records1) # 4.0 average2 = get_average(records2) # 4.0 # 方差 variance1 = get_variance(records1) # 0.66 variance2 = get_variance(records2) # 2.66 # 标准差 std_deviation1 = get_standard_deviation(records1) # 0.81 std_deviation2 = get_standard_deviation(records2) # 1.63 # 均方根 rms1 = get_rms(records1) # 4.08 rms2 = get_rms(records2) # 4.32 # 均方误差 mse = get_mse(records1, records2) # 0.66 # 均方根误差 rmse = get_rmse(records1, records2) # 0.81 # 平均绝对误差 mae = get_mae(records1, records2) # 0.66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
公式参考:
方差(variance)、标准差(Standard Deviation)、均方差、均方根值(RMS)、均方误差(MSE)、均方根误差(RMSE)
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/87936476
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)