gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:48:54 24 4
gpt4 key购买 nike

我正在尝试在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com