scipy curve_fit不像数学模块

时间:2018-01-12 12:13:33

标签: python python-3.x numpy scipy curve-fitting

在尝试使用scipy.optimize curve_fit创建示例时,我发现scipy似乎与Python的math模块不兼容。虽然函数f1工作正常,但f2会抛出错误消息。

from scipy.optimize import curve_fit
from math import sin, pi, log, exp, floor, fabs, pow

x_axis = np.asarray([pi * i / 6 for i in range(-6, 7)])  
y_axis = np.asarray([sin(i) for i in x_axis])

def f1(x, m, n):
    return m * x + n

coeff1, mat = curve_fit(f1, x_axis, y_axis)    
print(coeff1)

def f2(x, m, n):
    return m * sin(x) + n 

coeff2, mat = curve_fit(f2, x_axis, y_axis)  
print(coeff2)

完整的追溯是

Traceback (most recent call last):
  File "/Documents/Programming/Eclipse/PythonDevFiles/so_test.py", line 49, in <module>
    coeff2, mat = curve_fit(f2, x_axis, y_axis)
  File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 742, in curve_fit
    res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 377, in leastsq
    shape, dtype = _check_func('leastsq', 'func', func, x0, args, n)
  File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 26, in _check_func
    res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
  File "/usr/local/lib/python3.5/dist-packages/scipy/optimize/minpack.py", line 454, in func_wrapped
    return func(xdata, *params) - ydata
  File "/Documents/Programming/Eclipse/PythonDevFiles/so_test.py", line 47, in f2
    return m * sin(x) + n 
TypeError: only length-1 arrays can be converted to Python scalars

出现错误消息,其中包含列表和numpy数组作为输入。它会影响我测试的所有math函数(参见导入函数),并且必须与数学模块如何操作输入数据有关。这对于pow()函数最为明显 - 如果我不从math导入此函数,curve_fit可以与pow()一起正常工作。

显而易见的问题 - 为什么会发生这种情况以及math功能如何与curve_fit一起使用?

P.S。:请不要讨论,不应该用线性拟合拟合样本数据。这只是为了说明问题而选择的。

1 个答案:

答案 0 :(得分:3)

小心numpy-arrays,处理数组的操作和处理标量的操作!

Scipy optimize假设输入(初始点)是1d阵列,并且在其他情况下经常出现问题(例如,列表变成了数组,如果你假设在列表上工作,那么事情会受到严重破坏;那些问题在StackOverflow上很常见,并且调试并不容易看到;代码交互有帮助!)。

import numpy as np
import math

x = np.ones(1)

np.sin(x)
> array([0.84147098])

math.sin(x)
> 0.8414709848078965                     # this only works as numpy has dedicated support
                                         # as indicated by the error-msg below!
x = np.ones(2)

np.sin(x)
> array([0.84147098, 0.84147098])

math.sin(x)
> TypeError: only size-1 arrays can be converted to Python scalars

说实话:这是对numpy的基本理解的一部分,在使用scipy的某些敏感功能时应该理解。