我的数据有d
数组:
--> d
array([[ 60.41202301, 58.39997156, 55.3667636 , ..., -84.87512796,
-86.79190447, -86.19353546],
[ 60.10975935, 58.05402795, 55.3898762 , ..., -86.60428129,
-88.12205283, -89.45247056],
[ 60.14155715, 58.09627498, 55.7903388 , ..., -89.18612111,
-90.50667213, -92.67073841],
...,
[ 85.91138845, 84.50241834, 82.63664476, ..., -71.04692203,
-87.06297169, -100.36565732],
[ 82.74086966, 81.80343637, 79.96785709, ..., -64.08451752,
-79.18141593, -99.36881913],
[ 79.15278282, 78.42730549, 77.1980837 , ..., -58.38254858,
-75.93821858, -93.55287173]])
和另外2个数组,每个像素的半径和角度值:
--> radius
array([[ 42.42640687, 41.72529209, 41.03656906, ..., 37.80211634,
38.41874542, 39.05124838],
[ 41.72529209, 41.01219331, 40.31128874, ..., 37.01351105,
37.64306045, 38.28837944],
[ 41.03656906, 40.31128874, 39.59797975, ..., 36.23534186,
36.87817783, 37.53664876],
...,
[ 41.72529209, 41.01219331, 40.31128874, ..., 37.01351105,
37.64306045, 38.28837944],
[ 42.42640687, 41.72529209, 41.03656906, ..., 37.80211634,
38.41874542, 39.05124838],
[ 43.13930922, 42.44997055, 41.77319715, ..., 38.60051813,
39.20459157, 39.8246155 ]])
--> angle
array([[ 3.92699082, 3.94393835, 3.96145992, ..., 5.3664717 ,
5.38712992, 5.40712726],
[ 3.91004329, 3.92699082, 3.94453288, ..., 5.38291054,
5.40372591, 5.42384811],
[ 3.89252172, 3.90944876, 3.92699082, ..., 5.40006024,
5.42101525, 5.44124371],
...,
[ 2.37314202, 2.35619449, 2.33865243, ..., 0.90027477,
0.8794594 , 0.8593372 ],
[ 2.35619449, 2.33924696, 2.32172539, ..., 0.9167136 ,
0.89605538, 0.87605805],
[ 2.33980252, 2.32287349, 2.30539079, ..., 0.93247652,
0.91199029, 0.89213384]])
我还有p0
,我想要优化的对象:
--> p0
[1.0, 500.0, 0.5, 0.5, 5.2, 0.4]
我的职能定义:
def rotation(B,radiusref,angleref):
return B[0] + (B[1] * radiusref * np.cos(angleref-B[2]) * np.sin(B[3]) * np.cos(B[3])**B[5] ) / (radiusref**2 * ( np.sin(angleref-B[2])**2 + np.cos(B[3])**2*np.cos(angleref-B[2])**2 ) + B[4]**2*np.cos(B[3])**2 )**B[5]/2.0
def errfunc(p0,d,radius,angle):
return rotation(p0,radius,angle) - d
然后,我尝试运行optimize.leastsq
以找到最佳解决方案,并收到以下错误消息:
--> fit = optimize.leastsq(errfunc,p0,args=(d,radius,angle))
ValueError: object too deep for desired array
Traceback (innermost last):
File "<console>", line 1, in <module>
File "/home/luisgdh/Ureka/python/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 379, in leastsq
gtol, maxfev, epsfcn, factor, diag)
error: Result from function call is not a proper array of floats.
为什么会这样?
如果我尝试运行errfunc
,我会得到此
--> errfunc(p0,d,radius,angle)
array([[-311.96251984, -307.50362989, -301.89956899, ..., 121.84425081,
128.7017239 , 132.9161849 ],
[-312.24524019, -307.79621255, -302.61362546, ..., 127.21717127,
133.70343473, 139.86845861],
[-312.81581149, -308.43470253, -303.66703887, ..., 133.56598817,
139.87929082, 146.89601654],
...,
[-156.59031196, -150.97315566, -144.78021889, ..., 305.55786014,
324.82275173, 341.21407515],
[-149.69283006, -144.54525672, -138.38451089, ..., 297.67836311,
316.06244785, 339.38095877],
[-142.47209078, -137.53819725, -131.98946431, ..., 291.05301549,
311.92874028, 332.71166956]])
答案 0 :(得分:0)
我解决了自己的问题,如果有人再次发现这个问题,那么,minimalsq并不能很好地处理2d数据,所以我通过以下方式转换了1d数据中的2d数据:
d1radius = radius.ravel()
d1angle=angle.ravel()
d1data=d.ravel()
并重新定义了该功能:
def errfunc(p0,d1data,d1radius,d1angle):
return rotation(p0,d1radius,d1angle) - d1data
最后:
fit = optimize.leastsq(errfunc,p0,args=(d1data,d1radius,d1angle))