从2d numpy数组的每一行中选择随机的非零元素

从2d numpy数组的每一行中选择随机的非零元素

问题描述:

我有一个二维数组

a = array([[5, 0, 1, 0],
           [0, 1, 3, 5],
           [2, 3, 0, 0],
           [4, 0, 2, 4],
           [3, 2, 0, 3]])

和一维数组

b = array([1, 2, 1, 2, 2])

哪个( b )告诉我们要从数组 a 的每一行中选择多少个非零元素.

which (b) tells how many non-zero elements we want to choose from each row of the array a.

例如, b [0] = 1 告诉我们,我们必须从 a [0] b [1]中选择1个非零元素] = 2 告诉我们,我们必须从 a [1] 中选择2个非零元素,依此类推.

For example, b[0] = 1 tells us that we have to choose 1 non-zero element from a[0], b[1] = 2 tells us that we have to choose 2 non-zero elements from a[1], and so on.

对于一维数组,可以使用 np.random.choice 完成,但是我找不到如何对二维数组进行操作,因此必须使用for 循环会减慢计算速度.

For a 1d array, it can be done using np.random.choice, but I can't find how to do it for a 2d array, so I have to use a for loop which slows the computation.

我希望将结果作为2d数组作为

I want the result as 2d array as

array([[5, 0, 0, 0],
       [0, 1, 0, 5],
       [2, 0, 0, 0],
       [0, 0, 2, 4],
       [3, 2, 0, 0]])

在这里,第1行有1个元素,第2行有2个元素,第3行有1个元素,依此类推,如数组b所示.

Here, we have 1 element in row 1, 2 elements in row 2, 1 element in row 3 and so on as given in array b.

它看起来像是竞争编程问题.

It looks like a Competitive Programming problem.

我认为您不能使用numpy.random.choice获得结果(我可能是错误的).

I don't think that you can achieve the results using numpy.random.choice (I may be wrong).

无论如何,请这样想.要从大小为 n 的一维数组中选择 x 个非零元素,在最坏的情况下,它的复杂度为O(n).而且,对于2D数组,如果您采用相同的幼稚方法,则将为O(n ^ 2).

Anyways, think of it like this. To select x number of non-zero elements from a 1D array of size n, it will be of O(n) complexity in the worst case. And, for a 2D array it will be O(n^2) if you follow the same naive approach.

这篇帖子与您的帖子几乎相似问题,但numpy.nonzero也是O(n ^ 2)函数.

this post is almost similar to your question, but numpy.nonzero also is an O(n^2) function.