Numpy - Finding matches across multiple co-ordinates


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


<strong>Approach #1</strong>

We are working with NumPy arrays, so we can leverage <a href="https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html" rel="nofollow">broadcasting</a> for a vectorized solution -

ar = np.array(som.bmus) points = np.unique(ar, axis = 0) mask = (ar[:,0,None]==points[:,0]) & (ar[:,1,None]==points[:,1]) indices = np.argwhere(mask)

<strong>Approach #1-G</strong>

One more compact way to get mask, which covers for a generic no. of columns in ar, would be -

mask = (ar[:,None] == points).all(axis=2)

<strong>Approach #2</strong>

Another memory-efficient approach for generic no. of cols would be with views and <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.searchsorted.html" rel="nofollow">np.searchsorted</a> -

# https://stackoverflow.com/a/45313353/ @Divakar def view1D(a, b): # a, b are arrays a = np.ascontiguousarray(a) b = np.ascontiguousarray(b) void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1])) return a.view(void_dt).ravel(), b.view(void_dt).ravel() n = len(ar) indices = np.empty((n,2),dtype=int) indices[:,0] = np.arange(n) a,b = view1D(ar, points) # ar, points from app#1 indices[:,1] = np.searchsorted(b, a)


With numpy arrays, you can use <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.array_equal.html" rel="nofollow">np.array_equal</a>. This tests for same shape and same elements.

But if your logic is as simple as the code you have, use <a href="https://stackoverflow.com/a/49731025/9209546" rel="nofollow">@Divakar's vectorized solution</a>.

points = np.unique(np.array(som.bmus), axis = 0) for idx, bmu in enumerate(som.bmus): for point_idx, point in enumerate(points): if np.array_equal(bmu, point): print(idx, point_idx) break


  • Which MySQL collation compares e.g. é and e as equal?
  • Determining cluster membership in SOM (Self Organizing Map) for time series data
  • import XML to HTML
  • Creating a Countdown Watch with a slider in Xcode
  • getting cpu usage of one process using c++ (win32)
  • Keeping rows with at defined numbers of NAs in a dataframe [duplicate]
  • Splitting on comma outside quotes when escaped quotes exist
  • Solve apparent need for outside reference to entity inside aggregate (DDD)
  • How to asynchronously apply function via Spark to subsets of dataframe?
  • Using Regex to split XML string before and after match
  • Implement Gauss-Jordan elimination in Haskell
  • Streaming data from a NVarchar(Max) column using c#
  • Slowly changing dimension - What is Pure type 6 implementation
  • What's a fast (non-loop) way to apply a dict to a ndarray (meaning use elements as keys and rep
  • SQL append distinct values from two columns and two tables
  • Hibernate: Inheritance and relationship mapping + generics
  • help('modules') crashing? Not sure how to fix
  • Can XOR be expressed using SKI combinators?
  • Should I use composite primary keys in Grails?
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • Put value at centre of bins for histogram
  • Where can I find tomesh.c?
  • How can I replace the server in Web Component Tester
  • Android Studio Can't Find tools.jar
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Make new pandas columns based on pipe-delimited column with possible repeats
  • Fetching methods from BroadcastReceiver to update UI
  • How reduce the height of an mschart by breaking up the y-axis
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • PHP: When would you need the self:: keyword?
  • Proper folder structure for lots of source files
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Does armcc optimizes non-volatile variables with -O0?