适合一些复杂的功能
我正在尝试使用复杂功能拟合数据,该功能包括一些特殊的功能,并且对此有问题。它是此处介绍的公式(imgur)。为此,我使用spicy.optimize.curve。但是为了简单起见,我会跳过特殊功能,并且想与类似功能相吻合。我的问题是,我想使用一些函数的功能将数据拟合,但这也隐藏在一个积分中。
在这里,仅对于这篇文章,我生成了一些带有正常分布的数据,而不是我的情节的好数据,而只是为了运行代码。我有一个错误,
only size-1 arrays can be converted to Python scalars
我的代码在下面介绍。因此,lambda参数是我想从拟合中获得的参数。
import numpy as np
from scipy.optimize import curve_fit
import random
from scipy import integrate
def integrand(ξ, s, λ):
return np.exp(-ξ**2-2*ξ*λ)*np.cos(2*ξ*s)
def curve(s, ξ, λ):
return s/λ*np.exp(-s**2/λ**2)*integrate.quad(integrand, 0, np.infty, args=(s, λ))[0]
X = np.random.normal(0, 1, 10)
Y = np.random.normal(0, 1, 10)
X = np.array(X)
Y = np.array(Y)
params, cov = curve_fit(curve, X, Y)
I am trying to fit data with a complicated function which consists some special function and I have problem with it. It is formula presented here (imgur). To do this I use spicy.optimize.curve. But for simplicity here, I would skip special functions and I want to do a fit with a similiar function. My problem is that I want to fit my data with some functionm which is function of some s, but this s is also hidden in an integral.
Here, just for this post I generated some data with the normal distributions, it is not the good data for my plot, just to run the code. I got an error
only size-1 arrays can be converted to Python scalars
My code is presented below. Thus, the lambda parameter is the one I want to get from the fit.
import numpy as np
from scipy.optimize import curve_fit
import random
from scipy import integrate
def integrand(ξ, s, λ):
return np.exp(-ξ**2-2*ξ*λ)*np.cos(2*ξ*s)
def curve(s, ξ, λ):
return s/λ*np.exp(-s**2/λ**2)*integrate.quad(integrand, 0, np.infty, args=(s, λ))[0]
X = np.random.normal(0, 1, 10)
Y = np.random.normal(0, 1, 10)
X = np.array(X)
Y = np.array(Y)
params, cov = curve_fit(curve, X, Y)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的函数“曲线”不接受numpy数组或列表作为输入,但是您可以对其进行矢量化:
对于更优雅的矢量化方法,尝试执行函数阵列兼容或在此处查看:
< a href =“ https://numpy.org/doc/stable/reference/generated/numpy.dectorize.html” 。vectorize.html
The problem is that your function "curve" doesn't admit a numpy array or list as an input, but you can vectorize it:
For more elegant ways of vectorizing, try to do your function array compatible, or take a look here:
https://numpy.org/doc/stable/reference/generated/numpy.vectorize.html