简体   繁体   English

根据标签对NumPy数组进行分区

[英]Partitioning a NumPy array based on labels

I have data sets A and B. A is a matrix which shape is [169594, 22] B is a matrix which shape is [169594,1] B consists of (0, 1, 2, 3, 4, 5) which is the label of each row of A. 我有数据集A和B。A是一个形状为[169594,22]的矩阵B是一个形状为[169594,1]的矩阵B由(0,1,2,3,4,5)组成A的每一行的标签。

So, I would like to separate Data of A into each labels. 因此,我想将A的数据分成每个标签。

So my code is as below. 所以我的代码如下。

在此处输入图片说明

I am beginner in Python, so this code is not work. 我是Python的初学者,因此此代码不起作用。

If this code is work well, the expected result is as below. 如果此代码运行良好,则预期结果如下。

aa[xxx, 22]
bb[xxx, 22]
cc[xxx, 22]
dd[xxx, 22]
ee[xxx, 22]
ff[xxx, 22] 

How can I solve this problem? 我怎么解决这个问题? Thank you! 谢谢!

You could reshape B into a 1D array and then use boolean indexing on A . 您可以将B整形为一维数组,然后在A上使用boolean indexing

B = B.reshape(-1, )
aa = A[B == 0, :]
bb = A[B == 1, :]
cc = A[B == 2, :]
dd = A[B == 3, :]
ee = A[B == 4, :]
ff = A[B == 5, :]

Or, even better, keep segregated items in a list. 或者,甚至更好的是,将隔离的项目保留在列表中。

l = []
for i in range(6):
    l.append(A[B == i, :]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM