填充 numpy 数组时遇到问题

发布于 2024-10-06 21:30:31 字数 1695 浏览 1 评论 0原文

我下面的代码没有填充名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-10-13 21:30:31

显然 numpy 数组不存在 += 运算符(调用方法 array.extend)。如果您使用的是 numpy 数组,则应该通过使其达到您需要的完整大小来预先分配它。

RRmeanArray = p.zeros(len(range(int(p.floor(len(QRSandRRarray[0])/10))-1)))
for i,k in enumerate(range(int(p.floor(len(QRSandRRarray[0])/10))-1)):
    RRmeanArray[i] = p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
    startBeatIndex += 10
    endBeatIndex += 10

添加到数组的末尾并不是 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.

RRmeanArray = p.zeros(len(range(int(p.floor(len(QRSandRRarray[0])/10))-1)))
for i,k in enumerate(range(int(p.floor(len(QRSandRRarray[0])/10))-1)):
    RRmeanArray[i] = p.mean(QRSandRRarray[1,startBeatIndex:endBeatIndex])
    startBeatIndex += 10
    endBeatIndex += 10

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文