Condensed distance matrix - Calculating pairwise distances between data-points using SciPy

The scipy function pdist() from the spatial module returns pairwise distanced between data-points as a condensed distance matrix in a one-dimensional ndarray.

Distance Calculcations Updated 8 Jul 2025 1 min read

Overview

  • For a given set of points the pdist() function computes and returns pairwise distances between the data points.
  • The pairwise distances are returned as a condensed distance matrix in a flat 1-dimensional ndarray containing the upper triangular portion of the distance matrix. The upper triangular portion refers to the region above the main diagonal of the matrix.
  • In a distance matrix, both lower triangular portion and upper triangular are equal and the main diagonal contains all zero values.
ParrotCrowSparrowSeagullSnowy OwlDuck
Parrot00.050407940.200399661.300001051.500023240.50262991
Crow0.0504079400.150130151.350008361.550070180.55302356
Sparrow0.200399660.1501301501.500040331.70012970.70291963
Seagull1.300001051.350008361.5000403300.200249840.8017537
Snowy Owl1.500023241.550070181.70012970.2002498401.00092407
Duck0.502629910.553023560.702919630.80175371.000924070
  • The default metric used for distance calculation is Euclidean.
  • For the given data-points, the condensed distance matrix can be directly passed on to the SciPy linkage() function to create hierarchical clusters in an agglomerative fashion.
  • The SciPy function pdist() supports several metrics including 'braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulczynski1’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’ and ‘yule’ methods.

Example

python
# Example Python program that returns pairwise distances

            # between two-dimnsional points as a condensed distance matrix

            # (a one dimensional numpy array)

            import scipy.spatial.distance as dist

            import numpy as np

# Body weight vs brain weight of birds

            data_points = np.array([[0.5,    0.01365],     # Parrot

                                    [0.45,   0.00725],     # Crow

                                    [0.3,    0.001],     # Sparrow

                                    [1.8,    0.012],    # Sea gull

                                    [2,      0.022],     # Snowy owl

                                    [1,      0.065]        # Duck

                                   ])

# Find the pairwise distances

            ds = dist.pdist(data_points)

            print("Condensed distance matrix:")

            print(type(ds))

            print("Distances:")

            print(ds)

            print("Length of the distance matrix:")

            print(len(ds))

Output

Output
Condensed distance matrix:

            <class 'numpy.ndarray'>

            Distances:

            [0.05040794 0.20039966 1.30000105 1.50002324 0.50262991 0.15013015

             1.35000836 1.55007018 0.55302356 1.50004033 1.7001297  0.70291963

             0.20024984 0.8017537  1.00092407]

            Length of the distance matrix:

            15