Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

closest_pairs finds the closest pairs of points in a dataset

Project description

Closest Pairs :triangular_ruler:

Find the closest pairs in an array.

Getting Started

pip install closest_pairs

or install from source:

git clone https://github.com/justinshenk/closest-pairs
cd closest_pairs
pip install .

How to use

import closest_pairs

# X is an n x m numpy array
pairs, distances = closest_pairs.solve(X, n=1)

You can specify how many pairs you want to identify with n.

Example

import closest_pairs
import numpy as np
import matplotlib.pyplot as plt

# Create dataset
X = np.random.random((100,2))
pairs, distance = closest_pairs.solve(X, n=1)

# Plot points
z, y = np.split(X, 2, axis=1)
fig, ax = plt.subplots()
ax.scatter(z, y) 

for i, txt in enumerate(X): 
    if i in pairs: 
        ax.annotate(i, (z[i], y[i]), color='red') 
    else: 
        ax.annotate(i, (z[i], y[i])) 

Check pairs:

In [10]: pairs                                                                                                                                
Out[10]: 
array([[[ 7],
        [16]],

       [[96],
        [50]]])

Output: example_plot

Caveats

closest_pairs will reduce the dimensionality with PCA of your data to two-dimensions for faster processing.

It also removes the first point in a pair if n>1. In rare cases this leads to false negatives if the data is highly overlapping.

Credit and Explanation

Python code modified from Andriy Lazorenko, packaged and made useful for >2 features by Justin Shenk.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
closest_pairs-0.1.5-py3-none-any.whl (4.7 kB) Copy SHA256 hash SHA256 Wheel py3
closest_pairs-0.1.5.tar.gz (4.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page