-1
我使用python-3.x和我試圖運行這個鱈魚,但我收到此錯誤:ValueError異常:設置一個數組元素與以序列中的python-3.x的
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.
謝謝問題是所有回合浮動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 ...)'列表中以獲取列表。 – hpaulj
你爲什麼不用'numpy'函數執行'schwefel'計算? ('np.sin','np.abs'等) – hpaulj
你的意思是在代碼中加入list:'code(Z = np.fromiter(list(map(schwefel,zip(X.flat,Y.flat) )))'@hpaulj if yes does not work !! – azeez