我正在关注" Python for Finance"预订并尝试优化功能,但在遵循代码时会出现错误。
TypeError:' numpy.ndarray'对象不可调用
此错误的其他论坛似乎不适用。
请让我知道我哪里出错了。
代码:
# 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)
错误:
追踪(最近一次呼叫最后一次):
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
答案 0 :(得分:-1)
您对scipy.optimize.brute
功能的调用是错误的。您必须使用fo
函数进行调用,rranges
定义如下:
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)