Skip to main content

Compute distances in numpy arrays with nans

Project description

Nandist: Calculating distances in arrays with missing values

The python library nandist enables (fast) computation of various distances in numpy arrays containing missing (NaN) values. These distances are implemented as a drop-in replacement for distance functions in the scipy.spatial.distance module.

The distance functions in nandist can be used as a drop-in replacement for the distance functions in scipy.spatial.distance. The library nandist offers replacements for all standalone distance functions in scipy and partial support for the fast pairwise distance calculating functions cdist and pdist.

Supported "standalone" distance functions for calculating distances between two arrays (complete):

  • braycurtis
  • canberra
  • chebyshev
  • cityblock
  • cityblock
  • correlation
  • cosine
  • euclidean
  • jensenshannon
  • mahalanobis
  • minkowski
  • seuclidean
  • sqeuclidean

Supported functions for fast calculation of (pairwise) distances between multiple arrays (partial support for metrics):

  • cdist
  • pdist

Supported distance measures in cdist and pdist (passed as metric argument):

  • "chebyshev"
  • "cityblock"
  • "cosine"
  • "euclidean"
  • "minkowski"

Installation

Using pip:

pip install nandist

Usage

A simple example for calculating the cityblock distance between (0, 1) and (NaN, 0) is shown below.

>>> import nandist
>>> import scipy
>>> import numpy as np
>>>
>>> # City-block distance between  (0, 1) and (NaN, 0)
>>> u, v = np.array([0, 1]), np.array([np.nan, 0])
>>> scipy.spatial.distance.cityblock(u, v)
nan
>>> nandist.cityblock(u, v)
1.0

You can replace the function cityblock by any of the supported distance functions.

You can get pairwise distances between arrays in two matrices using cdist. The NaNs do not need to be in the same component.

>>> import nandist
>>> import numpy as np

>>> # City-block distances between vectors A = [(0, 0), (1, NaN)] and vectors B=[(1, NaN) and (1, 1)]
>>> XA, XB = np.array([[0, 0], [1, np.nan]]), np.array([[1, np.nan], [1, 1]])
>>> Y = nandist.cdist(XA, XB, metric="cityblock")
array([[1., 2.],
       [0., 0.]])

Supported metrics

Supported distance metrics are:

  • Chebyshev: chebyshev, metric="chebyshev"
  • Cityblock: cityblock, metric="cityblock"
  • Cosine: cosine, metric="cosine"
  • Euclidean: euclidean, metric="euclidean"
  • Minkowski: minkowski, metric="minkowski"

If you require support for additional distance metrics, please submit an Issue or Merge Request.

How does it work

In nandist, the components where a vector is NaN will be ignored (interpreted as "any number") in the distance metric. This is achieved by replacing NaN values with zeros and correcting the resulting overestimated distance value. Under the hood, nandist calls functions from scipy.spatial.distance and then applies the corrections using numpy linear algebra. This ensures that the outcomes of nandist functions are equivalent to scipy.spatial.distance distance functions when arrays are passed without NaNs in them. In addition, all heavy computational lifting is done through scipy, requiring only the additional computational cost of applying the corrections.

Does it always work?

No. The package nandist performs a correction on an overestimation of the distances when missing values are imputed as zero. It is possible that this correction runs into the limits of floating point arithmetic. In that case, nandist will raise an appropriate error. However, you don't often run into these edge cases in typical usage.

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

nandist-0.10.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

nandist-0.10.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file nandist-0.10.0.tar.gz.

File metadata

  • Download URL: nandist-0.10.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for nandist-0.10.0.tar.gz
Algorithm Hash digest
SHA256 a43bc1cf286cecef4106b4859d798b4d6d62232dbde6dab32d2e5b5dac0673df
MD5 d7bbda8c4a1cfedb40abeecaa1106cbe
BLAKE2b-256 0e690c0b0273ce68b90dd58668d3613bd618f4ff11c3964a2d61611ea22d3a6d

See more details on using hashes here.

File details

Details for the file nandist-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: nandist-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for nandist-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5093efe956278a274eb2cb14f27d18ce952e76ba2c25667546706b5768cde290
MD5 c860bf59b45879a4aa15889a4e588910
BLAKE2b-256 4cd307f48053707b23fe1384bfe6fc47a728141e29872f21076c1c150cc6f474

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