欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python小记:9.求多维坐标点之间的距离、均方误差(MSE)、均方根误差(RMSE)、平均绝对误差(MAE)等

程序员文章站 2022-04-12 08:32:12
1.多维坐标点之间的距离计算: import math def distance(p0,p1,digits=2): a=map(lambda x: (x[0]-x[1])**2, zip(p0, p1)) return round(math.sqrt(sum(a)),digits)测试:p0=(1,4,5)p1=(1,3,5)print(distance(p0,p1))print(" ")p2=(1,2)p3=(2,3)print(dist...

1.多维坐标点之间的距离计算:

    import math
    def distance(p0,p1,digits=2):
        a=map(lambda x: (x[0]-x[1])**2, zip(p0, p1))
        return round(math.sqrt(sum(a)),digits)

测试:

p0=(1,4,5)
p1=(1,3,5)
print(distance(p0,p1))
print(" ")

p2=(1,2)
p3=(2,3)
print(distance(p2,p3,3))
print(" ")

结果:

1.0

1.414

Press any key to continue . . .

2.多维坐标点相关计算:


a=[1,0,1]
b=[1,0,2]


error = []
for i in range(len(a)):
    error.append(a[i] - b[i])

print("Errors: ", error)
print(error)

squaredError = []
absError = []
for val in error:
    squaredError.append(val * val)#a-b之差平方
    absError.append(abs(val))#误差绝对值

print("Square Error: ", squaredError)
print("Absolute Value of Error: ", absError)
print("MSE = ", sum(squaredError) / len(squaredError))#均方误差MSE

from math import sqrt
print("RMSE = ", sqrt(sum(squaredError) / len(squaredError)))#均方根误差RMSE
print("MAE = ", sum(absError) / len(absError))#平均绝对误差MAE

aDeviation = []
aMean = sum(a) / len(a)#a平均值
for val in a:
    aDeviation.append((val - aMean) * (val - aMean))
print("a efficiency = ", 1-sum(squaredError) / sum(aDeviation))#效率系数E

本文地址:https://blog.csdn.net/weixin_44671418/article/details/107309968