作用:使用欧几里得距离,返回的是X距离Y最近点的index,所以形状与X的形状一致。过程:挨个查找X列表中的点,返回该点距离最近Y点的index
用法:
from sklearn.metrics.pairwise import pairwise_distances_argmin #加载包
#使用欧几里得距离,返回的是X距离Y最近点的index,所以形状与X的形状一致。过程:挨个查找X列表中的点,返回该点距离最近Y点的index
d = pairwise_distances_argmin([(1,1),(0,0),(4,4)],[(2,2),(3,3),(0,0)], axis=1,metric='euclidean')
print(d)
>>
array([0, 2, 1], dtype=int64)
官方解释:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise_distances_argmin.html#sklearn.metrics.pairwise_distances_argmin
sklearn.metrics.
pairwise_distances_argmin
(X, Y, axis=1, metric=’euclidean’, batch_size=None, metric_kwargs=None)
参数:
X : array-like
Arrays containing points. Respective shapes (n_samples1, n_features) and (n_samples2, n_features)
Y : array-like
Arrays containing points. Respective shapes (n_samples1, n_features) and (n_samples2, n_features)
axis : int, optional, default 1
Axis along which the argmin and distances are to be computed.
metric : string or callable
metric to use for distance computation. Any metric from scikit-learn or scipy.spatial.distance can be used.
If metric is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two arrays as input and return one value indicating the distance between them. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.
Distance matrices are not supported.
Valid values for metric are:
See the documentation for scipy.spatial.distance for details on these metrics.
batch_size : integer
Deprecated since version 0.20: Deprecated for removal in 0.22. Use sklearn.set_config(working_memory=…) instead.
metric_kwargs : dict
keyword arguments to pass to specified metric function.
return 返回:
argmin : numpy.ndarray
Y[argmin[i], :] is the row in Y that is closest to X[i, :]