math - multivariable non-linear curve_fit with scipy -


i have been trying use scipy.optimize curve_fit using multiple variables. works fine test code created when try implement on actual data keep getting following error

typeerror: arrays length -1 can converted python scalars

the shape of arrays , data types of elements in test code , actual code same confused why error.

test code:

    import numpy np      import scipy      scipy.optimize import curve_fit      def func(x,a,b,c):           return a+b*x[0]**2+c*x[1]     x_0=np.array([1,2,3,4])     x_1=np.array([5,6,7,8])     x=scipy.array([x_0,x_1])     y=func(x,3.1,2.2,2.1)     popt, pcov=curve_fit(func,x,y) 

actual code:

    f=open("exp_fresnal.csv", 'rb')     reader=csv.reader(f)     row in reader:          qz.append(row[0])          ref.append(row[1])          ref_f.append(row[2])     qz_arr,ref_farr=scipy.array((qz)),scipy.array((ref_f))     x=scipy.array([qz_arr,ref_farr]      def func(x,d,sig_int,sig_cp):          return x[1]*(x[0]*d*(math.exp((-sig_int**2)*(x[0]**2)/2)/(1-cmath.exp(complex(0,1)*x[0]*d)*math.exp((-sig_cp**2)*(x[0]**2)/2))))**2      y=scipy.array((ref))     popt, pcov=curve_fit(func,x,y) 

edit here full error message

traceback (most recent call last): file "dcm_03.py", line 46, in <module> popt, pcov=curve_fit(func,x,y) file "//anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 651, in curve_fit res = leastsq(func, p0, args=args, full_output=1, **kwargs) file "//anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 377, in leastsq shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) file "//anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 26, in _check_func res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) file "//anaconda/lib/python2.7/site-packages/scipy/optimize/minpack.py", line 453, in _general_function return function(xdata, *params) - ydata file "dcm_03.py", line 40, in func return (0.062/(2*x))**4*(x*d*(math.exp((-sig_int**2)*(x**2)/2)/(1-cmath.exp(complex(0,1)*x*d)*math.exp((-sig_cp**2)*(x**2)/2))))**2 typeerror: length-1 arrays can converted python scalars

i figured out issue. problem reason use of math.exp , cmath.exp in fitting function func. in place of these functions used np.exp(). not sure reason why though.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -