scipy.spatial.distance.squareform¶

scipy.spatial.distance.
squareform
(X, force='no', checks=True)[source]¶ Convert a vectorform distance vector to a squareform distance matrix, and viceversa.
 Parameters
 Xndarray
Either a condensed or redundant distance matrix.
 forcestr, optional
As with MATLAB(TM), if force is equal to
'tovector'
or'tomatrix'
, the input will be treated as a distance matrix or distance vector respectively. checksbool, optional
If set to False, no checks will be made for matrix symmetry nor zero diagonals. This is useful if it is known that
X  X.T1
is small anddiag(X)
is close to zero. These values are ignored any way so they do not disrupt the squareform transformation.
 Returns
 Yndarray
If 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 nbyn symmetric distance matrix
X
,v = squareform(X)
returns an * (n1) / 2
(i.e. binomial coefficient n choose 2) sized vector v where \(v[{n \choose 2}  {ni \choose 2} + (ji1)]\) is the distance between distinct pointsi
andj
. IfX
is nonsquare or asymmetric, an error is raised.X = squareform(v)
Given a
n * (n1) / 2
sized vectorv
for some integern >= 1
encoding distances as described,X = squareform(v)
returns a nbyn distance matrixX
. TheX[i, j]
andX[j, i]
values are set to \(v[{n \choose 2}  {ni \choose 2} + (ji1)]\) 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.