python - 替代 numpy roll 而不复制数组

标签 python performance numpy

我正在做类似以下代码的事情,我对 np.roll() 函数的性能不满意。我正在对 baseArray 和 otherArray 求和,其中 baseArray 在每次迭代中由一个元素滚动。但是我滚动时不需要 baseArray 的副本,我更喜欢这样的 View ,例如,当我将 baseArray 与其他数组相加时,如果 baseArray 滚动了两次,则 baseArray 的第二个元素与第 0 个元素相加otherArray,baseArray 的第三个元素与 otherArray 的第一个元素相加,等等。

IE。实现与 np.roll() 相同的结果,但不复制数组。

import numpy as np
from numpy import random
import cProfile

def profile():
    baseArray = np.zeros(1000000)
    for i in range(1000):
        baseArray= np.roll(baseArray,1)
        otherArray= np.random.rand(1000000)
        baseArray=baseArray+otherArray

cProfile.run('profile()')

输出(注意第三行 - 滚动功能):
         9005 function calls in 26.741 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    5.123    5.123   26.740   26.740 <ipython-input-101-9006a6c0d2e3>:5(profile)
        1    0.001    0.001   26.741   26.741 <string>:1(<module>)
     1000    0.237    0.000    8.966    0.009 numeric.py:1327(roll)
     1000    0.004    0.000    0.005    0.000 numeric.py:476(asanyarray)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
     1000   12.650    0.013   12.650    0.013 {method 'rand' of 'mtrand.RandomState' objects}
     1000    0.005    0.000    0.005    0.000 {method 'reshape' of 'numpy.ndarray' objects}
     1000    6.390    0.006    6.390    0.006 {method 'take' of 'numpy.ndarray' objects}
     2000    1.345    0.001    1.345    0.001 {numpy.core.multiarray.arange}
     1000    0.001    0.000    0.001    0.000 {numpy.core.multiarray.array}
     1000    0.985    0.001    0.985    0.001 {numpy.core.multiarray.concatenate}
        1    0.000    0.000    0.000    0.000 {numpy.core.multiarray.zeros}
        1    0.000    0.000    0.000    0.000 {range}

最佳答案

我很确定避免复制是不可能的due to the way in which numpy arrays are represented internally .一个数组由一个连续的内存地址块和一些元数据组成,这些元数据包括数组维度、项目大小和每个维度的元素之间的间隔(“步幅”)。向前或向后“滚动”每个元素将需要沿同一维度具有不同的长度步幅,这是不可能的。

也就是说,您可以避免复制 baseArray 中除一个元素之外的所有元素。使用切片索引:

import numpy as np

def profile1(seed=0):
    gen = np.random.RandomState(seed)
    baseArray = np.zeros(1000000)
    for i in range(1000):
        baseArray= np.roll(baseArray,1)
        otherArray= gen.rand(1000000)
        baseArray=baseArray+otherArray
    return baseArray

def profile2(seed=0):
    gen = np.random.RandomState(seed)
    baseArray = np.zeros(1000000)
    for i in range(1000):
        otherArray = gen.rand(1000000)
        tmp1 = baseArray[:-1]               # view of the first n-1 elements
        tmp2 = baseArray[-1]                # copy of the last element
        baseArray[1:]=tmp1+otherArray[1:]   # write the last n-1 elements
        baseArray[0]=tmp2+otherArray[0]     # write the first element
    return baseArray

这些将给出相同的结果:
In [1]: x1 = profile1()

In [2]: x2 = profile2()

In [3]: np.allclose(x1, x2)
Out[3]: True

在实践中,性能没有太大差异:
In [4]: %timeit profile1()
1 loop, best of 3: 23.4 s per loop

In [5]: %timeit profile2()
1 loop, best of 3: 17.3 s per loop

关于python - 替代 numpy roll 而不复制数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35916201/

相关文章:

python - 在 VirtualBox 中的 guest 操作系统上运行程序

python - 即使使用显式 cuda() 调用,pytorch 实例张量也不会移动到 GPU

java - 是否可以完全在内存上使用 Neo4j 而无需持久性,并且仍然能够使用图形算法插件?

python - 对巨大的矩阵进行排序,然后在列表中找到最小的元素及其索引

python - 在 AWS Lambda 函数中导入 numpy 时出现问题

python - 从 Flask 以 CSV 形式返回 Numpy 数组

python - 将 ManyToMany 转换为 ForeignKey (django)

python - 获取括号内的字符串

java - 为什么 Java native 缓冲区那么慢?

c++ - LRU缓存和快速定位对象常用哪些数据结构?