根据另一个数组的值(未排序,但已分组)将 NumPy 数组拆分为子数组 [英] Split a NumPy array into subarrays according to the values (not sorted, but grouped) of another array

查看:46
本文介绍了根据另一个数组的值(未排序,但已分组)将 NumPy 数组拆分为子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个 NumPy 数组

Suppose I have two NumPy arrays

x = [[1, 2, 8],
     [2, 9, 1],
     [3, 8, 9],
     [4, 3, 5],
     [5, 2, 3],
     [6, 4, 7],
     [7, 2, 3],
     [8, 2, 2],
     [9, 5, 3],
     [10, 2, 3],
     [11, 2, 4]]
y = [0, 0, 1, 0, 1, 1, 2, 2, 2, 0, 0] 

注意:(x 中的值没有以任何方式排序.我选择这个例子是为了更好地说明这个例子)(这只是 xy 的两个例子.xy 的值可以是任意多个不同的数字和 y 可以有任意不同的数字,但 x 中的值总是与 y 中的值一样多)

Note: (values in x are not sorted in any way. I chose this example to better illustrate the example) (These are just two examples of x and y. values of x and y can be arbitrarily many different numbers and y can have arbitrarily different numbers, but there are always as many values in x as there are in y)

我想根据y中的值有效地将数组x分割成子数组.

I want to efficiently split the array x into sub-arrays according to the values in y.

我想要的输出是

z_0 = [[1, 2, 8],
       [2, 9, 1],
       [4, 3, 5],
       [10, 2, 3],
       [11, 2, 4]]
z_1 = [[3, 8, 9],
       [5, 2, 3],
       [6, 4, 7],]
z_2 = [[7, 2, 3],
       [8, 2, 2],
       [9, 5, 3]]

假设 y 从零开始并且没有排序而是分组,那么最有效的方法是什么?

Assuming that y starts with zero and is not sorted but grouped, what is the most efficient way to do this?

注:本题是本题的未排序版本:将 NumPy 数组拆分为子数组根据另一个数组的值(按升序排序)

Note: This question is the unsorted version of this question: Split a NumPy array into subarrays according to the values (sorted in ascending order) of another array

推荐答案

解决这个问题的一种方法是为每个 y 值建立一个过滤器索引列表,然后简单地选择 <代码>x.例如:

One way to solve this is to build up a list of filter indexes for each y value and then simply select those elements of x. For example:

z_0 = x[[i for i, v in enumerate(y) if v == 0]]
z_1 = x[[i for i, v in enumerate(y) if v == 1]]
z_2 = x[[i for i, v in enumerate(y) if v == 2]]

输出

array([[ 1,  2,  8],
       [ 2,  9,  1],
       [ 4,  3,  5],
       [10,  2,  3],
       [11,  2,  4]])
array([[3, 8, 9],
       [5, 2, 3],
       [6, 4, 7]])
array([[7, 2, 3],
       [8, 2, 2],
       [9, 5, 3]])

如果您想更通用并支持 y 中的不同数字集,您可以使用推导式生成数组列表,例如

If you want to be more generic and support different sets of numbers in y, you could use a comprehension to produce a list of arrays e.g.

z = [x[[i for i, v in enumerate(y) if v == m]] for m in set(y)]

输出:

[array([[ 1,  2,  8],
       [ 2,  9,  1],
       [ 4,  3,  5],
       [10,  2,  3],
       [11,  2,  4]]),
 array([[3, 8, 9],
       [5, 2, 3],
       [6, 4, 7]]),
 array([[7, 2, 3],
       [8, 2, 2],
       [9, 5, 3]])]

如果 y 也是一个 np.array 并且长度与 x 相同,您可以将其简化为使用布尔索引:

If y is also an np.array and the same length as x you can simplify this to use boolean indexing:

z = [x[y==m] for m in set(y)]

输出和上面一样.

这篇关于根据另一个数组的值(未排序,但已分组)将 NumPy 数组拆分为子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆