Is there an efficient numpy mechanism to generate an array of values from a 2D array given a list of indexes into that array?
Specifically, I have a list of 2D coordinates that represent interesting values in a 2D numpy array. I calculate those coordinates as follows:
nonzeroValidIndices = numpy.where((array2d != noDataValue) & (array2d != 0))
nonzeroValidCoordinates = zip(nonzeroValidIndices[0],nonzeroValidIndices[1])
From there, I’m building a map by looping over the coordinates and indexing into the numpy array one at a time similarly to this simplified example:
for coord in nonzeroValidCoordinates:
map[coord] = array2d[coord]
I have several massive datasets I’m iterating this algorithm over so I’m interested in an efficient solution. Through profiling, I suspect that array2d[coord] line is causing some pain. Is there a better vector form to generate an entire vector of values from array2d or am I stuck with indexing one at a time?
I think you could try something like:
Or if you really want to use
nonzeroValidCoordinates:Source: Link