python - 删除 numpy 语句中的 for 循环

标签 python performance numpy vectorization

有一个数组,例如:

x:
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9]],

       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]],

       [[30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49]],

       [[50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]],

       [[60, 61, 62, 63, 64],
        [65, 66, 67, 68, 69]],

       [[70, 71, 72, 73, 74],
        [75, 76, 77, 78, 79]],

       [[80, 81, 82, 83, 84],
        [85, 86, 87, 88, 89]],

       [[90, 91, 92, 93, 94],
        [95, 96, 97, 98, 99]]])

目标是按 i+3 个项目对每个项目进行分组,并且在每个组中检查零轴的所有项目都大于 30。

对项目 i 和 i+3 进行分组:

for i in range(0,x.shape[0]-3):
    x[i:i+3]
    print()

array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9]],

       [[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]]])

array([[[10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]],

       [[30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]]])

array([[[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29]],

       [[30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49]]])

array([[[30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49]],

       [[50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]]])

array([[[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49]],

       [[50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]],

       [[60, 61, 62, 63, 64],
        [65, 66, 67, 68, 69]]])

array([[[50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]],

       [[60, 61, 62, 63, 64],
        [65, 66, 67, 68, 69]],

       [[70, 71, 72, 73, 74],
        [75, 76, 77, 78, 79]]])

array([[[60, 61, 62, 63, 64],
        [65, 66, 67, 68, 69]],

       [[70, 71, 72, 73, 74],
        [75, 76, 77, 78, 79]],

       [[80, 81, 82, 83, 84],
        [85, 86, 87, 88, 89]]])

最后检查条件:

for i in range(0,x.shape[0]-3+1):
    (x[i:i+3] > 30).all(axis=0)
    print()


array([[False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

array([[False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

array([[False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)

array([[False,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True]], dtype=bool)

问题是:有没有办法去掉for循环?以获得更好的性能。

最佳答案

这是一种使用 np.lib.stride_tricks.as_strided 的有效方法-

(strided_axis0(x,3)>30).all(1)

基于步幅的函数strided_axis0来自 here .

关于python - 删除 numpy 语句中的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44282625/

相关文章:

ios - 在 Swift 中,当我声明一个非可选属性但不能保证初始化时,我是否会丢失任何资源?

python - 各种性能指标中的sample_weight参数是什么

Python-根据平均值将递增的类分配给列表val

python - 如何解析列表的列表并将元素一起分析以查看它们随时间出现了多少次?

python - 在嵌套的for循环中获取迭代计数器

python - python/jinja 中的正则表达式模式替换

android - 已安装应用列表强制关闭 : java. lang.ClassCastException

python - python中字符串的终止符

python - 如何将一串句子重新格式化为每行 Python 一个句子

python - 从 Numpy 的 SVD 分解中获得负 S 值?