# jina.math.distance module¶

jina.math.distance.pdist(x_mat, metric, is_sparse=False)[source]

Computes Pairwise distances between observations in n-dimensional space.

Parameters
• x_mat (`Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`]) – Union[‘np.ndarray’,’scipy.sparse.csr_matrix’, ‘scipy.sparse.coo_matrix’] of ndim 2

• metric (`str`) – string describing the metric type

• is_sparse (`bool`) – boolean stating if computation must be done with sparse data

Return type

np.ndarray

Returns

np.ndarray of ndim 2

jina.math.distance.cdist(x_mat, y_mat, metric, is_sparse=False)[source]

Computes the pairwise distance between each row of X and each row on Y according to metric. - Let n_x = x_mat.shape[0] - Let n_y = y_mat.shape[0] - Returns a matrix dist of shape (n_x, n_y) with dist[i,j] = metric(x_mat[i], y_mat[j]). :type x_mat: `Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`] :param x_mat: numpy or scipy array of ndim 2 :type y_mat: `Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`] :param y_mat: numpy or scipy array of ndim 2 :type metric: `str` :param metric: string describing the metric type :type is_sparse: `bool` :param is_sparse: boolean describing if data type is sparse :rtype: np.ndarray :return: np.ndarray of ndim 2

jina.math.distance.cosine(x_mat, y_mat, eps=1e-07)[source]

Cosine distance between each row in x_mat and each row in y_mat. :type x_mat: `ndarray` :param x_mat: np.ndarray with ndim=2 :type y_mat: `ndarray` :param y_mat: np.ndarray with ndim=2 :type eps: `float` :param eps: a small jitter to avoid divde by zero :rtype: `ndarray` :return: np.ndarray with ndim=2

jina.math.distance.sqeuclidean(x_mat, y_mat)[source]

squared Euclidean distance between each row in x_mat and each row in y_mat. :type x_mat: `ndarray` :param x_mat: np.ndarray with ndim=2 :type y_mat: `ndarray` :param y_mat: np.ndarray with ndim=2 :rtype: `ndarray` :return: np.ndarray with ndim=2

jina.math.distance.sparse_cosine(x_mat, y_mat)[source]

Cosine distance between each row in x_mat and each row in y_mat. :type x_mat: `Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`] :param x_mat: scipy.sparse like array with ndim=2 :type y_mat: `Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`] :param y_mat: scipy.sparse like array with ndim=2 :rtype: np.ndarray :return: np.ndarray with ndim=2

jina.math.distance.sparse_sqeuclidean(x_mat, y_mat)[source]

Cosine distance between each row in x_mat and each row in y_mat. :type x_mat: `Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`] :param x_mat: scipy.sparse like array with ndim=2 :type y_mat: `Union`[`ForwardRef`, `ForwardRef`, `ForwardRef`, `ForwardRef`] :param y_mat: scipy.sparse like array with ndim=2 :rtype: np.ndarray :return: np.ndarray with ndim=2