TypeError:'numpy.int64'对象不可调用 - 这在外行术语中意味着什么?

时间:2013-04-07 18:47:37

标签: python numpy scipy nonlinear-optimization

我正在尝试使用我自己的S形模型在this example之后执行非线性回归:

$$ f(d)= \ frac {1} {1 + \ exp(-k(d-e))} $$

网站上解释的示例效果很好,但不是我的代码:

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,但我不明白问题的确切位置。我曾尝试在谷歌搜索此错误,但我也不太明白这些解释。想法,任何人?


data.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

1 个答案:

答案 0 :(得分:5)

当你写k(d-e)时,它认为你试图用k作为参数调用函数d-e。你不能使用并置来表示Python中的乘法。您必须明确地编写乘法:k*(d-e)