我有一個包含數據的圖像的每個像素數據立方體這個(非常像高光譜成像)。 我試圖以一種有效的方式在圖像的每個像素上劃一條線。 現在,我這樣做:優化嵌套循環操作
我的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異常:值過多解壓」
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
這與相關的問題表現出與Python/numpy的一個共同的問題,當你有沒有類似numpy的矢量操作,那麼你基本上是停留無論你怎麼努力優化緊湊的內部循環。在這些類型的情況下,應該認真考慮其他選擇,例如C-extensions或更好的Cython。 –