plot numpy的逻辑索引如何从下面的代码片段中的“data”变量获取数据点?我知道第一个参数是x坐标,第二个参数是y坐标。我不确定它如何映射到变量的数据点。
data = vstack((rand(150,2) + array([.5,.5]),rand(150,2)))
# assign each sample to a cluster
idx,_ = vq(data,centroids)
# some plotting using numpy's logical indexing
plot(data[idx==0,0],data[idx==0,1],'ob',
data[idx==1,0],data[idx==1,1],'or')
plot(centroids[:,0],centroids[:,1],'sg',markersize=8)
答案 0 :(得分:4)
一切形状都是:
In [89]: data.shape
Out[89]: (300, 2) # data has 300 rows and 2 columns
In [93]: idx.shape
Out[93]: (300,) # idx is a 1D-array with 300 elements
idx == 0
是一个布尔数组,其形状与idx
相同。只要True
中的元素等于idx
,<{1}}就是0
:
In [97]: (idx==0).shape
Out[97]: (300,)
当您使用data
对idx==0
进行索引时,您会获得data
的所有行,其中idx==0
为True:
In [98]: data[idx==0].shape
Out[98]: (178, 2)
使用元组data[idx==0, 0]
进行索引时,data
的第一个轴使用布尔数组idx==0
编制索引,data
的第二个轴编入索引0
:
In [99]: data[idx==0, 0].shape
Out[99]: (178,)
data
的第一个轴对应于行,第二个轴对应于列。所以你只得到data[idx==0]
的第一列。由于data
的第一列是x
- 值,因此x
- data
中的idx==0
值{{1}}。