以下代码:切片二维numpy的阵列
import numpy as p
myarr=[[0,1],[0,6],[0,1],[0,6],[0,1],[0,6],[0,1],[0,6],[0,1],[0,6],[0,1],[0,6],[0,1],[0,6],[0,1],[0,6],[0,1],[0,6]]
copy=p.array(myarr)
p.mean(copy)[:,1]
正在生成以下错误消息:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
p.mean(copy)[:,1]
IndexError: 0-d arrays can only use a single() or a list of newaxes (and a single ...) as an index
我抬头的语法at this link和我似乎可以用正确的语法切片。然而,当我键入
copy[:,1]
到Python外壳,它给了我下面的输出,这显然是错误的,并可能是什么引发错误:
array([1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6])
谁能告诉我怎么修复我的代码,以便我可以提取第二列,然后按照上面原始代码中的意图取第二列的含义?
编辑:
谢谢您的解决方案。但是,我的发帖是对我真正问题的过分简化。我在我的真实代码中使用了您的解决方案,并且遇到了新的错误。这是我与你的解决方案,我想一个真正的代码:
filteredSignalArray=p.array(filteredSignalArray)
logical=p.logical_and(EndTime-10.0<=matchingTimeArray,matchingTimeArray<=EndTime)
finalStageTime=matchingTimeArray.compress(logical)
finalStageFiltered=filteredSignalArray.compress(logical)
for j in range(len(finalStageTime)):
if j == 0:
outputArray=[[finalStageTime[j],finalStageFiltered[j]]]
else:
outputArray+=[[finalStageTime[j],finalStageFiltered[j]]]
print 'outputArray[:,1].mean() is: ',outputArray[:,1].mean()
这里是现在正由新的代码生成错误消息:
File "mypath\myscript.py", line 1545, in WriteToOutput10SecondsBeforeTimeMarker
print 'outputArray[:,1].mean() is: ',outputArray[:,1].mean()
TypeError: list indices must be integers, not tuple
第二个编辑:
现在这解决了我说:
outputArray=p.array(outputArray)
以上我的代码。
我一直在这太多的时间,需要休息一段时间,如果我犯这类错误。
`outputArray`是一个列表,而不是numpy的阵列。符号`[:,1]`是特定的numpy,而不是Python的一般。 – 2011-01-12 01:45:00