Skip to main content

Closely 📐

PyPI version Build Status

Find the closest pairs in an array.

Closely uses principal component analysis (PCA) to reduce the dimensions to 2 and a divide and conquer algorithm to find the closest pair of points.

Getting Started

pip install closely

or install from source:

git clone https://github.com/justinshenk/closely
cd closely
pip install .

How to use

import closely

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

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

Example

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

# Create dataset
X = np.random.random((100,2))
pairs, distance = closely.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]))

plt.show() 

Check pairs:

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

Output: example_plot

Caveats

If your data has more than 3 features, closely will reduce the dimensionality by projecting it onto two directions that explain most of the variance. This speeds up processing, but is not 100% precise. In other words, if your data has four columns (eg, x, y, z, a), it will apply divide-and-conquer on the new projection bases P1 and P2.

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.

Metadata

Release files for closely 19.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for closely 19.0.2
File Size Uploaded
closely-19.0.2.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for closely 19.0.2
File Interpreter ABI Platform
closely-19.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / closely-19.0.2.tar.gz

Download URL closely-19.0.2.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
848ab89b9d6958c938ca83225c26cae476fb5ebba86f054eaedb77513498dff4
BLAKE2b-256 checksum
How to use checksums
8e4682d1d9d0b2a42af880c7dda3f57d1b065681d9f1b2c51c7519347cb776ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / closely-19.0.2-py3-none-any.whl

Download URL closely-19.0.2-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
739365668d6beeed40137d28b937abeaa1ca2e6dd15d71eced9750c819b73125
BLAKE2b-256 checksum
How to use checksums
39c1126873342cd0ea249321adbaf3766a771a986d1b09d935a578116e890aaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

This release

19.0.2 This release

2 release files

19.0.1

2 release files

19.0.0

2 release files

0.1.8

1 release file

0.1.6

2 release files

0.1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page