ValueError: setting an array element with a sequence

后端 未结 8 1170
萌比男神i
萌比男神i 2020-11-22 05:46

This Python code:

import numpy as p

def firstfunction():
    UnFilteredDuringExSummaryOfMeansArray = []
    MeanOutputHeader=[\'TestID\',\'         


        
相关标签:
8条回答
  • 2020-11-22 06:17

    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 numpy
    
    numpy.array([1,2,3])               #good
    
    numpy.array([1, (2,3)])            #Fail, can't convert a tuple into a numpy 
                                       #array element
    
    
    numpy.mean([5,(6+7)])              #good
    
    numpy.mean([5,tuple(range(2))])    #Fail, can't convert a tuple into a numpy 
                                       #array element
    
    
    def foo():
        return 3
    numpy.array([2, foo()])            #good
    
    
    def 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])         #good
    
    
    
    x = 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.

    0 讨论(0)
  • 2020-11-22 06:17

    When the shape is not regular or the elements have different data types, the dtype argument passed to np.array only can be object.

    import numpy as np
    
    # arr1 = np.array([[10, 20.], [30], [40]], dtype=np.float32)  # error
    arr2 = np.array([[10, 20.], [30], [40]])  # OK, and the dtype is object
    arr3 = np.array([[10, 20.], 'hello'])     # OK, and the dtype is also object
    

    ``

    0 讨论(0)
  • 2020-11-22 06:26

    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 tf
    
    input_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.

    0 讨论(0)
  • 2020-11-22 06:34

    In my case, the problem was with a scatterplot of a dataframe X[]:

    ax.scatter(X[:,0],X[:,1],c=colors,    
           cmap=CMAP, edgecolor='k', s=40)  #c=y[:,0],
    
    #ValueError: setting an array element with a sequence.
    #Fix with .toarray():
    colors = 'br'
    y = label_binarize(y, classes=['Irrelevant','Relevant'])
    ax.scatter(X[:,0].toarray(),X[:,1].toarray(),c=colors,   
           cmap=CMAP, edgecolor='k', s=40)
    
    0 讨论(0)
  • 2020-11-22 06:37

    In my case, the problem was another. I was trying convert lists of lists of int to array. The problem was that there was one list with a different length than others. If you want to prove it, you must do:

    print([i for i,x in enumerate(list) if len(x) != 560])
    

    In my case, the length reference was 560.

    0 讨论(0)
  • 2020-11-22 06:39

    for those who are having trouble with similar problems in Numpy, a very simple solution would be:

    defining dtype=object when defining an array for assigning values to it. for instance:

    out = np.empty_like(lil_img, dtype=object)
    
    0 讨论(0)
提交回复
热议问题