【问题标题】:TypeError: Improper input: N=5 must not exceed M=2TypeError:输入不当:N=5 不得超过 M=2
【发布时间】:2020-12-05 11:39:33
【问题描述】:

我正在尝试将scipy.optimize.curve_fit 与自定义拟合函数一起使用(大致遵循this 教程):

# Fit function
def fit_function(x, y, x0, y0, A, FWHM):
    return A*np.exp(1)*4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2*np.exp(-4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2)

# Open image file
img = Image.open('/home/user/image.tif')

# xdata
X, Y = img.size
xRange = np.arange(1, X+1)
yRange = np.arange(1, Y+1)
xGrid, yGrid = np.meshgrid(xRange, yRange)
xyGrid = np.vstack((xGrid.ravel(), yGrid.ravel()))

# ydata
imgArray = np.array(img)
imgArrayFlat = imgArray.ravel()

# Fitting
params_opt, params_cov = curve_fit(fit_function, xyGrid, imgArrayFlat)

由于某种原因,Jupyter Notebook 一直抛出这个错误,我在代码中找不到问题:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-61-eaa3ebdb6469> in <module>()
     17     imgArrayFlat = imgArray.ravel()    # Flatten 2D pixel data into 1D array for scipy.optimize.curve_fit
     18 
---> 19     params_opt, params_cov = curve_fit(doughnut, xyGrid, imgArrayFlat)

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
    749         # Remove full_output from kwargs, otherwise we're passing it in twice.
    750         return_full = kwargs.pop('full_output', False)
--> 751         res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
    752         popt, pcov, infodict, errmsg, ier = res
    753         cost = np.sum(infodict['fvec'] ** 2)

/usr/lib/python3/dist-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    384     m = shape[0]
    385     if n > m:
--> 386         raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
    387     if epsfcn is None:
    388         epsfcn = finfo(dtype).eps

TypeError: Improper input: N=5 must not exceed M=2

我不明白 NM 指的是什么,但我在某处读到,当数据点少于参数(未确定的系统)时会引发此错误 - 不是 em> 这里的情况是图像文件每个大约有 15 x 15 = 225 个数据点。什么可能导致问题?

【问题讨论】:

    标签: arrays scipy curve-fitting shapes scipy-optimize


    【解决方案1】:

    可能你需要把函数改成

    def fit_function(X, x0, y0, A, FWHM):
        x, y = X
        return A*np.exp(1)*4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2*np.exp(-4*np.log(2)*((x+x0)**2 + (y+y0)**2)/FWHM**2)
    

    因为只有第一个变量被视为独立变量。

    目前,您在x 变量中发送一个数组,该数组是来自两个一维数组的np.vstack-ed,因此M=2:您有两个数据点。在函数中,所有其他参数都被视为要优化的参数(包括y!),因此N=5

    【讨论】:

    • 嘿嘿,这似乎行得通!你能解释一下会发生什么吗?这看起来像是某种变量打包/解包,但不是*args,您使用的是x, y = X...这相当于(x, y) = X
    • @frankmertens 它按行解包 np.array。要按列解压,您可以使用x, y = X.T。相当于(x, y) = X
    • 啊,这是特定的 NumPy 语法吗?
    • @frankmertens 不。尝试使用简单的嵌套 python 列表。a = [[1, 2, 3], [4, 5, 6]]c, d = a。当然转置方法T 不适用于python 列表。
    • 太棒了,谢谢!顺便说一句,有没有解释为什么它是c, d = a 而不是a = c, d? AFAIK = 是对象之间的指针,foo = bar 指向 foobar
    猜你喜欢
    • 2016-07-17
    • 2021-03-22
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多