2

I want to to make an array of arrays, but when I use np.append I get the list if their elements:

import numpy as np
im_data = np.array(['image0','image1','image2','image3','image4','image5','image6','image7','image8','image9','image10','image11','image12','image13','image14'])
batches = [[1,2,3,4],[7,8,9,10],[3,4,5,6]]

image_batches = []
for batch in batches:
    image_batches = np.append(image_batches,[im_data[batch]])

This is what I get:

In: image_batches

Out: array(['image1', 'image2', 'image3', 'image4', 'image7', 'image8', 'image9', 'image10', 'image3', 'image4', 'image5', 'image6'], dtype='<U32')

and this is what I need:

array([['image1', 'image2', 'image3', 'image4'], ['image7', 'image8', 'image9', 'image10'], ['image3', 'image4', 'image5', 'image6']], dtype='<U7')

I achieved this by using

image_batches = im_data[batches[0]]
for batch in batches[1:]:
    image_batches = np.vstack([image_batches, im_data[batch]])

but maybe there is more elegant way to do it?

2
  • 1
    Can't you make one list with all those im_data[batch]. Repeated np.append or vstack in a loop is inefficient. Do one vstack on a list. You already know how to make an array from a list of items (or list of lists). Commented Jul 27, 2020 at 23:11
  • 1
    Does im_data[batches] do anything useful? If you can index with a sublist, I think you can do the same with the whole list. Commented Jul 28, 2020 at 0:21

2 Answers 2

2

Like mentioned by @hpaulj in comments, you can directly use advances indexing:

im_data[np.array(batches)]

output:

[['image1' 'image2' 'image3' 'image4']
 ['image7' 'image8' 'image9' 'image10']
 ['image3' 'image4' 'image5' 'image6']]
1

You can use list comprehension and then convert it into numpy.array:

import numpy as np
im_data = np.array(['image0','image1','image2','image3','image4','image5','image6','image7','image8','image9','image10','image11','image12','image13','image14'])
batches = [[1,2,3,4],[7,8,9,10],[3,4,5,6]]

image_batches = np.array([im_data[batch_idx] for batch_idx in batches])
image_batches

Output:

array([['image1', 'image2', 'image3', 'image4'],
       ['image7', 'image8', 'image9', 'image10'],
       ['image3', 'image4', 'image5', 'image6']], dtype='<U7')

Cheers.

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.