本文简要介绍 python 语言中 scipy.spatial.distance.squareform 的用法。
用法:
scipy.spatial.distance.squareform(X, force='no', checks=True)#将 vector-form 距离向量转换为 square-form 距离矩阵,反之亦然。
- X: array_like
压缩或冗余距离矩阵。
- force: str,可选
与 MATLAB(TM) 一样,如果 force 等于
'tovector'或'tomatrix',则输入将分别被视为距离矩阵或距离向量。- checks: 布尔型,可选
如果设置为 False,则不会检查矩阵对称性或零对角线。如果已知
X - X.T1很小且diag(X)接近于零,则这非常有用。这些值无论如何都会被忽略,因此它们不会破坏正方形转换。
- Y: ndarray
如果传递的是压缩距离矩阵,则返回冗余距离矩阵,如果传递冗余距离矩阵,则返回压缩距离矩阵。
参数 ::
返回 ::
注意:
v = squareform(X)给定一个方阵n-by-n对称距离矩阵
X,v = squareform(X)返回一个n * (n-1) / 2(即二项式系数n选择2)大小的向量v其中是不同点之间的距离i和j.如果X如果是非方形或不对称的,则会产生错误。X = squareform(v)给定
n * (n-1) / 2大小的向量v对于某些整数n >= 1编码距离,如所述,X = squareform(v)返回 n-by-n 距离矩阵X。X[i, j]和X[j, i]值设置为,并且所有对角线元素均为零。
在SciPy 0.19.0 中,
squareform停止将所有输入类型转换为 float64,并开始返回与输入具有相同数据类型的数组。例子:
>>> import numpy as np >>> from scipy.spatial.distance import pdist, squareformx是三维空间中五个点的数组。>>> x = np.array([[2, 0, 2], [2, 2, 3], [-2, 4, 5], [0, 1, 9], [2, 2, 4]])pdist(x)计算x中每对点之间的欧几里得距离。距离以长度为5*(5 - 1)/2 = 10的一维数组返回。>>> distvec = pdist(x) >>> distvec array([2.23606798, 6.40312424, 7.34846923, 2.82842712, 4.89897949, 6.40312424, 1. , 5.38516481, 4.58257569, 5.47722558])squareform(distvec)返回 5x5 距离矩阵。>>> m = squareform(distvec) >>> m array([[0. , 2.23606798, 6.40312424, 7.34846923, 2.82842712], [2.23606798, 0. , 4.89897949, 6.40312424, 1. ], [6.40312424, 4.89897949, 0. , 5.38516481, 4.58257569], [7.34846923, 6.40312424, 5.38516481, 0. , 5.47722558], [2.82842712, 1. , 4.58257569, 5.47722558, 0. ]])当给定平方距离矩阵
m时,squareform(m)返回与该矩阵关联的一维压缩距离向量。在这种情况下,我们恢复distvec。>>> squareform(m) array([2.23606798, 6.40312424, 7.34846923, 2.82842712, 4.89897949, 6.40312424, 1. , 5.38516481, 4.58257569, 5.47722558])
相关用法
- Python SciPy distance.sqeuclidean用法及代码示例
- Python SciPy distance.sokalmichener用法及代码示例
- Python SciPy distance.sokalsneath用法及代码示例
- Python SciPy distance.seuclidean用法及代码示例
- Python SciPy distance.dice用法及代码示例
- Python SciPy distance.braycurtis用法及代码示例
- Python SciPy distance.cityblock用法及代码示例
- Python SciPy distance.jensenshannon用法及代码示例
- Python SciPy distance.kulczynski1用法及代码示例
- Python SciPy distance.jaccard用法及代码示例
- Python SciPy distance.minkowski用法及代码示例
- Python SciPy distance.pdist用法及代码示例
- Python SciPy distance.rogerstanimoto用法及代码示例
- Python SciPy distance.canberra用法及代码示例
- Python SciPy distance.is_valid_y用法及代码示例
- Python SciPy distance.chebyshev用法及代码示例
- Python SciPy distance.russellrao用法及代码示例
- Python SciPy distance.cdist用法及代码示例
- Python SciPy distance.mahalanobis用法及代码示例
- Python SciPy distance.is_valid_dm用法及代码示例
- Python SciPy distance.directed_hausdorff用法及代码示例
- Python SciPy distance.kulsinski用法及代码示例
- Python SciPy distance.yule用法及代码示例
- Python SciPy distance.cosine用法及代码示例
- Python SciPy distance.hamming用法及代码示例
注:本文由纯净天空筛选整理自scipy.org大神的英文原创作品 scipy.spatial.distance.squareform。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
