ValueError错误:在设置数组元素与序列 [英] ValueError: setting an array element with a sequence

查看:1258
本文介绍了ValueError错误:在设置数组元素与序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这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)
    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.

不过,这code工作:

However, this code works:

import numpy as p
a=range(24)
b = p.reshape(a,(6,4))
c=p.array(b,dtype=p.float64)[:,2]

我重新安排code有点把它变成一个有说服力的帖子,但它应该或多或少有同样的结果。谁能告诉我该怎么做来解决上述破code中的问题,以便它停止抛出一个错误信息?

I re-arranged the code a bit to put it into a cogent posting, but it should more or less have the same result. Can anyone show me what to do to fix the problem in the broken code above so that it stops throwing an error message?

编辑:
我做了一个打印命令来获得矩阵的内容,这是它打印出来:

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

UnFilteredDuringExSummaryOfMeansArray是:

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]]

看起来像一个5行13列的矩阵对我来说,虽然当不同的数据通过脚本运行的行数是可变的。随着我在此我将这个相同的数据。

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: 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?

第二个编辑:

仅供参考,如果我更换这个问题行code的:

FYI, if I replace this problem line of code:

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

与此相反:

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

然后脚本的该部分工作正常,没有出现任何的错误,但随后这条线的code进一步向下行:

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())

抛出这个错误:

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

所以你可以看到,我需要以指定的数据类型能在matplotlib使用ylim,但尚未指定的数据类型是扔发起这个职位的错误消息。

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.

推荐答案

从code你向我们展示了,我们可以告诉的唯一的事情是,你要创建从一个没有形状像一个列表的数组多维数组。例如:

From the code you showed us, the only thing we can tell is that you 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]])

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

将产生此错误消息,因为输入列表的形状不是(广义)盒子,它可以变成一个多维数组。所以大概 UnFilteredDuringExSummaryOfMeansArray 包含不同长度的序列。

修改:另一个可能的原因此错误消息试图在类型的数组使用一个字符串作为一个元素浮动

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)

这是什么你根据你的编辑尝试。如果你真的想有一个包含两个字符串和一个浮点数数组numpy的,你可以使用DTYPE 对象,这使数组来保存任意Python对象:

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)

不知道你的code应做到,我不能,如果这是你想要的判断。

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

这篇关于ValueError错误:在设置数组元素与序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆