我试图使用 mplt3d 绘制 3d 图,但我收到以下代码的值错误:

数据挖掘 机器学习 数据科学模型 麻木的
2022-03-03 22:02:43
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

observations = 1000
xs = np.random.uniform(low=-10, high=10, size=(observations,1))
zs = np.random.uniform(-10, 10, (observations,1))
inputs = np.column_stack((xs,zs))
noise = np.random.uniform(-1, 1, (observations,1))
targets = 2*xs - 3*zs + 5 + noise

targets = targets.reshape(observations,)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(xs, zs, targets)

# Set labels
ax.set_xlabel('xs')
ax.set_ylabel('zs')
ax.set_zlabel('Targets')

ax.view_init(azim=100)

plt.show()

targets = targets.reshape(observations,1)

我收到上述代码块的错误:

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (1000,)  and requested shape (1000,1)

---> ax.plot(xs, zs, targets)
2个回答

重塑 xs 和 zs 变量以及 matplotlib 不再支持 (1,1,N) 格式的标量输入。重塑 xs 和 zs 输入变量以及目标变量。

在目标重塑变量下方添加这 2 行,它将起作用:

xs = xs.reshape(观察,)

zs = zs.reshape(观察,)

注意:一旦完成绘图,请记住将所有变量重新整形为原始标量数组形式,以便优化算法起作用。

这更像是一个编程问题而不是数据科学问题,因此更适合 stackoverflow stackexchange 页面。但是,您提供的代码对我来说非常好,它给了我以下情节:

在此处输入图像描述