python - 类型错误 : 'numpy.int64' object is not callable — What does that mean in laymans terms?

标签 python numpy scipy nonlinear-optimization

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

$$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,但我不明白问题出在哪里或究竟是什么。我已经尝试在谷歌上搜索这个错误,但我也不太明白其中的解释。有想法吗?


数据.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

最佳答案

当您编写 k(d-e) 时,它认为您正在尝试以 d-e 作为参数调用函数 k。在 Python 中不能使用并列表示乘法。您必须显式地编写乘法:k*(d-e)

关于python - 类型错误 : 'numpy.int64' object is not callable — What does that mean in laymans terms?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866355/

相关文章:

Python OpenCV : inverting colors in a numpy image array

python - 扩展(添加行或列) scipy.sparse 矩阵

python - R 相当于 scipy.integrate.simps()?

Python Social Auth - 如何知道社交媒体连接是否成功?

python - 函数 numpy.fft.fftfreq 的含义

python - 从 Flask 返回重定向到 S3 不会下载文件

python - 使用 hdf5 的矩阵乘法

numpy - SciPy PearsonR ValueError : The truth value of an array with more than one element is ambiguous. 使用 a.any() 或 a.all()

python - 循环 reddit 机器人每 10 分钟检查一次答案

python - 时间序列数据的分层交叉验证