scipy.optimize.curve_fit:不是一个适当的浮点数组错误 [英] scipy.optimize.curve_fit: not a proper array of floats error
问题描述
我试图使用optimization.curve_fit来找到两个数组之间的最小二乘解,但是我不断收到错误:函数调用的结果不是一个合适的float数组。我粘贴下面的代码。任何想法如何解决这一问题?
import numpy as np
导入scipy.optimize作为优化
pcone = np.array([[ - 0.01043151],
[-0.00135030],
[-0.02566969],
[-0.02822495],
[-0.05463625],
[-0.00969918],
[-0.01332421],
[-0.03364439],
[-0.04009642],
[-0.03556982]])
pctwo = np.array([[0.02550008],
[0.04422852],
[0.06685288],
[0.04751296],
[0.02439405],
[0.09654185 ],
[0.03161849],
[0.03834721],
[0.01653997],
[-0.00802414]])
def func(x,a ,b,c):
return a + b * x + c * x * x
print optimization.curve_fit(func,pcone,pctwo)
$ c $你的数组有形状(10,1)。如果你的数组有形状(10,1)。也就是说,它们是二维的,具有平凡的第二维度。在最简单的情况下, curve_fit
需要一维数组。在将它们传递给 curve_fit $ c之前,将 pcone
和 pctwo
例如,这个工程:
[数组([0.05720879,0.65281483,-2.67840575]),
array([[5.90887090e-04,4.15822858e-02,6.14439732e-01],
[4.15822858e-02,4.07354227e + 00,6.94784914e + 01],
[6.14439732e-01 ,6.94784914e + 01,1.29240335e + 03]]))
创建了 pcone
和 pctwo
,可能会更清晰地创建它们作为一维数组,而不是稍后将它们弄平。)
I'm trying to use optimization.curve_fit to find the least square solution between two arrays, but I keep getting error: Result from function call is not a proper array of floats. I pasted my code below. Any ideas how to fix this? Thank you!
import numpy as np
import scipy.optimize as optimization
pcone = np.array([[-0.01043151],
[-0.00135030],
[-0.02566969],
[-0.02822495],
[-0.05463625],
[-0.00969918],
[-0.01332421],
[-0.03364439],
[-0.04009642],
[-0.03556982]])
pctwo = np.array([[0.02550008],
[0.04422852],
[0.06685288],
[0.04751296],
[0.02439405],
[0.09654185],
[0.03161849],
[0.03834721],
[0.01653997],
[-0.00802414]])
def func(x, a, b, c):
return a + b*x + c*x*x
print optimization.curve_fit(func, pcone, pctwo)
解决方案 Your arrays have shape (10, 1). That is, they are two-dimensional, with a trivial second dimension. In the simplest case, curve_fit
expects one-dimensional arrays. Flatten pcone
and pctwo
into one-dimensional arrays before passing them to curve_fit
.
For example, this works:
In [8]: curve_fit(func, pcone.ravel(), pctwo.ravel())
Out[8]:
(array([ 0.05720879, 0.65281483, -2.67840575]),
array([[ 5.90887090e-04, 4.15822858e-02, 6.14439732e-01],
[ 4.15822858e-02, 4.07354227e+00, 6.94784914e+01],
[ 6.14439732e-01, 6.94784914e+01, 1.29240335e+03]]))
(You haven't shown how pcone
and pctwo
were created. It would probably be cleaner to create them as 1-D arrays in the first place, instead of flattening them later.)
这篇关于scipy.optimize.curve_fit:不是一个适当的浮点数组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!