scipy的多变量非线性curve_fit [英] multivariable non-linear curve_fit with scipy

查看:361
本文介绍了scipy的多变量非线性curve_fit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过多个变量使用scipy.optimize curve_fit.它可以与我创建的测试代码一起正常工作,但是当我尝试在实际数据上实现该代码时,我不断收到以下错误

I have been trying to use scipy.optimize curve_fit using multiple variables. It works fine with the test code I created but when I try to implement this on my actual data I keep getting the following error

TypeError:只有长度为-1的数组可以转换为python标量

TypeError: only arrays length -1 can be converted to python scalars

在我的测试代码和实际代码中,数组的形状以及它们的元素的数据类型是完全相同的,所以对于为什么会出现此错误,我感到困惑.

The shape of the arrays and the data types of their elements in my test code and actual code are exactly the same so I am confused as to why I get this error.

测试代码:

    import numpy as np 
    import scipy 
    from 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)

实际代码:

    f=open("Exp_Fresnal.csv", 'rb')
    reader=csv.reader(f)
    for 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 is the 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: only length-1 arrays can be converted to Python scalars

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: only length-1 arrays can be converted to Python scalars

推荐答案

我发现了问题所在.由于某种原因,问题是在拟合函数func中使用了math.expcmath.exp.我使用np.exp()代替了这些功能.我不完全确定为什么会这样.

I figured out the issue. The problem for some reason was the use of math.exp and cmath.exp in the fitting function func. In place of these functions I used np.exp(). I am not completely sure the reason why though.

这篇关于scipy的多变量非线性curve_fit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆