Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: setting an array element with a sequence

This Python code:

import numpy as p  def firstfunction():     UnFilteredDuringExSummaryOfMeansArray = []     MeanOutputHeader=['TestID','ConditionName','FilterType','RRMean','HRMean',                       'dZdtMaxVoltageMean','BZMean','ZXMean','LVETMean','Z0Mean',                       'StrokeVolumeMean','CardiacOutputMean','VelocityIndexMean']     dataMatrix = BeatByBeatMatrixOfMatrices[column]     roughTrimmedMatrix = p.array(dataMatrix[1:,1:17])       trimmedMatrix = p.array(roughTrimmedMatrix,dtype=p.float64)  #ERROR THROWN HERE       myMeans = p.mean(trimmedMatrix,axis=0,dtype=p.float64)     conditionMeansArray = [TestID,testCondition,'UnfilteredBefore',myMeans[3], myMeans[4],                             myMeans[6], myMeans[9], myMeans[10], myMeans[11], myMeans[12],                            myMeans[13], myMeans[14], myMeans[15]]     UnFilteredDuringExSummaryOfMeansArray.append(conditionMeansArray)     secondfunction(UnFilteredDuringExSummaryOfMeansArray)     return  def secondfunction(UnFilteredDuringExSummaryOfMeansArray):     RRDuringArray = p.array(UnFilteredDuringExSummaryOfMeansArray,dtype=p.float64)[1:,3]     return  firstfunction() 

Throws this error message:

File "mypath\mypythonscript.py", line 3484, in secondfunction RRDuringArray = p.array(UnFilteredDuringExSummaryOfMeansArray,dtype=p.float64)[1:,3] ValueError: setting an array element with a sequence. 

Can anyone show me what to do to fix the problem in the broken code above so that it stops throwing an error message?


EDIT: I did a print command to get the contents of the matrix, and this is what it printed out:

UnFilteredDuringExSummaryOfMeansArray is:

[['TestID', 'ConditionName', 'FilterType', 'RRMean', 'HRMean', 'dZdtMaxVoltageMean', 'BZMean', 'ZXMean', 'LVETMean', 'Z0Mean', 'StrokeVolumeMean', 'CardiacOutputMean', 'VelocityIndexMean'], [u'HF101710', 'PreEx10SecondsBEFORE', 'UnfilteredBefore', 0.90670000000000006, 66.257731979420001, 1.8305673000000002, 0.11750000000000001, 0.15120546389880002, 0.26870546389879996, 27.628261216480002, 86.944190346160013, 5.767261352345999, 0.066259118585869997], [u'HF101710', '25W10SecondsBEFORE', 'UnfilteredBefore', 0.68478571428571422, 87.727887206978565, 2.2965444125714285, 0.099642857142857144, 0.14952476549885715, 0.24916762264164286, 27.010483303721429, 103.5237336525, 9.0682762747642869, 0.085022572648242867], [u'HF101710', '50W10SecondsBEFORE', 'UnfilteredBefore', 0.54188235294117659, 110.74841107829413, 2.6719262705882354, 0.077705882352917643, 0.15051306356552943, 0.2282189459185294, 26.768787504858825, 111.22827075238826, 12.329456404418824, 0.099814258468417641], [u'HF101710', '75W10SecondsBEFORE', 'UnfilteredBefore', 0.4561904761904762, 131.52996981880955, 3.1818159523809522, 0.074714285714290493, 0.13459344175047619, 0.20930772746485715, 26.391156337028569, 123.27387909873812, 16.214243779323812, 0.1205685359981619]] 

Looks like a 5 row by 13 column matrix to me, though the number of rows is variable when different data are run through the script. With this same data that I am adding in this.

EDIT 2: However, the script is throwing an error. So I do not think that your idea explains the problem that is happening here. Thank you, though. Any other ideas?


EDIT 3:

FYI, if I replace this problem line of code:

    RRDuringArray = p.array(UnFilteredDuringExSummaryOfMeansArray,dtype=p.float64)[1:,3] 

with this instead:

    RRDuringArray = p.array(UnFilteredDuringExSummaryOfMeansArray)[1:,3] 

Then that section of the script works fine without throwing an error, but then this line of code further down the line:

p.ylim(.5*RRDuringArray.min(),1.5*RRDuringArray.max()) 

Throws this error:

File "mypath\mypythonscript.py", line 3631, in CreateSummaryGraphics   p.ylim(.5*RRDuringArray.min(),1.5*RRDuringArray.max()) TypeError: cannot perform reduce with flexible type 

So you can see that I need to specify the data type in order to be able to use ylim in matplotlib, but yet specifying the data type is throwing the error message that initiated this post.

like image 693
MedicalMath Avatar asked Jan 12 '11 21:01

MedicalMath


People also ask

What does ValueError setting an array element with a sequence?

In python, we often encounter the error as ValueError: setting an array element with a sequence is when we are working with the numpy library. This error usually occurs when the Numpy array is not in sequence.

How do you change an element in an array Python?

Changing Multiple Array Elements In Python, it is also possible to change multiple elements in an array at once. To do this, you will need to make use of the slice operator and assign the sliced values a new array to replace them.

How do you convert a list to an array in Python?

To convert a list to array in Python, use the np. array() method. The np. array() is a numpy library function that takes a list as an argument and returns an array containing all the list elements.


2 Answers

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]])          # wrong! 

or

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

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.

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)   # wrong! 

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 is supposed to accomplish, I can't tell if this is what you want.

like image 173
Sven Marnach Avatar answered Sep 19 '22 13:09

Sven Marnach


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.

like image 42
Eric Leschinski Avatar answered Sep 17 '22 13:09

Eric Leschinski