r"""Compute distance between samples in :math:`\mathbf{x_1}` and :math:`\mathbf{x_2}` .. note:: This function is backend-compatible and will work on arrays from all compatible backends for the following metrics: 'sqeuclidean', 'euclidean', 'cityblock', 'minkowski', 'cosine', 'co
(
x1,
x2=None,
metric="sqeuclidean",
p=2,
w=None,
backend="auto",
nx=None,
use_tensor=False,
)
| 297 | |
| 298 | |
| 299 | def dist( |
| 300 | x1, |
| 301 | x2=None, |
| 302 | metric="sqeuclidean", |
| 303 | p=2, |
| 304 | w=None, |
| 305 | backend="auto", |
| 306 | nx=None, |
| 307 | use_tensor=False, |
| 308 | ): |
| 309 | r"""Compute distance between samples in :math:`\mathbf{x_1}` and :math:`\mathbf{x_2}` |
| 310 | |
| 311 | .. note:: This function is backend-compatible and will work on arrays |
| 312 | from all compatible backends for the following metrics: |
| 313 | 'sqeuclidean', 'euclidean', 'cityblock', 'minkowski', 'cosine', 'correlation'. |
| 314 | |
| 315 | Parameters |
| 316 | ---------- |
| 317 | |
| 318 | x1 : array-like, shape (n1,d) |
| 319 | matrix with `n1` samples of size `d` |
| 320 | x2 : array-like, shape (n2,d), optional |
| 321 | matrix with `n2` samples of size `d` (if None then :math:`\mathbf{x_2} = \mathbf{x_1}`) |
| 322 | metric : str | callable, optional |
| 323 | 'sqeuclidean' or 'euclidean' on all backends. On numpy the function also |
| 324 | accepts from the scipy.spatial.distance.cdist function : 'braycurtis', |
| 325 | 'canberra', 'chebyshev', 'cityblock', 'correlation', 'cosine', 'dice', |
| 326 | 'euclidean', 'hamming', 'jaccard', 'kulczynski1', 'mahalanobis', |
| 327 | 'matching', 'minkowski', 'rogerstanimoto', 'russellrao', 'seuclidean', |
| 328 | 'sokalmichener', 'sokalsneath', 'sqeuclidean', 'wminkowski', 'yule'. |
| 329 | p : float, optional |
| 330 | p-norm for the Minkowski and the Weighted Minkowski metrics. Default value is 2. |
| 331 | w : array-like, rank 1 |
| 332 | Weights for the weighted metrics. |
| 333 | backend : str, optional |
| 334 | Backend to use for the computation. If 'auto', the backend is |
| 335 | automatically selected based on the input data. if 'scipy', |
| 336 | the ``scipy.spatial.distance.cdist`` function is used (and gradients are |
| 337 | detached). |
| 338 | use_tensor : bool, optional |
| 339 | If true use tensorized computation for the distance matrix which can |
| 340 | cause memory issues for large datasets. Default is False and the |
| 341 | parameter is used only for the 'cityblock' and 'minkowski' metrics. |
| 342 | nx : Backend, optional |
| 343 | Backend to perform computations on. If omitted, the backend defaults to that of `x1`. |
| 344 | |
| 345 | Returns |
| 346 | ------- |
| 347 | |
| 348 | M : array-like, shape (`n1`, `n2`) |
| 349 | distance matrix computed with given metric |
| 350 | |
| 351 | """ |
| 352 | if nx is None: |
| 353 | nx = get_backend(x1, x2) |
| 354 | if x2 is None: |
| 355 | x2 = x1 |
| 356 | if backend == "scipy": # force scipy backend with cdist function |
no test coverage detected