【发布时间】:2019-04-15 16:27:13
【问题描述】:
我在这个函数中输入了不同的值并观察了输出。但我在输出的内容中找不到可预测的模式。
然后我尝试挖掘 function 本身,但它令人困惑,因为它可以进行许多不同的计算。
根据Docs:
Compute the distance matrix from a vector array X and optional Y.
我看到它返回一个高度和宽度矩阵,等于输入的嵌套列表的数量,这意味着它正在比较每个列表。
但除此之外,我很难理解它在做什么以及价值来自哪里。
我尝试过的示例:
pairwise_distances([[1]], metric='correlation')
>>> array([[0.]])
pairwise_distances([[1], [1]], metric='correlation')
>>> array([[ 0., nan],
>>> [nan, 0.]])
# returns same as last input although input values differ
pairwise_distances([[1], [2]], metric='correlation')
>>> array([[ 0., nan],
>>> [nan, 0.]])
pairwise_distances([[1,2], [1,2]], metric='correlation')
>>> array([[0.00000000e+00, 2.22044605e-16],
>>> [2.22044605e-16, 0.00000000e+00]])
# returns same as last input although input values differ
# I incorrectly expected more distance because input values differ more
pairwise_distances([[1,2], [1,3]], metric='correlation')
>>> array([[0.00000000e+00, 2.22044605e-16],
>>> [2.22044605e-16, 0.00000000e+00]])
用 Scipy 计算相关距离
如果 scipy 针对相同的输入返回 0.0,我不明白 sklearn 2.22044605e-16 值的来源。
# Scipy
import scipy
scipy.spatial.distance.correlation([1,2], [1,2])
>>> 0.0
# Sklearn
pairwise_distances([[1,2], [1,2]], metric='correlation')
>>> array([[0.00000000e+00, 2.22044605e-16],
>>> [2.22044605e-16, 0.00000000e+00]])
我不是在寻找高级别的解释,而是如何计算数字的示例。
【问题讨论】:
-
正如其他人所说,2.22e-16 is 0 用于所有密集目的,因为 python 中的浮点数并不比这更精确。
标签: python scikit-learn pairwise-distance