0
0
SciPydata~5 mins

Distance computation (distance.cdist) in SciPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the function scipy.spatial.distance.cdist compute?

cdist computes the distance between each pair of points from two collections of points. It returns a matrix where each element is the distance between a point in the first set and a point in the second set.

Click to reveal answer
beginner
What are the inputs required by distance.cdist?

Two arrays of points (2D arrays), where each row is a point and each column is a dimension. Optionally, a metric string to specify the type of distance (like 'euclidean', 'cityblock').

Click to reveal answer
beginner
Name two common distance metrics you can use with cdist.

Euclidean distance (straight line) and Manhattan distance (sum of absolute differences).

Click to reveal answer
intermediate
How does cdist differ from computing distances in a loop?

cdist is optimized and vectorized, so it is much faster and simpler than manually looping over points to compute distances.

Click to reveal answer
intermediate
What shape does the output of cdist have if the first input has shape (m, n) and the second input has shape (p, n)?

The output shape is (m, p), where each element [i, j] is the distance between the i-th point in the first set and the j-th point in the second set.

Click to reveal answer
What does scipy.spatial.distance.cdist return?
AA single distance value between two points
BA matrix of distances between points in two sets
CA list of points sorted by distance
DA boolean matrix indicating if points are equal
Which of these is NOT a valid metric for cdist?
Aeuclidean
Bcityblock
Crandom
Dcosine
If you have 5 points in set A and 3 points in set B, what will be the shape of the output from cdist(A, B)?
A(1, 15)
B(3, 5)
C(8, 1)
D(5, 3)
Why is cdist preferred over manual loops for distance calculation?
AIt is faster and simpler to use
BIt only works for 1D points
CIt returns distances as strings
DIt uses less memory
Which Python library provides the cdist function?
Ascipy
Bpandas
Cnumpy
Dmatplotlib
Explain how you would use scipy.spatial.distance.cdist to find distances between two sets of points.
Think about inputs, metric, and output shape.
You got /4 concepts.
    Describe the difference between Euclidean and Manhattan distance metrics used in cdist.
    Imagine walking in a city grid vs flying straight.
    You got /4 concepts.