from scipy.spatial.distance import cdist
时间: 2023-06-11 10:09:49 浏览: 78
`scipy.spatial.distance.cdist` is a function that calculates the distance between each pair of two collections of inputs. It takes in two arrays of shape `(n,m)` and `(k,m)` and returns a distance matrix of shape `(n,k)` where the `(i,j)`-th element is the distance between the `i`-th element of the first array and the `j`-th element of the second array, calculated using a specified distance metric.
For example, if we have two arrays `X` and `Y` of shape `(n,2)` and `(m,2)` respectively, we can calculate the Euclidean distance between each pair of points in `X` and `Y` as follows:
```
from scipy.spatial.distance import cdist
distances = cdist(X, Y, metric='euclidean')
```
The resulting `distances` array will have shape `(n,m)` where `distances[i,j]` is the Euclidean distance between the `i`-th point in `X` and the `j`-th point in `Y`.
阅读全文