Skip to content
Advertisement

Curve_Fit returrns error “Result from function Call is not a proper array of floats”

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.

with open("Data_case_3.csv",'r') as i:           #open a file in directory of this script for reading 
rawdata = list(csv.reader(i,delimiter=","))   #make a list of data in file

exampledata = np.array(rawdata[1:],dtype=np.float)    #convert to data array
xdata = exampledata[:,0]
ydata = exampledata[:,1]

m = 0.5 
omega0 = 34.15
k = np.square(omega0)*m


def model_f(x,a,g):
    zetaeq = (a*np.sqrt(np.pi)*(x**(g-1))*omega0*math.gamma(g/2))/(2*np.pi*k*math.gamma((3+g)/2))
    return zetaeq

#——————————————————————————

funcdata = model_f(xdata,0.3,0.1)                 
plt.plot(xdata,funcdata,label="Model")
plt.legend()

popt, pcov = curve_fit(model_f, xdata, ydata, p0=[0.3,0.1])

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:

In [217]: xdata, ydata = np.ones(5), np.ones(5)
In [218]: curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
Out[218]: 
(array([0.74436049, 0.02752099]),
 array([[2.46401533e-16, 9.03501810e-18],
        [9.03501810e-18, 3.31294823e-19]]))

and

In [219]: xdata, ydata = np.ones((5,1)), np.ones((5,1))
In [220]: curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
ValueError: object too deep for desired array

Traceback (most recent call last):
  Input In [220] in <module>
    curve_fit(model_f, xdata, ydata, p0=[0.3, 0.1])
  File /usr/local/lib/python3.8/dist-packages/scipy/optimize/_minpack_py.py:789 in curve_fit
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File /usr/local/lib/python3.8/dist-packages/scipy/optimize/_minpack_py.py:423 in leastsq
    retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
error: Result from function call is not a proper array of floats.

Which is closer to your experience?

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement