【发布时间】:2023-03-30 07:40:01
【问题描述】:
我是 python 和机器学习的新手,作为我大学课程的一部分,我们正在使用 numpy、matplotlib 和 sci-kit learn。好的,我有一个问题。下面的代码工作得很好,我的问题是我真的不明白发生了什么。所以对于这个:
%matplotlib inline
X=iris.data
Y=iris.target
#first two features are sepal length and sepal width
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
我试图检查文档,但对我来说并没有任何意义。 在这里,我想知道 plt.scatter() 中的参数是什么意思。不是很明白,c=Y是什么意思,cmap是什么为什么会有两个X坐标?
至于接下来的代码:
%matplotlib inline
#here's also how to plot in 3d:
from mpl_toolkits.mplot3d import Axes3D #
#create a new figure
fig = plt.figure(figsize=(5,5))
#this creates a 1x1 grid (just one figure), and now we are plotting
#subfigure 1 (this is what 111 means)
ax = fig.add_subplot(111, projection='3d')
#plot first three features in a 3d Plot. Using : means that we take all
#elements in the correspond array dimension
ax.scatter(X[:, 0], X[:, 1], X[:, 2],c=Y)
这里我想知道的是:
fig.add_subplot(111, 投影 = '3d')。这第三个 1 对我来说真的没有意义。我理解 1x1 网格,但我不理解“现在我们正在绘制子图 1”。
还有:
ax.scatter() 参数在这里也没有任何意义。为什么格式与上面的格式不同?为什么有三个 X 而没有 cmap?我真的不明白。为什么他们不使用 plt.scatter()?
【问题讨论】:
-
X是一个变量,而不是参数的名称。它是一个至少包含三列的数组。第一个用作绘图的 x 变量。第二列用作绘图的 y 变量。第三列用于缩放标记的大小。标记的颜色是根据分配给Y变量的数据设置的。
标签: python numpy matplotlib machine-learning scikit-learn