数组拆分为子数组

数组拆分为子数组

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

问题描述

假设我有两个 NumPy 数组

Suppose I have two NumPy arrays

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

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

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

我想要的输出是

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

假设 y 从零开始并按升序排序,那么最有效的方法是什么?

Assuming that y starts with zero and is sorted in ascending order, what is the most efficient way to do this?

注意:本题是本题的排序版:拆分 NumPy 数组根据另一个数组的值(未排序,而是分组)分成子数组

Note: This question is the sorted version of this question:Split a NumPy array into subarrays according to the values (not sorted, but grouped) of another array

推荐答案

如果 y 是分组的(不必排序),可以使用 diff 得到分割点:

If y is grouped (doesn't have to be sorted), you can use diff to get the split points:

indices = np.flatnonzero(np.diff(y)) + 1

您可以将这些直接传递给 np.split:

You can pass those directly to np.split:

z = np.split(x, indices, axis=0)

如果你也想知道标签:

labels = y[np.r_[0, indices]]

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

08-21 11:53