Python函数vs部分-Scipy不';我不喜欢partials
所以我在读Python函数vs部分-Scipy不';我不喜欢partials,python,function,scipy,partial,Python,Function,Scipy,Partial,所以我在读曲线拟合曲线拟合将要拟合的函数作为其第一个参数。我稍微修改了示例代码,以便将其作为第一个参数: In: import numpy as np from scipy.optimize import curve_fit def func( t, x , a, b, c): # I added a dummy variable t so that I can apply partials later return a*np.exp(-b*x) + c + t func = par
曲线拟合
<代码>曲线拟合将要拟合的函数作为其第一个参数。我稍微修改了示例代码,以便将其作为第一个参数:
In:
import numpy as np
from scipy.optimize import curve_fit
def func( t, x , a, b, c): # I added a dummy variable t so that I can apply partials later
return a*np.exp(-b*x) + c + t
func = partial( func, 0 ) # use of partial to remove the dummy variable
x = np.linspace(0,4,50)
y = func(x, 2.5, 1.3, 0.5)
yn = y + 0.2*np.random.normal(size=len(x))
popt, pcov = curve_fit(func, x, yn) # curve_fit gets a partial instead of a Python function
Out:
TypeError: <functools.partial object at 0x104551c58> is not a Python function
In:
将numpy作为np导入
从scipy.optimize导入曲线\u拟合
def func(t,x,a,b,c):#我添加了一个伪变量t,以便以后可以应用partials
返回a*np.exp(-b*x)+c+t
func=partial(func,0)#使用partial删除虚拟变量
x=np.linspace(0,4,50)
y=func(x,2.5,1.3,0.5)
yn=y+0.2*np.随机.正常(尺寸=len(x))
popt,pcov=curve_-fit(func,x,yn)#curve_-fit获取一个分部函数而不是Python函数
输出:
TypeError:不是Python函数
啊,真令人失望。我想下次我会使用lambda
。
不管怎样,这里有什么问题?函数可以做什么,分部函数不能做什么
曲线拟合
使用标准库中的参数来确定函数的参数。不幸的是,getargspec
无法处理部分:
In [31]: from inspect import getargspec
In [32]: from functools import partial
In [33]: def func(t, x, a, b, c):
....: return a*np.exp(-b*x) + c + t
....:
In [34]: pfunc = partial(func, 0)
getargspec(func)
工作正常
In [35]: getargspec(func)
Out[35]: ArgSpec(args=['t', 'x', 'a', 'b', 'c'], varargs=None, keywords=None, defaults=None)
但是getargspec
不处理部分函数:
In [36]: getargspec(pfunc)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-36-3fb5eaea7c94> in <module>()
----> 1 getargspec(pfunc)
/Users/warren/anaconda/python.app/Contents/lib/python2.7/inspect.pyc in getargspec(func)
814 func = func.im_func
815 if not isfunction(func):
--> 816 raise TypeError('{!r} is not a Python function'.format(func))
817 args, varargs, varkw = getargs(func.func_code)
818 return ArgSpec(args, varargs, varkw, func.func_defaults)
TypeError: <functools.partial object at 0x107ec6d08> is not a Python function
[36]中的:getargspec(pfunc)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 getargspec(pfunc)
/getargspec(func)中的Users/warren/anaconda/python.app/Contents/lib/python2.7/inspect.pyc
814 func=func.im_func
815如果不是isfunction(函数):
-->816 raise TypeError(“{!r}不是Python函数”。format(func))
817 args,varargs,varkw=getargs(函数函数代码)
818返回ArgSpec(args、varargs、varkw、func.func_默认值)
TypeError:不是Python函数
更新:
getargspec在python 3.5和3.6中处理分部函数,因此使用带有曲线拟合的分部函数是可行的。我没有安装python3的旧版本,因此无法检查它们
python 3.6、scipy 0.18.1、numpy 1.12.0
import numpy as np
from functools import partial
from scipy.optimize import curve_fit
def func(t, x, a, b, c):
return a*np.exp(-b*x) + c + t
x = np.linspace(0,4,50)
pfunc = partial(func, 0.0)
y = pfunc(x, 2.5, 1.3, 0.5)
popt, pcov = curve_fit(pfunc, x, y)
print(popt, y[0:2])
pfunc = partial(func, 100.0)
y = pfunc(x, 2.5, 1.3, 0.5)
popt, pcov = curve_fit(pfunc, x, y)
print(popt, y[0:2])
[ 2.5 1.3 0.5] [ 3. 2.7482863]
[ 2.5 1.3 0.5] [ 103. 102.7482863]
进一步追踪代码,问题是
是否为instance(func,types.FunctionType)
type(pfunc)
是functools。partial
curvefit
只使用getargspec
确定len(args)
。在这种情况下,5表示func
,5-1表示pfunc
,即len(getargspec(pfunc.func)[0])-len(pfunc.args)
。使用此计算的自定义curvefit
可能会起作用。