python - 优化嵌套循环操作

标签 python numpy nested-loops data-cube

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

我的数据立方体是一个 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:要解压的值太多”

map = np.zeros((1024,1024))
for i,j in map:
    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

最佳答案

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

for (i, j) in itertools.product(np.mgrid[1:1024], np.mgrid[1:1024]):
    ... stuff ...

改进(Python 2.7.1):

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

关于python - 优化嵌套循环操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924702/

相关文章:

python - 文本文件中的输出与 python 输出不同

Python 列表插入函数和数据类型问题

python - 选择和操作 .csv 中的列

python - 根据时间戳中存在的两列中的元素分隔行

python - Pandas 中的 groupby -Python

python - 在Mac上的终端中停止python

c++ - 打印与数组中的值一样多的星星

python - Itertools 相当于嵌套循环 "for x in xs: for y in ys..."

python - 在几个列表中查找重复项

python - pysqlite - 如何保存图像