lundi 25 mai 2020

Fastest way to Select a random number from each row padded numpy array (excluding the pad) and number of non padded values, using numpy operations

I have a 2D numpy array, each row is padded with (with -1 for the example below).

For each row, I want to pick a random number, excluding the padding, and also get the number of non-padded values for each row, using only numpy operations.

Here is a minimal example. I picked -1 for the pad, but the pad can by any negative int.

import numpy as np
numList = [[0, 32, 84, 93, 1023, -1], [0, 23, 33, 45, -1, -1], [0, 10, 15, 21, 24, 25], [0, 23, -1, -1, -1, -1], [0 , 13, 33, 34, -1, -1]]
numArray = np.array(numList)
numArray

array([[   0,   32,   84,   93, 1023,   -1],
       [   0,   23,   33,   45,   -1,   -1],
       [   0,   10,   15,   21,   24,   25],
       [   0,   23,   -1,   -1,   -1,   -1],
       [   0,   13,   33,   34,   -1,   -1]])

For the lengths, the output should look something like this

LengthsResults
[5, 4, 6, 2, 4]. 

And here's an example output for picking a random non-pad number for each row.

randomNonPad
[84, 45, 0, 0, 34]

Edit:

I was looking at np.where, which lets you filter out parts of your numpy array on a conditional, and numpy random choice, which lets you pick a random number for an array. I'm not sure what to do with np.where though, it seems that you can change it to something, but I'm not sure what yet, or even if it's the right approach. For python, you could start with a list, and append it to any length, but for numpy you need to establish the array length ahead of time.




Aucun commentaire:

Enregistrer un commentaire