maxframe.learn.metrics.pairwise.euclidean_distances#

maxframe.learn.metrics.pairwise.euclidean_distances(X, Y=None, Y_norm_squared=None, squared=False, X_norm_squared=None)[源代码]#

将 X 的行(以及 Y=X)视为向量,计算每对向量之间的距离矩阵。

出于效率考虑,行向量 x 和 y 之间的欧几里得距离计算如下:

dist(x, y) = sqrt(dot(x, x) - 2 * dot(x, y) + dot(y, y))

这种公式与其他计算距离的方法相比有两个优势。首先,在处理稀疏数据时计算效率更高。其次,如果一个参数变化而另一个保持不变,则可以预先计算 dot(x, x) 和/或 dot(y, y)

然而,这不是最精确的计算方法,该函数返回的距离矩阵可能不完全对称,而这在例如 scipy.spatial.distance 函数中是必需的。

更多信息请参阅 用户指南

参数:
  • X ({array-like, sparse matrix}, shape (n_samples_1, n_features))

  • Y ({array-like, sparse matrix}, shape (n_samples_2, n_features))

  • Y_norm_squared (array-like, shape (n_samples_2, ), optional) -- Y 中向量的预先计算点积(例如 (Y**2).sum(axis=1)),在某些情况下可能会被忽略,详见下方说明。

  • squared (boolean, optional) -- 返回平方欧几里得距离。

  • X_norm_squared (array-like, shape = [n_samples_1], optional) -- X 中向量的预先计算点积(例如 (X**2).sum(axis=1)),在某些情况下可能会被忽略,详见下方说明。

备注

为获得更高的精度,如果 X_norm_squaredY_norm_squaredfloat32 传递,则可能不会使用它们。

返回:

距离

返回类型:

tensor, shape (n_samples_1, n_samples_2)

示例

>>> from maxframe.learn.metrics.pairwise import euclidean_distances
>>> X = [[0, 1], [1, 1]]
>>> # distance between rows of X
>>> euclidean_distances(X, X).execute()
array([[0., 1.],
       [1., 0.]])
>>> # get distance to origin
>>> euclidean_distances(X, [[0, 0]]).execute()
array([[1.        ],
       [1.41421356]])

参见

paired_distances

X 和 Y 元素对之间的距离。