Chris Chris - 6 months ago 62
Python Question

numpy.ndarray object not callable in brute

I am following the code in a "Python for Finance" book and trying to optimize a function but am getting an error when following the code.


TypeError: 'numpy.ndarray' object is not callable


The other forums on this error don't seem to be applicable.

Please let me know where I'm going wrong.

Code:

# import libraries
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import scipy.optimize as spo
from mpl_toolkits.mplot3d import Axes3D

# define function
def fm(x, y):
return ( np.sin(x) + 0.05*x**2 + np.sin(y) + 0.05*y**2 )

# construct range vectors
x = np.linspace(-10, 10, 50)
y = np.linspace(-10, 10, 50)
X, Y = np.meshgrid(x, y)
Z = fm(X, Y)

# plot surface
fig = plt.figure(figsize=(9, 6))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, rstride=2, cstride=2,cmap=mpl.cm.coolwarm,linewidth=0.5, antialiased=True)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x, y)')
fig.colorbar(surf, shrink=0.5, aspect=5)

# define function for optimisation
def fo(x, y):
z = np.sin(x) + 0.05*x**2 + np.sin(y) + 0.05*y**2
if output == True:
print(x, y, z)
return z

# print each iteration?
output = True
rranges = (slice(-10, 10.1, 5), slice(-10, 10.1, 5))
params = (x,y)
# optimise
spo.brute(fo(x,y), ((-10, 10.1, 5),(-10, 10.1, 5)), finish=None)


Error:

Traceback (most recent call last):

File "<ipython-input-1-76b5e42b4ae6>", line 1, in <module>
runfile('C:/Users/Chris/Dropbox/Chris Personal/Learning Python Resources/Python for Finance/Chapter 9/trial_convex_optimisation.py', wdir='C:/Users/Chris/Dropbox/Chris Personal/Learning Python Resources/Python for Finance/Chapter 9')

File "C:\Users\Chris\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)

File "C:\Users\Chris\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "C:/Users/Chris/Dropbox/Chris Personal/Learning Python Resources/Python for Finance/Chapter 9/trial_convex_optimisation.py", line 46, in <module>
spo.brute(fo(x,y), [(-10, 10.1, 5),(-10, 10.1, 5)], finish=None)

File "C:\Users\Chris\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 2604, in brute
Jout = vecfunc(*grid)

File "C:\Users\Chris\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1811, in __call__
return self._vectorize_call(func=func, args=vargs)

File "C:\Users\Chris\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1874, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)

File "C:\Users\Chris\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1836, in _get_ufunc_and_otypes
outputs = func(*inputs)

File "C:\Users\Chris\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 2598, in _scalarfunc
return func(params, *args)

TypeError: 'numpy.ndarray' object is not callable

Answer

Your calling to scipy.optimize.brute function is wrong. You have to call with fo function and rranges defined as follow:

def fo(xy):
    x, y = xy
    z = np.sin(x) + 0.05*x**2 + np.sin(y) + 0.05*y**2
    if output == True:
        print(x, y, z)
    return z

rranges = (slice(-10, 10.1, 5), slice(-10, 10.1, 5))

spo.brute(fo, rranges, finish=None)
Comments