本文介绍了TypeError:输入错误:N = 5不能超过M = 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use scipy.optimize.curve_fit with a custom fit function (roughly following this tutorial):

# 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不断抛出此错误,而我在代码中找不到该问题:

For some reason, Jupyter Notebook keeps throwing this error and I can't locate the problem in the code:

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

我不明白 N M 指的是什么,但是我读过某个地方,当数据点少于参数时,抛出此错误(系统欠佳))-这不是 情况,因为图像文件每个都有大约15 x 15 = 225个数据点.可能是什么原因引起的麻烦?

I don't understand what N and M refer to, but I've read somewhere that this error gets thrown when there are fewer datapoints than parameters (underdetermined system) - which is not the case here as the image files have about 15 x 15 = 225 datapoints each. What might be causing the trouble?

推荐答案

可能您需要将函数更改为

Probably you need to change the function to

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)

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

because only the first variable is treated as independent.

当前,您在 x 变量内发送一个数组,该数组是从两个1D数组进行 np.vstack 编辑的,因此 M = 2 :您有两个数据点.在该函数中,所有其他参数都被视为要优化的参数(包括 y !),因此, N = 5 .

Currently you send an array inside x variable which is np.vstack-ed from two 1D arrays, hence the M=2: you have two datapoints. In the function all other arguments are treated as parameters to optimize (including y!), hence the N=5.

这篇关于TypeError:输入错误:N = 5不能超过M = 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:57