Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

python - ValueError:使用序列设置数组元素(ValueError: setting an array element with a sequence)

This Python code:

(此Python代码:)

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 "mypathmypythonscript.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:

(UnFilteredDuringExSummaryOfMeansArray为:)

[['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.

(对我来说,这看起来像是5行乘13列的矩阵,但是当通过脚本运行不同的数据时,行数是可变的。)

With this same data that I am adding in this.

(使用我要添加的相同数据。)

EDIT 2 : However, the script is throwing an error.

(编辑2 :但是,脚本抛出错误。)

So I do not think that your idea explains the problem that is happening here.

(因此,我认为您的想法不能解释此处正在发生的问题。)

Thank you, though.

(谢谢你)

Any other ideas?

(还有其他想法吗?)


EDIT 3:

(编辑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 "mypathmypythonscript.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.

(因此,您可以看到我需要指定数据类型以便能够在matplotlib中使用ylim,但是指定数据类型会引发引发此帖子的错误消息。)

  ask by MedicalMath translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(因此,可能UnFilteredDuringExSummaryOfMeansArray包含不同长度的序列。)

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

(编辑 :此错误消息的另一个可能原因是尝试将字符串用作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数组,则可以使用dtype object ,它使该数组可以容纳任意Python对象:)

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

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

(不知道您的代码将完成什么,我无法判断这是否是您想要的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...