< Browse > Home / Informatique / Blog article: Numpy error – ValueError: setting an array element with sequence

| Mobile | RSS

Numpy error - ValueError: setting an array element with sequence

June 4th, 2009 | 6 Comments | Posted in Informatique

You have struggled during hours and hours to understand why you got this damned error while compiling a complex array structure? I did. In my case I was trying to create an array of the following shape :

[  [ [0,0,1,0] , 0.5] , [ [0,1,1,0] , 0.3] , [ [1,0,1,0] , 0.2] ]

With the basic syntax, it was impossible. I first didn't notice the problem because I wasn't using the 'array' constructor, but when I tried to append some value to my initial vector, I got the error : ValueError: setting an array element with sequence.

Even the simple construction : array([( [0,0,1,0],0.5)]) would give the error.

the problem is that numpy is made to calculate vectors and arrays. It doesn't like the fact you give it table with different sizes. Ok, then you can try to change the second value 0.5 to [0.5,0,0,0]. It would work.... but it's not really clean. Your code will look messy.

Instead, here is the solution I found after 4 hours looking everywhere on the web... finding nothing. This is my own solution, so if helped you, I would be very glad :-D

In order to help numpy to accept your vector... tell it it's not a vector :

array([( [0,0,1,0],0.5)],object)

By doing this you change de dtype of your array, and it can accept even personnal classes.

Does that work for you?

Be Sociable, Share!
Leave a Reply 16062 views, 33 so far today |
Tags: ,
Follow Discussion

6 Responses to “Numpy error - ValueError: setting an array element with sequence”

  1. Nin Says:

    Absolutely fantastic, i was looking for that for ages!

  2. Caro Says:

    :) Is the first time I reply to somebody. THHHHHAAANNNNNKKKKKKSSSSSSSSSS!!!

  3. szajtan Says:

    yep, I had the same problem.
    I was constructing list of arrays and then converting it to an array of arrays using
    array_of_arrays=array(list_of_arrays)
    it worked fine on python2.7 and numpy-1.5.1-1.fc15.x86_64
    but failed on
    python2.5 and numpy-1.2.0-3.fc10.x86_64.
    So I changed the code as:
    array_of_arrays=array(list_of_arrays,list)
    now it works fine :)
    btw:
    array_of_arrays=array(list_of_arrays,object)
    also works fine
     
    Thanks a lot !
     

  4. hicham Says:

    Genial !!!!!!! je cherchais depuis longtemps à comment faire accepter à numpy des calculs sur des tableau de dimensions differentes Bravo !!

  5. mikidkolan Says:

    yes! It helps. It looked like some problem with list-array in my case, but could not understand why my remedy did not work. Thanks!

  6. Andie Says:

    I have a similar problem but mine occurs because my vectors are generated by abstract objects, they are not even arrays, but they do have elements generated by __getitem__... any suggestions? I'm trying to make a list of tokeninzed documents from gensim fit into sklearn.svm.svr.

    If the objects are generated one at a time, i guess time complexity and more importantly (the very reason I'm doing this) space complexity will not suffer too much. I can create a zero-array of the correct length and then add real numbers where needed.. but still.. abstraction is more fun and it's what Python is supposed to be good at!

Leave a Reply

CommentLuv badge

Additional comments powered by BackType


CarroCarro - Mars 2011Street ArtFirenze - Ponte VecchioBergerieBergerie2Citadelle de CalviParc enfantManègeÔ la vache!