【发布时间】:2016-08-08 16:52:20
【问题描述】:
让我们从头开始。这就是我获得x 和y 值的方式:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d') # get current axis
w0 = np.arange(30, 80, 1) # x - values
w1 = np.arange(-3, 3, 0.1) # y - values
X, Y = np.meshgrid(w0, w1) # no idea why to do that
因为我不知道如何避免循环,所以我用这种方式计算 Z 值:
sizeWo = len(w0)
sizeW1 = len(w1)
Z = np.zeros((sizeWo, sizeW1))
for i in xrange(0,sizeWo):
for j in xrange(0,sizeW1):
Z[i,j] = errorLose(w0[i], w1[j])
surf = ax.plot_surface(X, Y, Z) # that lines generates the error (ValueError: shape mismatch: objects cannot be broadcast to a single shape)
即使这段代码也会产生同样的错误:
surf = ax.plot_surface(w0, w1, Z) shape mismatch: objects cannot be broadcast to a single shape
plt.show()
这里出了什么问题以及如何使它起作用?
errorLose 函数接受两个值并使用后面的数据框data 计算错误:
def errorLose(w0,w1):
return np.sum((data.Height - (w0 + w1 *data.Weight))**2)
这就是您可以获取相同数据的方式。这是csv file的链接:
data = pd.read_csv('weights_heights.csv', index_col='Index')
【问题讨论】:
标签: python python-2.7 numpy plot