我的目标是从朱莉娅的pdist()中复制SciPy的功能。我尝试使用Distances.jl包对观测之间的距离进行配对计算。然而,结果与下面提到的例子不一样。
Python示例:
from scipy.spatial.distance import pdist
a = [[1,2], [3,4], [5,6], [7,8]]
b = pdist(a)
print(b)
output --> array([2.82842712, 5.65685425, 8.48528137, 2.82842712, 5.65685425, 2.82842712])朱莉娅例子:
using Distances
a = [1 2; 3 4; 5 6; 7 8]
dist_function(x) = pairwise(Euclidean(), x, dims = 1)
dist_function(a)
output -->
4×4 Array{Float64,2}:
0.0 2.82843 5.65685 8.48528
2.82843 0.0 2.82843 5.65685
5.65685 2.82843 0.0 2.82843
8.48528 5.65685 2.82843 0.0关于上述例子:
pdist() SciPy in python中的度量值默认设置为Euclidean()?中复制结果。
请提出解决这个问题的办法。
pdist()的文档参考:-> https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html
提前谢谢!!
发布于 2021-02-17 08:38:26
根据您链接的文档页,要获得与python中的Julia相同的表单(是的,我知道,这是您的问题的反面),您可以将它传递给正方形表单。例如,在您的示例中,添加
from scipy.spatial.distance import squareform
squareform(b)另外,是的,在相同的文档页面中,您可以看到“度量”参数默认为“欧几里得”,如果没有明确定义的话。
对于相反的情况,只需注意python向量就是非对角线中的所有元素(因为对于“适当”距离度量,生成的距离矩阵是对称的)。
所以你可以简单地把所有的元素从对角线上收集到一个向量中。
发布于 2021-02-17 08:38:51
对于(1),答案是肯定的,根据你所链接的文档,上面写着
scipy.spatial.distance.pdist(X, metric='euclidean', *args, **kwargs)指示默认情况下,metric arg确实被设置为'euclidean'。
我不太明白你的第二个问题-结果是一样的?对我来说,唯一的区别似乎是,this将上三角形作为向量返回,所以如果要这样做,请看一下:https://discourse.julialang.org/t/vector-of-upper-triangle/7764
https://stackoverflow.com/questions/66237199
复制相似问题