sklearn.metrics.pairwise
.manhattan_distances¶
- sklearn.metrics.pairwise.manhattan_distances(X, Y=None, *, sum_over_features='deprecated')[source]¶
Compute the L1 distances between the vectors in X and Y.
With sum_over_features equal to False it returns the componentwise distances.
Read more in the User Guide.
- Parameters:
- Xarray-like of shape (n_samples_X, n_features)
An array where each row is a sample and each column is a feature.
- Yarray-like of shape (n_samples_Y, n_features), default=None
An array where each row is a sample and each column is a feature. If
None
, method usesY=X
.- sum_over_featuresbool, default=True
If True the function returns the pairwise distance matrix else it returns the componentwise L1 pairwise-distances. Not supported for sparse matrix inputs.
Deprecated since version 1.2:
sum_over_features
was deprecated in version 1.2 and will be removed in 1.4.
- Returns:
- Dndarray of shape (n_samples_X * n_samples_Y, n_features) or (n_samples_X, n_samples_Y)
If sum_over_features is False shape is (n_samples_X * n_samples_Y, n_features) and D contains the componentwise L1 pairwise-distances (ie. absolute difference), else shape is (n_samples_X, n_samples_Y) and D contains the pairwise L1 distances.
Notes
When X and/or Y are CSR sparse matrices and they are not already in canonical format, this function modifies them in-place to make them canonical.
Examples
>>> from sklearn.metrics.pairwise import manhattan_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.]])