如何使用Scipy使用最小语功能
我试图了解最小值函数的工作方式。我已经检查了文档和以下链接(
摘要我所知道的:具有剩余功能的应将数据从数据到拟合的差异,并返回最适合
我无法理解的数据的参数:< /em>一般而言,我们给出了最小值(1.函数;2。x0=初始值的数组,3。args =(xdata,ydata))。
但是,我遇到的错误是说数组的大小不是相同的,但是如果我有len(2)和xdata(1000)和ydata(1000)的Intial值数组,则它们的大小不一样
。到目前为止,我已经尝试过:
def func(w,coeffs):
ln = 2*(coeffs[0] + (coeffs[1]/w**2))
ld = (coeffs[0] + (coeffs[1]/w**2) +1 )
l = ln/ld
r = (2/(1+(coeffs[0]+(coeffs[1]/w**2))))
res = (l*r)**2
return res
def residuals(coeffs, y, x):
return y - func(x, coeffs)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
x = np.linspace(1,10,1000)
y = -3*np.exp(-x)
x0 = np.array([1.5, 6800], dtype=float)
x2, flag = leastsq(residuals, x0, args = (x,y))
错误消息:valueError:操作数无法与形状(2,)(1000)一起播放,
如果有人可以向我解释此功能的工作原理,我会对其进行评估。
I am trying to understand how the least_square function works. I have checked the documentation and the following link (example least_sqr) which gives me a step-by-step process that I can follow.
However, I cannot get my code to fit some data. The error I am getting is that the arrays are from different sizes. I am quite sure the true issue is that I don't understand what the function is doing.
Summary of what I know: with a residual function the leastsqr should calculate the difference from the data to the fitting, and return parameters which that best fit the data
What I can't understand: in general we give to the leastsqr (1.function; 2. x0 = array of initial values, 3. args = (xdata, ydata)).
However, the error I am getting is saying that the arrays are not the same size, but they should not be the same size, if I have an intial values array of len(2) and xdata(1000) and ydata(1000)
What I have tried so far:
def func(w,coeffs):
ln = 2*(coeffs[0] + (coeffs[1]/w**2))
ld = (coeffs[0] + (coeffs[1]/w**2) +1 )
l = ln/ld
r = (2/(1+(coeffs[0]+(coeffs[1]/w**2))))
res = (l*r)**2
return res
def residuals(coeffs, y, x):
return y - func(x, coeffs)
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
x = np.linspace(1,10,1000)
y = -3*np.exp(-x)
x0 = np.array([1.5, 6800], dtype=float)
x2, flag = leastsq(residuals, x0, args = (x,y))
Error Message: ValueError: operands could not be broadcast together with shapes (2,) (1000,)
If anyone could explain to me how this function works, I would appreaciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论