Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

How to Compute Pairwise Distances Between Point Sets in Python with SciPy

Use SciPy pdist for unique pairwise distances within one set, cdist for every cross-set distance, and squareform when you need a full within-set matrix.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use scipy.spatial.distance.pdist to calculate distances among rows in one point set, and scipy.spatial.distance.cdist to calculate every distance between two point sets. The key difference is the output: pdist returns one value per unique within-set pair, while cdist returns a rectangular matrix of cross-set distances.

Choose pdist or cdist

In SciPy, each row of an input array represents one observation or point; columns represent its coordinates or other features. For either function, the compared points must use the same feature dimensions.

As an Amazon Associate I earn from qualifying purchases.

Function Use it for Input shape Output
pdist(X) Distances among rows of one set m × n: m points with n features A condensed vector containing the distances for each unique, unordered pair
cdist(XA, XB) Distances between two sets XA is mA × n; XB is mB × n An mA × mB matrix, with one row per point in XA and one column per point in XB

For example, use pdist to ask how far each point in a dataset is from the others. Use cdist when you have a reference set and a separate set of query points, and need every cross-set comparison. The SciPy pdist reference and SciPy cdist reference describe their inputs and return shapes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compute within-set and cross-set distances

This example uses two-dimensional coordinates and Euclidean distance, the default metric for both functions:

import numpy as np
from scipy.spatial.distance import cdist, pdist, squareform

X = np.array([[0.0, 0.0], [3.0, 4.0], [3.0, 0.0]])
Y = np.array([[1.0, 1.0], [4.0, 4.0]])

within = pdist(X, metric="euclidean")
within_square = squareform(within)
between = cdist(X, Y, metric="euclidean")

within has three values because three points form three unique unordered pairs. It does not repeat each distance in the opposite direction or include self-distances. within_square is the corresponding symmetric matrix. Since X has three rows and Y has two, between has shape (3, 2).

Both inputs to cdist have two columns, so their points share the same feature space. If the number of columns differs, the inputs are not compatible for the direct pairwise calculation.

Convert a condensed result to a square matrix

The compact output from pdist is useful when you need only unique pair values. If a downstream operation or display needs a full matrix, pass the vector to squareform. It can also convert a square distance matrix back to condensed form. See the SciPy squareform reference for the conversion details.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
within_square = squareform(within)
within_again = squareform(within_square)

A square within-set distance matrix is symmetric: the distance from point A to point B equals the distance from B to point A. Its diagonal represents self-comparisons. In contrast, cdist is rectangular when the two sets have different numbers of points, and its rows and columns correspond to different inputs.

Select a metric that matches the data

Both functions accept a metric name or a callable. Euclidean is straight-line distance in the feature coordinates, but it is not automatically the right interpretation for every dataset. The SciPy distance-computations manual documents supported metrics and their definitions.

  • Euclidean: straight-line distance between coordinate vectors.
  • Cityblock: Manhattan distance, computed by summing absolute coordinate-wise differences.
  • Cosine: compares vector direction; it is often relevant when orientation matters more than magnitude.
  • Correlation: compares centered patterns across vector entries.
  • Jaccard and Hamming: dissimilarity choices for Boolean or categorical-style representations where their definitions fit the data.
  • Minkowski: a family of distances whose behavior depends on the parameter p.

For instance, request Manhattan distance with metric="cityblock". Metric choice changes what a distance means, so select it based on the representation and the question being asked rather than treating one metric as universally preferable.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Pass parameters for parameterized metrics

Some metrics depend on values beyond the input points. The SciPy function references document options including Minkowski parameter p, coordinate weights w, standardized-Euclidean variance V, and Mahalanobis inverse covariance VI. Set these deliberately when using the corresponding metric; they affect the resulting distances.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For standardized Euclidean distance, V supplies the variance values used for standardization. For Mahalanobis distance, VI supplies the inverse covariance matrix. Consult the version-specific pdist and cdist documentation for the accepted arguments and metric-specific behavior.

Check dimensions and output needs before calculating

  • Confirm that rows are points and columns are comparable features in the same order for both arrays.
  • Use pdist only when you want all pairwise distances within one set; its condensed vector is not an m × m matrix.
  • Use cdist for all comparisons across two sets; its output size follows the number of rows in each input.
  • Choose a metric and any required parameters before interpreting the values.
  • For large inputs, decide whether you actually need every pair and whether the resulting vector or matrix fits your workflow. The API references establish the output forms but do not establish a universal runtime or memory limit.

The cited API pages are for SciPy v1.18.0. Function signatures and supported metric details can differ by installed release, so check the manual for the SciPy version in your environment. The broader SciPy spatial algorithms and data structures manual provides context for related spatial tools.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.