Python 优化嵌套循环操作

Python 优化嵌套循环操作,python,numpy,nested-loops,data-cube,Python,Numpy,Nested Loops,Data Cube,我有一个数据立方体,其中包含图像每个像素的数据(非常像高光谱成像)。 我试图以一种有效的方式在图像的每个像素上拟合一条线。 现在,我是这样做的: 我的datacube是一个6X1024x1024 numpy数组,我还有一个变量包含我的数据的自变量 map = np.zeros((1024,1024)) for i in np.mgrid[1:1024]: for j in np.mgrid[1:1024]: x = independent_variable # This

我有一个数据立方体,其中包含图像每个像素的数据(非常像高光谱成像)。 我试图以一种有效的方式在图像的每个像素上拟合一条线。 现在,我是这样做的:

我的datacube是一个6X1024x1024 numpy数组,我还有一个变量包含我的数据的自变量

map = np.zeros((1024,1024))
for i in np.mgrid[1:1024]:
    for j in np.mgrid[1:1024]:
        x = independent_variable # This is my independent variable
        y = spec_cube[:,i,j] # The Y data to be fitted is the power at each scale, for a pixel
        index = polyfit(x,y,1) # Outputs the slope and the offset
        map[i,j] = index[0] # The pixel value is the index
我知道嵌套for循环通常是最糟糕的事情,但我想不出更好的方法

我尝试了以下操作,但出现了此错误:“ValueError:太多的值无法解包”


加快速度的一种方法:使用:

改进(Python 2.7.1):

在[2]中:def multiline(): …:对于np.mgrid[1:1024]中的i: …:对于np.mgrid[1:1024]中的j: …:通过 ...: 在[3]:def single_line(): …:对于产品中的i,j(np.mgrid[1:1024],np.mgrid[1:1024]): …:通过 ...: In[4]:来自itertools进口产品 在[5]:%timeit multiline()中 10个回路,最好为3个:每个回路138毫秒 在[6]:%timeit single_line() 10个回路,最佳3个:每个回路75.6毫秒
由于循环中的操作是查找直线的斜率,因此我使用了一种不太精确的方法,但使用了数组操作。基本上,为了找到斜率,我做了:每个相邻点的δY/δX,然后平均所有斜率

结果是只需要几分之一秒

以下是新代码:

map = np.zeros((spec_cube.shape[1],spec_cube.shape[2])) # This will be the power index map
x = scale_array
for i in np.mgrid[1:spec_cupe.shape[0]]:
  spec_cube[i-1] = (spec_cube[i]-spec_cube[i-1])/(scale_array[i]-scale_array[i-1])
  map += spec_cube[i-1]
map /= (spec_cube.shape[0]-1)

我的剧本从420秒到9.66秒

谢谢,它确实简化了代码并提供了一些速度。这对我来说不够重要,我想每3秒执行一次,需要412秒。。。我必须找到一种方法来避免这种计算。恭喜你!当你有能力时,请确保将你的答案标记为“已接受”,以便其他人能够从你的成功中学习。Cheers~这个问题和相关问题展示了python/numpy的一个常见问题,当你有一个紧密的内部循环,没有等价的numpy向量操作时,无论你如何努力优化,你基本上都会陷入困境。在这些类型的情况下,人们应该认真考虑其他选择,如C扩展或更好,但Cython。
for (i, j) in itertools.product(np.mgrid[1:1024], np.mgrid[1:1024]):
    ... stuff ...
In [2]: def multiline(): ...: for i in np.mgrid[1:1024]: ...: for j in np.mgrid[1:1024]: ...: pass ...: In [3]: def single_line(): ...: for i, j in product(np.mgrid[1:1024], np.mgrid[1:1024]): ...: pass ...: In [4]: from itertools import product In [5]: %timeit multiline() 10 loops, best of 3: 138 ms per loop In [6]: %timeit single_line() 10 loops, best of 3: 75.6 ms per loop
map = np.zeros((spec_cube.shape[1],spec_cube.shape[2])) # This will be the power index map
x = scale_array
for i in np.mgrid[1:spec_cupe.shape[0]]:
  spec_cube[i-1] = (spec_cube[i]-spec_cube[i-1])/(scale_array[i]-scale_array[i-1])
  map += spec_cube[i-1]
map /= (spec_cube.shape[0]-1)