【发布时间】:2016-12-27 01:46:20
【问题描述】:
我正在尝试使用 SciPy curve_fit 将我的数据拟合到用户定义的函数中,该函数在拟合到具有固定功率 (func1) 的函数时有效。但是当函数包含一个幂作为参数以适应 (func2) 时,curve_fit 不起作用。
如果我使用关键字 p0 为参数提供初始猜测,Curve_fit 仍然不起作用。我不能使用 bounds 关键字,因为我没有它的 SciPy 版本。
这个脚本说明了这一点:
import scipy
from scipy.optimize import curve_fit
import sys
print 'scipy version: ', scipy.__version__
print 'np.version: ', np.__version__
print sys.version_info
def func1(x,a):
return (x-a)**3.0
def func2(x,a,b):
return (x-a)**b
x_train = np.linspace(0, 12, 50)
y = func2(x_train, 0.5, 3.0)
y_train = y + np.random.normal(size=len(x_train))
print 'dtype of x_train: ', x_train.dtype
print 'dtype of y_train: ', y_train.dtype
popt1, pcov1 = curve_fit( func1, x_train, y_train, p0=[0.6] )
popt2, pcov2 = curve_fit( func2, x_train, y_train, p0=[0.6, 4.0] )
print 'Function 1: ', popt1, pcov1
print 'Function 2: ', popt2, pcov2
输出如下:
scipy version: 0.14.0
np.version: 1.8.2
sys.version_info(major=2, minor=7, micro=6, releaselevel='final', serial=0)
dtype of x_train: float64
dtype of y_train: float64
stack_overflow.py:14: RuntimeWarning: invalid value encountered in power
return (x-a)**b
Function 1: [ 0.50138759] [[ 3.90044196e-07]]
Function 2: [ nan nan] [[ inf inf]
[ inf inf]]
【问题讨论】:
-
我猜
func2在指数为小数时会因x-a的负值而跳闸。有没有办法改变你的模型(也许抵消你的数据),让x-a更有可能是积极的?
标签: python-2.7 scipy curve-fitting