manhattan_distances#

sklearn.metrics.pairwise.manhattan_distances(X,Y=None)[source]#

Compute the L1 distances between the vectors in X and Y.

Read more in theUser Guide.

Parameters:
X{array-like, sparse matrix} of shape (n_samples_X, n_features)

An array where each row is a sample and each column is a feature.

Y{array-like, sparse matrix} of shape (n_samples_Y, n_features), default=None

An array where each row is a sample and each column is a feature.IfNone, method usesY=X.

Returns:
distancesndarray of shape (n_samples_X, n_samples_Y)

Pairwise L1 distances.

Notes

When X and/or Y are CSR sparse matrices and they are not alreadyin canonical format, this function modifies them in-place tomake them canonical.

Examples

>>>fromsklearn.metrics.pairwiseimportmanhattan_distances>>>manhattan_distances([[3]],[[3]])array([[0.]])>>>manhattan_distances([[3]],[[2]])array([[1.]])>>>manhattan_distances([[2]],[[3]])array([[1.]])>>>manhattan_distances([[1,2],[3,4]],[[1,2],[0,3]])array([[0., 2.],       [4., 4.]])
On this page

This Page