Skip to main content

Closely find closest pairs of points, eg duplicates, in a dataset

Project description

Closely :triangular_ruler:

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.

Project details


Download files

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

Source Distribution

closely-19.0.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

closely-19.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file closely-19.0.1.tar.gz.

File metadata

  • Download URL: closely-19.0.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for closely-19.0.1.tar.gz
Algorithm Hash digest
SHA256 d82c806ef0fbb577c4fdbd01c83e8b910e949c6cdfaa9c43a4d34b8d01973c12
MD5 ed2efa478c94f137b6ba4433442e9ff9
BLAKE2b-256 661c07179495d3ff0762b33c69b2029b8720207f7c9b74c6eaa0ef4b4a250f66

See more details on using hashes here.

File details

Details for the file closely-19.0.1-py3-none-any.whl.

File metadata

  • Download URL: closely-19.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for closely-19.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 964ecdc596ea336184b4cb1bf0281d1523cd27852bf2d84427e53adfbea6828c
MD5 be64d0efa07deaa1595e1b24a9329a6d
BLAKE2b-256 b4f699fc6944267cd8adfbffdb98555d17f5ed913a82464ac6f7ccdf0e8cad93

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page