适合一些复杂的功能

发布于 2025-01-22 23:09:38 字数 869 浏览 1 评论 0原文

我正在尝试使用复杂功能拟合数据,该功能包括一些特殊的功能,并且对此有问题。它是此处介绍的公式(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2025-01-29 23:09:38

问题在于您的函数“曲线”不接受numpy数组或列表作为输入,但是您可以对其进行矢量化:

from scipy.optimize import curve_fit
from scipy import integrate
import matplotlib.pyplot as plt
import numpy as np
import random

def integrand(ξ, s, λ):
    return np.exp(-ξ**2-2*ξ*λ)*np.cos(2*ξ*s)

@np.vectorize
def curve(s, λ):
    return s/λ*np.exp(-s**2/λ**2)*integrate.quad(integrand, 0, np.infty, args=(s, λ))[0]

x = np.linspace(0,3, 100)
y = curve(x,1) + np.random.normal(0, 0.005, 100)

params, cov = curve_fit(curve, x, y, p0=[0.3])

plt.scatter(x,y, s=4)
plt.plot(x, curve(x,params[0]))
plt.show()

对于更优雅的矢量化方法,尝试执行函数阵列兼容或在此处查看:

< 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:

from scipy.optimize import curve_fit
from scipy import integrate
import matplotlib.pyplot as plt
import numpy as np
import random

def integrand(ξ, s, λ):
    return np.exp(-ξ**2-2*ξ*λ)*np.cos(2*ξ*s)

@np.vectorize
def curve(s, λ):
    return s/λ*np.exp(-s**2/λ**2)*integrate.quad(integrand, 0, np.infty, args=(s, λ))[0]

x = np.linspace(0,3, 100)
y = curve(x,1) + np.random.normal(0, 0.005, 100)

params, cov = curve_fit(curve, x, y, p0=[0.3])

plt.scatter(x,y, s=4)
plt.plot(x, curve(x,params[0]))
plt.show()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文