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

霸气de小男生 提交于 2021-02-11 14:19:40

问题


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)

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

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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/63432150/typeerror-improper-input-n-5-must-not-exceed-m-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!