Distance Metric APIs - 5.2 English - 68552

AOCL API Guide (68552)

Document ID
68552
Release Date
2025-12-29
Version
5.2 English
aoclda.metrics.pairwise_distances(X, Y=None, metric='euclidean', p=2.0, force_all_finite='allow_infinite')#

Pairwise distance metrics.

Parameters:
  • X (array-like) – The feature matrix for which the distance matrix needs to be computed. Its shape is (n_samples_X, n_features).

  • Y (array-like, optional) – The optional second feature matrix for which the distance matrix needs to be computed. Its shape is (n_samples_Y, n_features).

  • metric (str, optional) – The type of metric used to compute the distance matrix. It can take the values ‘euclidean’, ‘l2’, ‘sqeuclidean’, ‘manhattan’, ‘l1’, ‘cityblock’, ‘cosine’, ‘minkowski’, ‘euclidean_gemm’, or ‘sqeuclidean_gemm’. Default: ‘euclidean’.

  • p (float, optional) – The power parameter used for the Minkowski metric. For p = 1.0, this defaults to ‘manhattan’ metric and for p = 2.0 this defaults to ‘euclidean’ metric. p is only used for Miknowski distance and will be ignored otherwise. Will return an error when p is not positive. Default p = 2.0.

Returns:

numpy.ndarray with shape (n_samples_X, n_samples_Y) if Y is provided, or shape (n_samples_X, n_samples_X), if Y is None and the distance matrix for the rows of X is required.

da_status da_pairwise_distances_s(da_order order, da_int m, da_int n, da_int k, const float *X, da_int ldx, const float *Y, da_int ldy, float *D, da_int ldd, float p, da_metric metric)#
da_status da_pairwise_distances_d(da_order order, da_int m, da_int n, da_int k, const double *X, da_int ldx, const double *Y, da_int ldy, double *D, da_int ldd, double p, da_metric metric)#

Compute the distance matrix for an m by k matrix X and optionally an n by k matrix Y.

Parameters:
  • order[in] a da_order enumerated type, specifying whether X, Y and D are stored in row-major order or column-major order.

  • m[in] the number of rows of matrix X.

  • n[in] the number of rows of matrix Y.

  • k[in] the number of columns of matrices X and Y.

  • X[in] the m \(\times \) k matrix.

  • ldx[in] the leading dimension of the matrix X. Constraint: ldx \(\ge\) m if order = column_major, or ldx \(\ge\) k if order = row_major.

  • Y[in] the n \(\times \) k matrix.

  • ldy[in] the leading dimension of the matrix Y. Constraint: ldy \(\ge\) n if order = column_major, or ldy \(\ge\) k if order = row_major.

  • D[out] if Y is nullptr, the m \(\times \) m distance matrix, and the m \(\times \) n distance matrix, otherwise.

  • ldd[in] the leading dimension of the matrix D. Constraint: ldd \(\ge\) m, if Y is nullptr or order = column_major, and ldd \(\ge\) n, otherwise.

  • p[in] the order of the Minkowski metric used to compute the distance matrix. For p = 1.0, this defaults to Manhattan metric and for p = 2.0 this defaults to Euclidean metric. p is only used for Minkowski distance and will be ignored otherwise.

  • metric[in] enum that specifies the metric to use to compute the distance matrix. The default value is da_euclidean.

Returns:

da_status. The function returns:

typedef enum da_metric_ da_metric#

Alias for the da_metric_ enum.

enum da_metric_#

Defines the metric used when calculating the pairwise distances.

Values:

enumerator da_euclidean#

Use euclidean distance.

enumerator da_l2#

Use l2 distance.

enumerator da_sqeuclidean#

Use squared euclidean distance.

enumerator da_minkowski#

Use Minkowski distance.

enumerator da_manhattan#

Use Manhattan distance.

enumerator da_l1#

Use l1 distance.

enumerator da_cityblock#

Use cityblock distance.

enumerator da_cosine#

Use Cosine distance.

enumerator da_euclidean_gemm#

Use gemm-based euclidean distance.

enumerator da_sqeuclidean_gemm#

Use gemm-based squared euclidean distance.

typedef enum da_data_types_ da_data_types#

Alias for the da_data_types_ enum.

enum da_data_types_#

Defines the type of data that is allowed as inputs when pairwise distances are computed.

Values:

enumerator da_all_finite#

Force all values to be finite.

enumerator da_allow_infinite#

Allow infinite and NaN values in input data.

enumerator da_allow_NaN#

Allow NaN values in input data.