数据线性拟合
作者:Sun zi chao     发布时间:2022-02-05 12:00:00    阅读次数:86

import numpy as np 
import matplotlib.pyplot as plot 

x = np.array([12,34,3,56,34,67,23,45])
y = np.array([11,29,4,12,17,19,20,21])
m = len(x)

sum_x = np.sum(x)
sum_y = np.sum(y)
sum_xy = np.sum(x*y)
sum_xx = np.sum(x**2)
a = (m*sum_xy-sum_x*sum_y)/(m*sum_xx-(sum_x)**2)
b = (sum_y*sum_xx - sum_x*sum_xy) / (m*sum_xx-(sum_x)**2)

#y = ax+b
print(a,b)

yy = x*a+b

fig,ax = plot.subplots()

ax.scatter(x,y)
ax.plot(x,yy,'red')
plot.show()


桂ICP备11003301号-1 公安备案号:45040302000027 Copyright @ 2021- 2022 By Sun zi chao

阅读统计: 1.93W 文章数量: 76 运行天数: 416天 返回cmnsoft