python - 向量化嵌套的 for 循环 Python

标签 python for-loop numpy nested vectorization

我有一个正在迭代的 numpy 数组:

import numpy
import math
array = numpy.array([[1, 1, 2, 8, 2, 2],
               [5, 5, 4, 1, 3, 2],
               [5, 5, 4, 1, 3, 2],
               [5, 5, 4, 1, 3, 2],
               [9, 5, 8, 8, 2, 2],
               [7, 3, 6, 6, 2, 2]])


Pixels = ['U','D','R','L','UL','DL','UR','DR']

for i in range (1,array.shape[0]-1):
    for j in range (1,array.shape[1]-1):


         list = []
         while len(list) < 2:
                iToMakeList = i
                jToMakeList = j

                if iToMakeList > array.shape[0]-1 or iToMakeList < 1 or jToMakeList> array.shape[0]-1 or jToMakeList < 1:

                    break

                PixelCoord = {
            'U' : (iToMakeList-1,jToMakeList),
            'D' : (iToMakeList+1,jToMakeList),
            'R' : (iToMakeList,jToMakeList+1),
            'L' : (iToMakeList,jToMakeList-1),
            'UL' : (iToMakeList-1,jToMakeList-1),
            'DL' : (iToMakeList+1,jToMakeList-1),
            'UR' : (iToMakeList-1,jToMakeList+1),
            'DR' : (iToMakeList+1,jToMakeList+1)
                }
                Value = {
            'U' : array[iToMakeList-1][jToMakeList],
            'D' : array[iToMakeList+1][jToMakeList],
            'R' : array[iToMakeList][jToMakeList+1],
            'L' : array[iToMakeList][jToMakeList-1],
            'UL' : array[iToMakeList-1][jToMakeList-1],
            'DL' : array[iToMakeList+1][jToMakeList-1],
            'UR' : array[iToMakeList-1][jToMakeList+1],
            'DR' : array[iToMakeList+1][jToMakeList+1]
                }


                candidates = []
                for pixel in Pixels:
                    candidates.append((Value[pixel],pixel))

                Lightest = max(candidates)


                list.append(PixelCoord[Lightest[1]])

                iToMakeList = PixelCoord[Lightest[1]][0]
                jToMakeList = PixelCoord[Lightest[1]][1]

我想加快这个过程。这很慢。

假设此代码段的输出是我的最终目标,而我唯一想做的就是加速此代码。

最佳答案

为了使您的问题对我有意义,我认为您需要移动 list = [] 出现的位置。否则,在 list 已满之前,您永远无法访问 i=0j=1。我无法想象它会像当前实现的那样慢——列表很快就会满,然后 for 循环应该很快。这就是我相信你的意图。如果这不正确,请澄清。

for i in range (0,array.shape[0]):
    for j in range (0,array.shape[1]):
         list = []
         while len(list) < 100:
                print "identity", i, j

                #find neighboring entry with greatest value (e.g., assume it is [i-1, j] with value 10)
                list.append((i-1,j))
                i = i-1
                j = j
         #perform operations on list

让我们做一些修改。我假设有一个函数 get_max_nbr(i,j) 返回最大邻居的坐标。您的代码速度较慢的地方之一是它会多次调用 get_max_nbr 以获得相同的坐标(在循环的每个步骤中它都会调用 100 次)。下面的代码使用 memoization解决这个问题(平均减少到 1 次)。因此,如果这是您的瓶颈,这应该能让您获得接近 100 倍的加速。

maxnbr = {}
for i in range(0,array.shape[0]):
    for j in range (0,array.shape[1]):
        list = []
        current_loc = (i,j)
        while len(list) < 100:
            if current_loc not in maxnbr:  #if this is our first time seeing current_loc
                maxnbr[current_loc] = get_max_nbr(*current_loc) #note func(*(i,j)) becomes func(i,j)
            current_loc = maxnbr[current_loc]
            list.append(current_loc)
        #process list here                 

这并没有成功矢量化,但它确实创建了您想要的列表(我认为),这应该是一个重大改进。可能是如果我们对列表处理了解得更多,我们也许能够找到更好的方法,但目前还不清楚。

关于python - 向量化嵌套的 for 循环 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672570/

相关文章:

python - 根据移动设备与桌面优化 Django WebApp 的显示

python - virtualenv 重新启动时删除库( flask /蛋黄)

python - 使用列表推导式计算所有列表元素的总和

Javascript for/in 循环 - 连接来自两个不同对象的项目?

python - 在 numpy 数组中围绕定义的对角线设置递增值

python - numpy.where 是如何工作的?

python - 在 pytest 中,如何暂时禁用类方法中的捕获?

python - 如何改进代码,减少代码量?

java - 在Python中, "args = [temp[n] for n in array(index)]"是否在检查temp[n]?

python - 为什么对称矩阵的Numpy特征向量无法构造原始矩阵