python matplotlib 散点图的拟合直线的简单示例
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 | # sample points X = [0, 5, 10, 15, 20] Y = [0, 7, 10, 13, 20] # solve for a and b def best_fit(X, Y): xbar = sum(X) / len(X) ybar = sum(Y) / len(Y) n = len(X) # or len(Y) numer = sum([xi * yi for xi, yi in zip(X, Y)]) - n * xbar * ybar denum = sum([xi ** 2 for xi in X]) - n * xbar ** 2 b = numer / denum a = ybar - b * xbar print( 'best fit line:\ny = {:.2f} + {:.2f}x' .format(a, b)) return a, b # solution a, b = best_fit(X, Y) # best fit line: # y = 0.80 + 0.92x # plot points and fit line import matplotlib.pyplot as plt plt.scatter(X, Y) yfit = [a + b * xi for xi in X] plt.plot(X, yfit) plt.show() |
用Scripy实现最小二乘法与股票K线回归例子
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 | import numpy as np import matplotlib.pyplot as plt from scipy.optimize import leastsq ##样本数据(Xi,Yi),需要转换成数组(列表)形式 Xi = np.array([160, 165, 158, 172, 159, 176, 160, 162, 171]) Yi = np.array([58, 63, 57, 65, 62, 66, 58, 59, 62]) ##需要拟合的函数func :指定函数的形状 k= 0.42116973935 b= -8.28830260655 def func(p, x): k, b = p return k * x + b ##偏差函数:x,y都是列表:这里的x,y更上面的Xi,Yi中是一一对应的 def error(p, x, y): return func(p, x) - y # k,b的初始值,可以任意设定,经过几次试验,发现p0的值会影响cost的值:Para[1] p0 = [1, 20] # 把error函数中除了p0以外的参数打包到args中(使用要求) Para = leastsq(error, p0, args=(Xi, Yi)) print(Para) # 读取结果 k, b = Para[0] print( "k=" , k, "b=" , b) # 画样本点 plt.figure(figsize=(8, 6)) ##指定图像比例:8:6 plt.scatter(Xi, Yi, color= "green" , label= "source" , linewidth=2) # 画拟合直线 x = np.linspace(150, 190, 100) ##在150-190直接画100个连续点 y = k * x + b ##函数式 plt.plot(x, y, color= "red" , label= "target" , linewidth=2) plt.legend() # 绘制图例 plt.show() |
参考:https://www.soinside.com/question/QaunXuEAg3coPrUDgwpoYf
http://www.manongjc.com/detail/58-dlzygymeojwhhtj.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-04-23 ATX-uiautomator2 使用 QPython 在 Android 手机内执行自动化 -及wsl安装使用【未成功】