I am trying to call scipy curve_fit(), with the proper:
- model function
- xdata (float numpy 1D Array)
- ydata (float numpy 1D Array)
- p (float numpy 1D Array, initial values)
However I am getting the error: ValueError: Object too deep for desired Array
Result from function Call is not a proper array of floats.
the model function I am computing is :
The mathematical expression that optimizes model_f, from which we are trying to find the optimal alpha, gamma.
function model_f computes the mathematical expression appended in the picture.
JavaScript
x
16
16
1
with open("Data_case_3.csv",'r') as i: #open a file in directory of this script for reading
2
rawdata = list(csv.reader(i,delimiter=",")) #make a list of data in file
3
4
exampledata = np.array(rawdata[1:],dtype=np.float) #convert to data array
5
xdata = exampledata[:,0]
6
ydata = exampledata[:,1]
7
8
m = 0.5
9
omega0 = 34.15
10
k = np.square(omega0)*m
11
12
13
def model_f(x,a,g):
14
zetaeq = (a*np.sqrt(np.pi)*(x**(g-1))*omega0*math.gamma(g/2))/(2*np.pi*k*math.gamma((3+g)/2))
15
return zetaeq
16
#——————————————————————————
JavaScript
1
6
1
funcdata = model_f(xdata,0.3,0.1)
2
plt.plot(xdata,funcdata,label="Model")
3
plt.legend()
4
5
popt, pcov = curve_fit(model_f, xdata, ydata, p0=[0.3,0.1])
6
And I am attaching the data types of the variables mentioned:
Variable types and shapes of the script Can you help me understand what I am doing wrong?
Advertisement
Answer
Compare these 2 calls to curve_fit
:
JavaScript
1
7
1
In [217]: xdata, ydata = np.ones(5), np.ones(5)
2
In [218]: curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
3
Out[218]:
4
(array([0.74436049, 0.02752099]),
5
array([[2.46401533e-16, 9.03501810e-18],
6
[9.03501810e-18, 3.31294823e-19]]))
7
and
JavaScript
1
13
13
1
In [219]: xdata, ydata = np.ones((5,1)), np.ones((5,1))
2
In [220]: curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
3
ValueError: object too deep for desired array
4
5
Traceback (most recent call last):
6
Input In [220] in <module>
7
curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
8
File /usr/local/lib/python3.8/dist-packages/scipy/optimize/_minpack_py.py:789 in curve_fit
9
res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
10
File /usr/local/lib/python3.8/dist-packages/scipy/optimize/_minpack_py.py:423 in leastsq
11
retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
12
error: Result from function call is not a proper array of floats.
13
Which is closer to your experience?