ValueError: setting an array element with a sequence ValueError: setting an array element with a sequence python python

ValueError: setting an array element with a sequence


From the code you showed us, the only thing we can tell is that you are trying to create an array from a list that isn't shaped like a multi-dimensional array. For example

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

or

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

will yield this error message, because the shape of the input list isn't a (generalised) "box" that can be turned into a multidimensional array. So probably UnFilteredDuringExSummaryOfMeansArray contains sequences of different lengths.

Edit: Another possible cause for this error message is trying to use a string as an element in an array of type float:

numpy.array([1.2, "abc"], dtype=float)

That is what you are trying according to your edit. If you really want to have a NumPy array containing both strings and floats, you could use the dtype object, which enables the array to hold arbitrary Python objects:

numpy.array([1.2, "abc"], dtype=object)

Without knowing what your code shall accomplish, I can't judge if this is what you want.


The Python ValueError:

ValueError: setting an array element with a sequence.

Means exactly what it says, you're trying to cram a sequence of numbers into a single number slot. It can be thrown under various circumstances.

1. When you pass a python tuple or list to be interpreted as a numpy array element:

import numpynumpy.array([1,2,3])               #goodnumpy.array([1, (2,3)])            #Fail, can't convert a tuple into a numpy                                    #array elementnumpy.mean([5,(6+7)])              #goodnumpy.mean([5,tuple(range(2))])    #Fail, can't convert a tuple into a numpy                                    #array elementdef foo():    return 3numpy.array([2, foo()])            #gooddef foo():    return [3,4]numpy.array([2, foo()])            #Fail, can't convert a list into a numpy                                    #array element

2. By trying to cram a numpy array length > 1 into a numpy array element:

x = np.array([1,2,3])x[0] = np.array([4])         #goodx = np.array([1,2,3])x[0] = np.array([4,5])       #Fail, can't convert the numpy array to fit                              #into a numpy array element

A numpy array is being created, and numpy doesn't know how to cram multivalued tuples or arrays into single element slots. It expects whatever you give it to evaluate to a single number, if it doesn't, Numpy responds that it doesn't know how to set an array element with a sequence.


In my case , I got this Error in Tensorflow , Reason was i was trying to feed a array with different length or sequences :

example :

import tensorflow as tfinput_x = tf.placeholder(tf.int32,[None,None])word_embedding = tf.get_variable('embeddin',shape=[len(vocab_),110],dtype=tf.float32,initializer=tf.random_uniform_initializer(-0.01,0.01))embedding_look=tf.nn.embedding_lookup(word_embedding,input_x)with tf.Session() as tt:    tt.run(tf.global_variables_initializer())    a,b=tt.run([word_embedding,embedding_look],feed_dict={input_x:example_array})    print(b)

And if my array is :

example_array = [[1,2,3],[1,2]]

Then i will get error :

ValueError: setting an array element with a sequence.

but if i do padding then :

example_array = [[1,2,3],[1,2,0]]

Now it's working.