在此示例之后,我尝试使用我自己的 sigmoidal 模型执行非线性回归:
$$f(d) = \frac{1}{1 + \exp (-k (de))}$$
网站上解释的示例完美运行,但我的代码并非如此:
import pylab
import numpy
from scipy import optimize
def f(d, k, e):
return 1 / (1 + numpy.exp(-k(d-e)))
def resid(p, y, d):
k, e = p
return y - f(d, k, e)
# load the data
d, r, n = numpy.loadtxt('data.txt', unpack=True)
y = numpy.concatenate([d, r])
k0, e0 = 1, 10
[k, e], flag = optimize.leastsq(resid, [k0, e0], args=(d, r))
print flag, k, e
# plot the data
pylab.plot(d, r, 'ro')
pylab.show()
但是,当我执行脚本时,它会引发以下错误:
Traceback (most recent call last):
File "./logisticfit.py", line 22, in <module>
[k, e], flag = optimize.leastsq(resid, [k0, e0], args=(d, r))
File "/Library/Python/2.7/site-packages/scipy-0.12.0.dev_d631749_20121222-py2.7-macosx-10.8-intel.egg/scipy/optimize/minpack.py", line 348, in leastsq
m = _check_func('leastsq', 'func', func, x0, args, n)[0]
File "/Library/Python/2.7/site-packages/scipy-0.12.0.dev_d631749_20121222-py2.7-macosx-10.8-intel.egg/scipy/optimize/minpack.py", line 14, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "./logisticfit.py", line 12, in resid
return y - f(d, k, e)
File "./logisticfit.py", line 8, in f
return 1 / (1 + numpy.exp(-k(d-e)))
TypeError: 'numpy.int64' object is not callable
某处显然存在 TypeError,但我不明白问题出在哪里或到底是什么。我曾尝试在 Google 上搜索此错误,但我也不太明白其中的解释。想法,有人吗?
数据.txt
0.0 0.0 6
4.5 0.0 3
6.1 0.333333333333 3
7.7 0.0 3
8.5 0.2 10
9.0 0.6 5
9.3 0.333333333333 3
9.5 0.333333333333 6
10.0 0.333333333333 6
10.5 0.8 5
10.9 0.5 2
11.0 1.0 5
11.5 1.0 5
12.0 1.0 4
12.5 1.0 8
13.0 1.0 1