0 votes
in Python by
Explain Error in python ValueError: setting an array element with a sequence

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?

1 Answer

0 votes
by
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.

Related questions

+1 vote
asked Jan 10, 2021 in Python by rajeshsharma
+1 vote
asked May 17, 2020 in Python by sharadyadav1986
...