I have a numpy array of n x m values, which may look something like this:
[[ 1, 2, 1, 3, 5],
[ 0, 4, 2, 4, 1],
[ 1, 1, 1, 0, 2]]
I want to calculate the difference and mean from every grid point to its neighbours. I am not sure on how to do this in an easy and fast way, since the actual arrays are much bigger. I also dont know, which is the best way to represent the resulting data. I was thinking something like an nm x nm array, where each value represents a tuple of the desired calculations like this (Pn :
P1 P2 P3 ...
P1 (0, P1) ((P1 - P2), mean(P1, P2)) ((P1 - P2), mean(P1, P2))
P2 ((P2 - P1), mean(P2, P1)) (0, P2) ((P2 - P3), mean(P2, P3))
P3 ((P3 - P1), mean(P3, P1)) ((P3 - P2), mean(P3, P2)) (0, P3)
...
but this seems rather wasteful, since I am only interested in the calcuations for the 8 neighbours.
I think two n x m output grids like this would be best for further processing:
[[(mean1, mean2, mean3...), (mean1, mean2, mean3...), ...],
[(mean1, mean2, mean3...), (mean1, mean2, mean3...), ...],
[(mean1, mean2, mean3...), (mean1, mean2, mean3...), ...]]
This resulting grid should hold a tuple of 8 values for every cell position, but this is also not optimal. This feels like a problem, which should have very clever solutions, so I am hoping for you to understand what I want to do and maybe help me achieve this goal :)
I tried a lot of slicing in numpy and I am expecting an output array (however this may look) which holds all the calculated data.
I am assuming that you want to find the tuple (difference, mean) for each neighbor of a cell, for all the cells.
Option 1:
If you are okay with some wasted space, you can use a list of 8 tuples of (difference, mean), where all the cells on the edges will have some
Nonevalues.Option 2:
Have a
dictfor each cell, to save on space. The key of the dict tells which location the neighbor is, according to the current.