2

I have a 2D numpy array of zeros, a list of numpy arrays (which can be of different lengths) and a list of indices. I want to add the contents of each of the arrays in the list from the start of the corresponding row indice in the 2D array.

Of course, I can just loop over the arrays. However, I need to perform this operations for many different samples. Therefore, I was wondering whether anybody is aware of a more efficient way to do this.

In [1]: A = np.zeros((5, 5))
   ...: arrays = [np.random.randint(1, 10, size=(1,5)) for i in range(3)]
   ...: indices = [1, 3, 4]
   ...: print(arrays)
Out[1]:
[array([3, 1, 3, 6]), array([4, 9]), array([5, 9, 6])]

Expected output:

array([[0., 0., 0., 0., 0.], 
       [3., 1., 3., 6., 0.], 
       [0., 0., 0., 0., 0.], 
       [4., 9., 0., 0., 0.], 
       [5., 9., 6., 0., 0.]] 

Any help would be much appreciated!

4
  • Typically how many samples do you have? And typically what's the length of such samples? Commented Aug 28, 2019 at 8:33
  • The shape of A is roughly (10, 200) and I have approximately 700,000 indices+arrays combinations. The shape of A is constant across all samples. In my current application I loop over all samples and put them in one big array of shape (700000, 10, 200). Commented Aug 28, 2019 at 13:05
  • You are starting with 2 lists. One of those lists contains arrays that vary in size; so it can't be turned into a 2d array. How else do you deal with lists other than to zip and iterate? Commented Aug 28, 2019 at 18:44
  • If you search on the question of padding a list of arrays, you'll find an advanced method by @Divakar that uses an hstack(array) and [len(i) for i in arrays] to map the arrays onto A in one step. Your indices complicates things, though it might be easier to use indices to remap A after padding. Commented Aug 28, 2019 at 18:50

1 Answer 1

1

Using zip:

for i, a in zip(indices, arrays):
    A[i, :len(a)] = a

Output:

array([[0., 0., 0., 0., 0.],
       [3., 1., 3., 6., 0.],
       [0., 0., 0., 0., 0.],
       [4., 9., 0., 0., 0.],
       [5., 9., 6., 0., 0.]])
Sign up to request clarification or add additional context in comments.

1 Comment

This approach what I meant with "looping over the arrays". Do you think this is the fastest approach possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.