[英]scipy curve_fit doesn't like math module
在尝试使用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
一起使用?
PS:请不要讨论,不应该用线性拟合拟合样本数据。 这只是为了说明问题而选择的。
小心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的某些敏感函数时应该被理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.