JavaEar 专注于收集分享传播有价值的技术资料

TypeError: 'numpy.int64' object is not callable — What does that mean in laymans terms?

I'm trying to perform a non-linear regression after this example with my own sigmoidal model:

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

The example explained on the website works perfectly, but not so my code:

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()

However, when I execute the script, it throws the following error:

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

There clearly is a TypeError somewhere, but I don't understand where or what exactly the problem is. I have tried searching Google for this error, but also I don't quite understand the explanations. Ideas, anyone?


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个回答

    最佳答案
  1. When you write k(d-e) it thinks you are trying to call a function k with d-e as an argument. You can't use juxtaposition to indicate multiplication in Python. You have to explicitly write the multiplication: k*(d-e).