Shaping numpy 2D array based on another 1D array

Submitted 3 years, 7 months ago
Ticket #123
Views 187
Language/Framework Python
Priority Medium
Status Closed

I have (n,m) array arr1, where n is almost 2.5 million and m =100. Another arr2 is (n, 1).
I want to modify arr1 such that:

arr1[i, j] = arr1[i, j], if j <= arr2[i]
           = nan       , otherwise

Sample arr1 and arr2 are as follows:

>>> import numpy as np
>>> arr1 = np.random.randint(500, size=(4,3))
>>> arr1
array([[147,  46, 168],
       [232, 446, 415],
       [ 21, 245, 214],
       [246, 194, 271]])
>>> arr2 = np.random.randint(4, size=(4,))
>>> arr2
array([3, 0, 2, 1])

Desired output in this case would be:

array([[147,  46, 168],
       [nan, nan, nan],
       [ 21, 245, nan],
       [246, nan, nan]])

Realistic sample to try for speed is

>>> import numpy as np
>>> arr1 = np.random.randint(500, size=(2500000, 100))
>>> arr2 = np.random.randint(101, size=(2500000,))

Submitted on Sep 10, 20
add a comment

1 Answer

Verified

def reshape_to_vect(ar):
    if len(ar.shape) == 1:
      return ar.reshape(ar.shape[0],1)
    return ar

Submitted 3 years, 6 months ago


Latest Blogs