I have this datacube containing data for each pixel of an image (pretty much like hyperspectral imaging). I'm trying to fit a line on each pixel of the image, in an efficient way. Right now, I do it like this:
My datacube is a 6X1024x1024 numpy array, and I have another variable containing the independent variable of my data.
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
I know that nested for loops are pretty much the worst thing to do in general, but I can't think of a better way.
I tried the following but it gives this error: "ValueError: too many values to unpack"
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