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.
Compute within-set and cross-set distances
This example uses two-dimensional coordinates and Euclidean distance, the default metric for both functions:
#1 Best Overall
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.
Rank #2
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.
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.
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.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallFor 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.
Best Value
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
pdistonly when you want all pairwise distances within one set; its condensed vector is not anm × mmatrix. - Use
cdistfor 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.
Quick Recap
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.




