填充 numpy 数组时遇到问题
我下面的代码没有填充名为 RRmeanArray 的 numpy/pylab 数组。谁能告诉我如何修复代码以使其填充数组?
import pylab as p
RRmeanArray = p.array([])
startBeatIndex = 0
endBeatIndex = 10
for k in range(int(p.floor(len(QRSandRRarray[0])/10))-1):
print '++++++++++++++++++++++++++++++++++++++++++++'
print 'k is: ',k
print 'startBeatIndex is: ',startBeatIndex
print 'endBeatIndex is: ',endBeatIndex
print 'p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex]) is: ',p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
newMean = p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
print 'newMean is: ',newMean
RRmeanArray += [newMean]
print 'len(RRmeanArray) is: ',len(RRmeanArray)
startBeatIndex += 10
endBeatIndex += 10
print '++++++++++++++++++++++++++++++++++++++++++++'
以下是我在 python shell 中针对 k 的典型迭代得到的输出:
++++++++++++++++++++++++++++++++++++++++++++
k is: 619
startBeatIndex is: 6190
endBeatIndex is: 6200
p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex]) is: 0.5971
newMean is: 0.5971
len(RRmeanArray) is: 0
++++++++++++++++++++++++++++++++++++++++++++
编辑: 谢谢,托马斯。你几乎明白了。工作版本是:
RRmeanArray = p.zeros(len(range(int(p.floor(len(QRSandRRarray[0])/10))-1)))
startBeatIndex = 0
endBeatIndex = 10
for i,k in enumerate(range(int(p.floor(len(QRSandRRarray[0])/10))-1)):
newMean = p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
RRmeanArray[i] += [newMean]
startBeatIndex += 10
endBeatIndex += 10
这个问题现在已经得到解答。
My code below is not populating the numpy/pylab array called RRmeanArray. Can anyone show me how to fix the code so that it populates the array?
import pylab as p
RRmeanArray = p.array([])
startBeatIndex = 0
endBeatIndex = 10
for k in range(int(p.floor(len(QRSandRRarray[0])/10))-1):
print '++++++++++++++++++++++++++++++++++++++++++++'
print 'k is: ',k
print 'startBeatIndex is: ',startBeatIndex
print 'endBeatIndex is: ',endBeatIndex
print 'p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex]) is: ',p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
newMean = p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
print 'newMean is: ',newMean
RRmeanArray += [newMean]
print 'len(RRmeanArray) is: ',len(RRmeanArray)
startBeatIndex += 10
endBeatIndex += 10
print '++++++++++++++++++++++++++++++++++++++++++++'
Here is what I am getting as output in the python shell for a typical iteration of k:
++++++++++++++++++++++++++++++++++++++++++++
k is: 619
startBeatIndex is: 6190
endBeatIndex is: 6200
p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex]) is: 0.5971
newMean is: 0.5971
len(RRmeanArray) is: 0
++++++++++++++++++++++++++++++++++++++++++++
EDIT:
Thanks, Thomas. You almost got it. The working version is:
RRmeanArray = p.zeros(len(range(int(p.floor(len(QRSandRRarray[0])/10))-1)))
startBeatIndex = 0
endBeatIndex = 10
for i,k in enumerate(range(int(p.floor(len(QRSandRRarray[0])/10))-1)):
newMean = p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
RRmeanArray[i] += [newMean]
startBeatIndex += 10
endBeatIndex += 10
This question is now answered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然 numpy 数组不存在 += 运算符(调用方法 array.extend)。如果您使用的是 numpy 数组,则应该通过使其达到您需要的完整大小来预先分配它。
添加到数组的末尾并不是 numpy 数组的真正用途 - 为此您可能需要一个列表。
编辑:将 newMean 修复为 RRmeanArray,这就是一直以来的含义。
我认为这就是您想要的,而不是您将 RRmeanArray[i] 增加 [newMean] 的版本,但我很高兴有些东西对您有用。
Apparently += operator (which calls the method array.extend) doesn't exist for numpy arrays. If you're using a numpy array, you should preallocate it by making it the full size of what you're going to need.
Adding on to the end of the array isn't really what numpy arrays are for - for that you might want a list.
Edit: fixed newMean to be RRmeanArray, what was meant all along.
I think this is what you want, not your version where you increment RRmeanArray[i] by [newMean], but I'm glad something's working for you.