Phi*_*Kay 2 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 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
Run Code Online (Sandbox Code Playgroud)
我知道嵌套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
Run Code Online (Sandbox Code Playgroud)
一种加快速度的方法:使用itertools.product:
for (i, j) in itertools.product(np.mgrid[1:1024], np.mgrid[1:1024]):
... stuff ...
Run Code Online (Sandbox Code Playgroud)
改进(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