【发布时间】:2017-10-04 23:42:56
【问题描述】:
我正在使用 python-3.x,我正在尝试运行此 cod,但收到此错误:
Z = np.fromiter(map(schwefel, zip(X.flat,Y.flat)), dtype=np.float16,
count=X.shape[0]*X.shape[1]).reshape(X.shape)
ValueError: setting an array element with a sequence.
我感谢问题是我无法解决的 float dtype,请任何建议或建议将不胜感激。
import random
from math import sin, cos, pi, exp, e, sqrt
from operator import mul
from functools import reduce
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
try:
import numpy as np
except:
exit()
def schwefel(individual):
N = len(individual)
return 418.9828872724339*N-sum(x*sin(sqrt(abs(x))) for x in individual),
fig = plt.figure()
# ax = Axes3D(fig, azim = -29, elev = 50)
ax = Axes3D(fig)
X = np.arange(-500, 500, 10)
Y = np.arange(-500, 500, 10)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(schwefel, zip(X.flat,Y.flat)), dtype=np.float16, count=X.shape[0]*X.shape[1]).reshape(X.shape)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0.2)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
【问题讨论】:
-
在 Py3 中,
map是一种生成器,需要将其包裹在list(map...)中以获取列表。 -
您为什么不用
numpy函数执行schwefel计算? (np.sin、np.abs等) -
你的意思是在代码中添加列表:
code(Z = np.fromiter (list(map(schwefel, zip(X.flat,Y.flat))))@hpaulj 如果是的话它不起作用!! -
不要让我们有悬念!提供有关错误和中间值的更多信息。您不会指望我将代码复制粘贴到我的计算机上并自己调试,对吗?
-
我所做的是
code Z = np.fromiter (list(map(schwefel, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1])).reshape(X.shape),但我收到了这个错误code TypeError: list() takes at most 1 argument (3 given)!!! @hpaulj
标签: python-3.x numpy valueerror