如何在 Python 2 中正确编码矢量化函数条目
How to correctly code vectorized function entries in Python 2
所以我玩了一些我在网上找到的使用Python进行优化的代码3.修改后,它呈现了这样的情节
现在我正在使用 Python 2,*
没有被处理。我认为问题出在 Python 迭代,但在遵循 this post 中建议的括号技巧时我没有得到任何结果。这是完整的代码:
%matplotlib inline
import matplotlib.pyplot as plt
import pylab as pylab
import autograd.numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LogNorm
from matplotlib import animation
from IPython.display import HTML
from autograd import elementwise_grad, value_and_grad
from scipy.optimize import minimize
from collections import defaultdict
from itertools import izip_longest
from functools import partial
f = lambda x, y: 10*np.cos(1*x) * 15*np.sin(1/2*y) + 150
xmin, xmax, xstep = -4.5, 4.5, .2
ymin, ymax, ystep = -4.5, 4.5, .2
x, y = np.meshgrid(np.arange(xmin, xmax + xstep, xstep), np.arange(ymin, ymax + ystep, ystep))
z = f(x, y)
minima = np.array([np.pi, np.pi])
minima_ = minima.reshape(-1, 1)
fig = plt.figure(figsize=(8, 5))
ax = plt.axes(projection='3d', elev=50, azim=-50)
ax.plot_surface(x, y, z, norm=LogNorm(), rstride=1, cstride=1,
edgecolor='none', alpha=.8, cmap=plt.cm.jet)
ax.plot(*minima_, f(*minima_), 'o', markersize=4, color='w')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$z$')
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
plt.show()
和错误信息:
File "<ipython-input-5-3b03e44c1cac>", line 31
ax.plot(*minima_, f(*minima_), 'o', markersize=4, color='w')
SyntaxError: only named arguments may follow *expression
考虑按索引分配两个 minima_ 值。以下符合 python 2 和 3:
a,b = minima_[0], minima_[1] # TO ADD
ax.plot(a,b, f(a,b), 'o', markersize=4, color='w') # TO REPLACE
所以我玩了一些我在网上找到的使用Python进行优化的代码3.修改后,它呈现了这样的情节
现在我正在使用 Python 2,*
没有被处理。我认为问题出在 Python 迭代,但在遵循 this post 中建议的括号技巧时我没有得到任何结果。这是完整的代码:
%matplotlib inline
import matplotlib.pyplot as plt
import pylab as pylab
import autograd.numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LogNorm
from matplotlib import animation
from IPython.display import HTML
from autograd import elementwise_grad, value_and_grad
from scipy.optimize import minimize
from collections import defaultdict
from itertools import izip_longest
from functools import partial
f = lambda x, y: 10*np.cos(1*x) * 15*np.sin(1/2*y) + 150
xmin, xmax, xstep = -4.5, 4.5, .2
ymin, ymax, ystep = -4.5, 4.5, .2
x, y = np.meshgrid(np.arange(xmin, xmax + xstep, xstep), np.arange(ymin, ymax + ystep, ystep))
z = f(x, y)
minima = np.array([np.pi, np.pi])
minima_ = minima.reshape(-1, 1)
fig = plt.figure(figsize=(8, 5))
ax = plt.axes(projection='3d', elev=50, azim=-50)
ax.plot_surface(x, y, z, norm=LogNorm(), rstride=1, cstride=1,
edgecolor='none', alpha=.8, cmap=plt.cm.jet)
ax.plot(*minima_, f(*minima_), 'o', markersize=4, color='w')
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$z$')
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
plt.show()
和错误信息:
File "<ipython-input-5-3b03e44c1cac>", line 31
ax.plot(*minima_, f(*minima_), 'o', markersize=4, color='w')
SyntaxError: only named arguments may follow *expression
考虑按索引分配两个 minima_ 值。以下符合 python 2 和 3:
a,b = minima_[0], minima_[1] # TO ADD
ax.plot(a,b, f(a,b), 'o', markersize=4, color='w') # TO REPLACE