I'm trying to perform a number of functions to get some results from a set of satellite imagery (in the example case I am performing similarity functions). I first intended to iterate through all the pixels simultaneously, each containing 4 numbers, then calculating a value for each one based off these too numbers then write it to an array e.g scipy.spatial.distance.correlation(pixels_0, pixels_1).
The issue I have is when I run this loop I am having issues getting it to save to an array 1000x1000 giving it a value for each pixel.
array_0 = # some array with dimensions(1000, 1000, 4)
array_1 = # some array with dimensions(1000, 1000, 4)
result_array = 
for rows_0, rows_1 in itertools.izip(array_0, array_1):
for pixels_0, pixels_1 in itertools.izip(rows_0, rows_1):
results = some_function(pixels_0, pixels_1)
>>> # successfully prints desired results
>>> # unsuccessful in creating the desired array
a = np.random.rand(10, 10, 4)
b = np.random.rand(10, 10, 4)
def dotprod(T0, T1):
return np.dot(T0, T1)/(np.linalg.norm(T0)*np.linalg.norm(T1))
results =dotprod(a.flatten(), b.flatten())
results = results.reshape(a.shape)
The best way is to use
Numpy for your task. You should think in vectors. And you should write your
some_function()to work in a vectorized manner. Here is an example:
array_0 = np.random.rand(1000,1000,4) array_1 = np.random.rand(1000,1000,4) results = some_function(array_0.flatten(), array_1.flatten()) ## this will be (1000*1000*4 X 1) results = results.reshape(array_0.shape) ## reshaping to make it the way you want it.