3867

Find the index of the min value in a pdist condensed distance matrix

Question:

I have used scipy.spatial.distance.pdist(X) to calculate the euclidian distance metric between each pair of elements of the below list X:

X = [[0, 3, 4, 2], [23, 5, 32, 1], [3, 4, 2, 1], [33, 54, 5, 12]]

This returns a condensed distance matrix:

array([ 36.30426972, 3.87298335, 61.57109712, 36.06937759, 57.88782255, 59.41380311])

For each element X, I need to find the index of the closest other element.

Converting the condensed distance matrix to square form help visualize the results, but I can't figure out how to programmatically identify the index of the closest element X for each element in X.

array([[ 0. , 36.30426972, 3.87298335, 61.57109712], [ 36.30426972, 0. , 36.06937759, 57.88782255], [ 3.87298335, 36.06937759, 0. , 59.41380311], [ 61.57109712, 57.88782255, 59.41380311, 0. ]])

I believe argmin() is the function to use, but I'm lost from here. Thanks for any help in advance.

Answer1:

We'll operate on the square form of the results. First, to exclude "New York is closest to New York" answers,

numpy.fill_diagonal(distances, numpy.inf)

Then, it's a simple argmin along an axis:

closest_points = distances.argmin(axis=0)

Recommend

  • Does kafka lose message if consumer holds message longer then auto commit interval time?
  • node.js Nerve framework unicode response
  • How to crash a react native android app
  • Unable to delete User objects in Django
  • How do you extend the SimpleMembership authentication in ASP.NET MVC4
  • Label histogram by bins matplotlib [duplicate]
  • How to perform cartesian product with Tensorflow? [duplicate]
  • Error in Java : java.io.FileNotFoundException: C:\\Users\\FSSD\\Desktop\\My Test (Access is
  • How do I port query with GROUP BY clause to PostgreSQL?
  • Is there any example how to use Matrix Toolkit Java (MTJ)? [closed]
  • ORA-24247: network access denied by access control list (ACL) while sending email oracle
  • Upgrading Ember inside of Ember-CLI
  • Finding the Kth Largest element in a Python List using recursion
  • Fine Uploader to S3 Folder
  • Multiple instances of a wpf user control all use the same viewmodel
  • How can I disable the arrows on a FlipView?
  • Unity method doesn't work when other script accesses?
  • $geoNear aggregation ignoring maxDistance
  • Neo4j 2.0 - summing up different property weights - Problems with WHERE clause
  • Neo4J - Optimizing 3 merge queries into a single query
  • Regular expression to check if a String is a positive natural number
  • Rotate links along a circle
  • Convert a hashmap to a json string in spring-boot
  • NSTimers causing leaks
  • How to return multiple result in the same query?
  • Why do I need an infinite loop in STM32 programming?
  • How to create mirrored image effect with CSS single element
  • How to modify the way a ForeignKey field is rendered in a Django admin page to avoid browser crash?
  • How to convert days into months using datetime in Python3?
  • Strange results while measuring delta time on Linux
  • Year over Year Stats from a Crossfilter Dataset
  • How to turn off notice reporting in xampp?
  • Cloud Code: Creating a Parse.File from URL
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to get rgb from transparent pixel in js
  • Make checkout phone field optional for specific countries in WooCommerce
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Angular 4: Responsive Grid List
  • Write to .csv file with PHP (Commas in Data Error)
  • Access to a Matlab gui from the web