import numpy as np
import matplotlib.pyplot as plt
X = [2, 3, 4, 5, 7, 8, 9]
Y = [2.5, 4.7, 4.5, 6.2, 7.9, 10.5, 8]
# z= numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)[source]
#Deg Degree of the fitting polynomial 1次多项式即为线性,
#deg次数太高会有震荡采用最小二乘多项式拟合,返回多项式的系数
z = np.polyfit(X,Y,1)
print("z",z)
# z [0.93287671 1.26438356]
#为一个数组,若没有参数2,则生成一个多项式,
p = np.poly1d(z)
print("p",p)
# p 0.9329 x + 1.264
x = np.arange(1,11)#设置横坐标
y = z[0] * x + z[1]#设置纵坐标
plt.figure() #创建画板
plt.scatter(X,Y) #设置坐标
plt.plot(x,y)
plt.show()
#转换格式求取差值
print(type(Y))
y1 = y.tolist()
print(type(y1))
ls = []
for index,i in enumerate(Y):
k = round(i - y1[index],2)
ls.append(k)
print(ls)
# [0.3, 1.57, 0.44, 1.2, 1.97, 3.64, 0.21]
最小二乘多项式拟合c语言程序_1.最简单的线性拟合
最新推荐文章于 2023-07-18 09:56:59 发布