2

I would like to understand why I can't create an array of variable size by one way, but I can by another..

Here you can see the 2 different code, the 1st works, but not the second (img0, img1, ... are images with different size).

1st one :

img = np.array([img0, img1, img2, img3, img4, img5, img6, img7, img8, img9])

2nd one :

tableau = np.ones((10,))
for i in range (0, 10):
    tableau[i] = img[i]        

In the second case I have this error message :

ValueError: setting an array element with a sequence.

But nothing in the 1st code and everything is ok.. I don't understand why and in my case I really need to work with the second method.

Thanks !

4
  • Use a python list and append the items, then do the conversion to an array at the end. What do you get from print(img.shape)? Commented Aug 7, 2019 at 15:37
  • tableau.append() youre treating the array like a dict instead of an array Commented Aug 7, 2019 at 15:43
  • @ErikK no they aren't, that is the syntax for accessing list items by index. What they are trying to do is initialise an array with a set shape and then replace elements later. This approach makes sense with numpy in many cases, since arrays do not work well with changing size through, e.g. append, but they have initialised the array of scalar values and then try to put multi-dimensional arrays at the index; you can't do this. Commented Aug 7, 2019 at 15:45
  • Why do you need to make an array to contain these images? Commented Aug 7, 2019 at 16:42

1 Answer 1

1

A numpy array has a fixed, 'rectangular' shape, and dtype. It that isn't clear you need to reread the basic documentation.

np.array(...) tries to make a multidimensional array from the inputs. The classic case is

np.array([ [1,2], [3,4] ])

which makes a 2d array from the list of lists.

But if the inputs differ in size it can't do that. The fall back is to make an object dtype array, and fill it with pointers to those inputs. That's what's happening in your first case. Look at its dtype and shape.

In the second case you create an array with 10 floats. You get the error when you try to put a multidimensional image object into one of those float slots.

In [173]: np.array([ [1,2],[3,4] ])                                                                          
Out[173]: 
array([[1, 2],
       [3, 4]])
In [174]: np.array([ [1,2],[3,4,5] ])                                                                        
Out[174]: array([list([1, 2]), list([3, 4, 5])], dtype=object)

You could start with an object dtype array, and fill it with all kinds of objects:

In [175]: x = np.empty(3, object)                                                                            
In [176]: x                                                                                                  
Out[176]: array([None, None, None], dtype=object)
In [177]: x[0] = [1,2,3]                                                                                     
In [178]: x[1] = {1:2, 3:4}                                                                                  
In [180]: x[2] = np.arange(3)                                                                                
In [181]: x                                                                                                  
Out[181]: array([list([1, 2, 3]), {1: 2, 3: 4}, array([0, 1, 2])], dtype=object)

But beware that such an array is more like a list than the regular number n-d array.

In [182]: x.tolist()                                                                                         
Out[182]: [[1, 2, 3], {1: 2, 3: 4}, array([0, 1, 2])]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.