ValueError: setting an array element with a sequence

后端 未结 8 1276
萌比男神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.

提交回复
热议问题