【问题标题】:scipy.optimize.curve_fit - TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))scipy.optimize.curve_fit - TypeError('输入错误:N=%s 不得超过 M=%s' % (n, m))
【发布时间】:2021-08-27 07:47:23
【问题描述】:

我正在尝试优化我编写的函数的参数,该函数旋转点,然后将它们从像素位置转换为特定点的 mm。我遇到了TypeError('Improper input: N=%s must not exceed M=%s' % (n, m)),其他关于 SO 的问题似乎表明我的参数多于数据点。我输入的数据有 26 组 x,y 坐标。任何人都可以看到我在下面的代码中哪里出错了吗?

提前致谢

from scipy import optimize
import numpy as np
import math

def px_to_mm_v4(coords, cf_x, cf_y, nudge_x, nudge_y, center_x, center_y, rotate_degrees):
    
    ## set lower left loc
    ll_x = center_x - (127.76/2/cf_x) ## lower left x location in pixels
    ll_y = center_y + (85.47/2/cf_y) ## lower left y location in pixels
    
    ## unpack coordinates
    x,y = coords
    
    ## rotate points around center
    rotate_radians = math.radians(rotate_degrees)
    x_rotated = center_x + math.cos(rotate_radians) * (x - center_x) - math.sin(rotate_radians) * (y - center_y)
    y_rotated = center_y + math.cos(rotate_radians) * (x - center_x) + math.cos(rotate_radians) * (y - center_y)
    
    ## convert px to mm
    x_converted = (x_rotated - ll_x) * cf_x + nudge_x
    y_converted = (ll_y - y_rotated) * cf_y + nudge_y

    ret_x = x_converted
    ret_y = y_converted
    ret = (ret_x,ret_y)
    return ret


x_px = np.array([1723,1530,1334,1135,943,747,548,2520,2322,2120,1921,1726,1530,1331,1132,937,741,545,346,349,352,355,358,358,361,361,148])
y_px = np.array([596,791,986,1176,1373,1569,1769,1973,1967,1967,1964,1962,1964,1967,1967,1967,1962,1964,1967,1769,1569,1373,1178,986,791,602,2162])

x_mm = np.array([80,70,60,50,40,30,20,120,110,100,90,80,70,60,50,40,30,20,10,10,10,10,10,10,10,10,0])
y_mm = np.array([80,70,60,50,40,30,20,10,10,10,10,10,10,10,10,10,10,10,10,20,30,40,50,60,70,80,0])
test_coords_tup = (x_px,y_px)
points_to_fit_tup = (x_mm,y_mm)

cf_x_test = 0.05072
cf_y_test = 0.05076
nudge_x_test = -2.2
nudge_y_test = 2.1
center_x_test = 1374
center_y_test = 1290
rotate_degrees_test = 1.4

params0 = [cf_x_test,cf_y_test,nudge_x_test,nudge_y_test,center_x_test,center_y_test,rotate_degrees_test]

popt, pcov = optimize.curve_fit(px_to_mm_v4, test_coords_tup, points_to_fit_tup, p0=params0)

编辑:运行代码会返回以下错误

Traceback (most recent call last):
  File "SO_example.py", line 48, in <module>
    popt, pcov = optimize.curve_fit(px_to_mm_v4, test_coords_tup, points_to_fit_tup, p0=params0)
  File "//anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 784, in curve_fit
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File "//anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 414, in leastsq
    raise TypeError('Improper input: N=%s must not exceed M=%s' % (n, m))
TypeError: Improper input: N=7 must not exceed M=2

【问题讨论】:

  • 在搜索其他 SO 时,您是否遇到过发布带有回溯的 full 错误的想法?
  • @hpaulj 对,请参阅帖子中添加的跟踪

标签: python numpy scipy-optimize


【解决方案1】:

来自leastsq docs

func callable
    Should take at least one (possibly length N vector) argument and 
    returns M floating point numbers. It must not return NaNs or fitting 
    might fail. M must be greater than or equal to N.

【讨论】:

  • 针对这个问题,我已经通过最小化使用 lmfit 来进行优化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 2020-12-05
  • 2021-03-22
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多