numpy的索引如何在这种情况下工作

时间:2013-07-27 23:54:44

标签: python numpy matplotlib

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)

1 个答案:

答案 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,)

当您使用dataidx==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}}。