- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
scipy.spatial.distance.pdist
返回一个压缩的距离矩阵。来自 the documentation :
Returns a condensed distance matrix Y. For each and (where ), the metric dist(u=X[i], v=X[j]) is computed and stored in entry ij.
我认为 ij
的意思是 i*j
。但我想我可能错了。考虑
X = array([[1,2], [1,2], [3,4]])
dist_matrix = pdist(X)
然后文档说 dist(X[0], X[2])
应该是 dist_matrix[0*2]
。但是,dist_matrix[0*2]
是 0——而不是应该的 2.8。
在给定 i
和 j
的情况下,我应该使用什么公式来访问两个向量的相似性?
最佳答案
你可以这样看:假设 x
是 m × n。一次选择两个可能的 m
行对是 itertools.combinations(range(m), 2)
,例如,对于 m=3
:
>>> import itertools
>>> list(combinations(range(3),2))
[(0, 1), (0, 2), (1, 2)]
所以如果 d = pdist(x)
,combinations(range(m), 2))
中的第 k
个元组给出与 d[k]
关联的 x
行的索引。
例子:
>>> x = array([[0,10],[10,10],[20,20]])
>>> pdist(x)
array([ 10. , 22.36067977, 14.14213562])
第一个元素是 dist(x[0], x[1])
,第二个是 dist(x[0], x[2])
和第三个是dist(x[1], x[2])
。
或者您可以将其视为平方距离矩阵的上三角部分中的元素,串在一起形成一维数组。
例如
>>> squareform(pdist(x))
array([[ 0. , 10. , 22.361],
[ 10. , 0. , 14.142],
[ 22.361, 14.142, 0. ]])
>>> y = array([[0,10],[10,10],[20,20],[10,0]])
>>> squareform(pdist(y))
array([[ 0. , 10. , 22.361, 14.142],
[ 10. , 0. , 14.142, 10. ],
[ 22.361, 14.142, 0. , 22.361],
[ 14.142, 10. , 22.361, 0. ]])
>>> pdist(y)
array([ 10. , 22.361, 14.142, 14.142, 10. , 22.361])
关于python - 压缩距离矩阵如何工作? (pdist),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079563/
我正在使用 pdist 命令查找存储在矩阵中的 x 和 y 坐标之间的距离。 X = [100 100; 0 100; 100 0; 500 400; 3
scipy.spatial.distance.pdist 返回一个压缩的距离矩阵。来自 the documentation : Returns a condensed distance matrix
我想根据 pdist 的结果创建一个矩阵。 pdist 返回距离向量:1-2, 1-3, 1-4.. 2-3.. 等。 我试图按照建议使用它来获得上三角形: a = [1,2,3,4,5,6,7,8,
octave 文档说 pdist 存在,但我无法在 ubuntu 12.04 上安装的版本中使用它。 Octave 版本: GNU Octave, version 3.6.2 Copyright (C
我一直在用 scipy 计算成对距离,并且试图获取到两个最近邻居的距离。我当前的工作解决方案是: dists = squareform(pdist(xs.todense())) dists = np.
我已经使用 scipy.spatial.distance.pdist(X) 来计算下面列表 X 的每对元素之间的欧几里德距离度量: X = [[0, 3, 4, 2], [23, 5, 32, 1],
我有一个 theano 符号矩阵 x = T.fmatrix('input') x 稍后将由 n 个暗淡的 d 向量填充(在训练时)。 我想要 theano 等同于 pdist(scipy.spati
在 scipy 中调用 pdist 时如何指定缺失值?即此处描述的功能: http://docs.scipy.org/doc/scipy/reference/generated/scipy.spati
我编写了以下代码 arr_coord = [] for chains in structure: for chain in chains: for residue in cha
我正在尝试对时间序列进行聚类。簇内元素具有相同的形状,但比例不同。因此,我想使用相关性度量作为聚类的度量。我正在尝试相关或 PIL 逊系数距离(欢迎任何建议或替代方案)。但是,当我运行 Z = lin
使用 scipy.spatial.distance.euclidean 计算两个复数之间的欧几里得距离: import numpy import scipy.spatial.distance z1 =
我一直对 scipy.spatial.distance.pdist(...) 的用法感兴趣在 python 中,它对于我一直在处理的一些应用程序来说已经变得有用且快速。 我需要使用自定义的成对距离函数
如何在 Python 中计算字符串的 Jaro Winkler 距离矩阵? 我有大量手写字符串(名称和记录编号),我试图在列表中查找重复项,包括拼写可能略有不同的重复项。 response to a
我有一个大型数据框(例如 15k 个对象),其中每一行都是一个对象,列是数字对象特征。它的形式是: df = pd.DataFrame({ 'A' : [0, 0, 1],
我有一个 Pandas 数据框(请参见下面的小示例)。我想根据 3 列(特征)中的值计算观测值(行)之间的欧几里德距离。我正在使用 scipy.spatial.distance.pdist。 据我了解
我使用 scipy 的 pdist 和相关度量来构建相关矩阵,但这些值与我从 numpy 的 corrcoef 获得的值不匹配。 我将 pdist 应用于一个非常简单的两个具有相同值的一维数组:[1,
我又遇到了一个奇怪的问题。 假设我有以下虚拟数据框(通过演示我的问题): import numpy as np import pandas as pd import string # Test dat
我正在尝试使用 DBSCAN(scikit 学习实现)和位置数据进行聚类。我的数据是 np 数组格式,但要将 DBSCAN 与 Haversine 公式一起使用,我需要创建一个距离矩阵。当我尝试执行此
我是一名优秀的程序员,十分优秀!