bundles / scipy 1.17.1 / scipy / spatial / distance / squareform
function
scipy.spatial.distance:squareform
source: /scipy/spatial/distance.py :2154
Signature
def squareform ( X , force = no , checks = True ) Summary
Convert a vector-form distance vector to a square-form distance matrix, and vice-versa.
Parameters
X: array_likeEither a condensed or redundant distance matrix.
force: str, optionalAs with MATLAB(TM), if force is equal to
'tovector'or'tomatrix', the input will be treated as a distance matrix or distance vector respectively.checks: bool, optionalIf set to False, no checks will be made for matrix symmetry nor zero diagonals. This is useful if it is known that
X - X.T1is small anddiag(X)is close to zero. These values are ignored any way so they do not disrupt the squareform transformation.
Returns
Y: ndarrayIf a condensed distance matrix is passed, a redundant one is returned, or if a redundant one is passed, a condensed distance matrix is returned.
Notes
v = squareform(X)Given a square n-by-n symmetric distance matrix
X,v = squareform(X)returns ann * (n-1) / 2(i.e. binomial coefficient n choose 2) sized vectorvwhere is the distance between distinct pointsiandj. IfXis non-square or asymmetric, an error is raised.X = squareform(v)Given an
n * (n-1) / 2sized vectorvfor some integern >= 1encoding distances as described,X = squareform(v)returns an n-by-n distance matrixX. TheX[i, j]andX[j, i]values are set to and all diagonal elements are zero.
In SciPy 0.19.0, squareform stopped casting all input types to float64, and started returning arrays of the same dtype as the input.
Examples
import numpy as np from scipy.spatial.distance import pdist, squareform✓
x = np.array([[2, 0, 2], [2, 2, 3], [-2, 4, 5], [0, 1, 9], [2, 2, 4]])
✓distvec = pdist(x)
✓distvec
✗m = squareform(distvec)
✓m
✗squareform(m)
✗Aliases
-
scipy.cluster._optimal_leaf_ordering.squareform