I'm using somoclu
to produce an emergent Self-Organising Map of some data. Once I have the BMUs (Best Matching Units) I'm performing a Delaunay Triangulation on the co-ordinates of the BMUs in order to find each BMU's neighbours in the SOM.
In the following snippet of Python, is there a more Pythonic version of the a == c and b == d
conditional? In other words, how can I compare bmu
and point
directly without splitting out the separate co-ordinates?
points = np.unique(np.array(som.bmus), axis = 0)
for idx, bmu in enumerate(som.bmus):
a, b = bmu
for point_idx, point in enumerate(points):
c, d = point
if a == c and b == d: # More Pythonic version of this line?
print (idx, point_idx)
break
With
numpy
arrays, you can usenp.array_equal
. This tests for same shape and same elements.But if your logic is as simple as the code you have, use @Divakar's vectorized solution.
Approach #1
We are working with NumPy arrays, so we can leverage
broadcasting
for a vectorized solution -Approach #1-G
One more compact way to get
mask
, which covers for a generic no. of columns inar
, would be -Approach #2
Another memory-efficient approach for generic no. of cols would be with
views
andnp.searchsorted
-