# -*- 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
具体对某组数据操作可参考下例:
#导入math包
import math
#定义点的函数
class Point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def getx(self):
return self.x
def gety(self):
return self.y
#定义直线函数
class Getlen:
def __init__(self,p1,p2):
self.x=p1.getx()-p2.getx()
self.y=p1.gety()-p2.gety()
#用math.sqrt()求平方根
self.len= math.sqrt((self.x**2)+(self.y**2))
#定义得到直线长度的函数
def getlen(self):
return self.len
#设置点p1的坐标为(0,0)
p1=Point(0,0)
#设置点p2的坐标为(3,4)
p2=Point(3,4)
#定义对象
l=Getlen(p1,p2)
#获取两点之间直线的长度
d=l.getlen())
或者
import numpy as np
import math
p1=np.array([0,0])
p2=np.array([1000,2000])
p3=p2-p1
p4=math.hypot(p3[0],p3[1])
print(p4)