🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

spatial.distance.cosine()

AI & DATA SCIENCE // spatial-distance-cosine

scipy.spatial.distance.cosine() computes the cosine distance between two vectors — a measure of how different their directions are, regardless of their magnitude.

Syntax

scipy.spatial.distance.cosine(u, v)

Deep Dive Course

Cosine distance is defined as 1 minus the cosine similarity, the cosine of the angle between two vectors, so two vectors pointing in exactly the same direction have a cosine distance of 0, regardless of how long each vector actually is, while two perpendicular vectors have a cosine distance of 1. This makes it especially popular for comparing text documents represented as word-frequency vectors, or other high-dimensional data, where the overall magnitude of a vector, like a document's total length, shouldn't affect how similar two things are considered to be, only their relative proportions/direction.

1Understanding spatial.distance.cosine()

Cosine distance is defined as 1 minus the cosine similarity, the cosine of the angle between two vectors, so two vectors pointing in exactly the same direction have a cosine distance of 0, regardless of how long each vector actually is, while two perpendicular vectors have a cosine distance of 1. This makes it especially popular for comparing text documents represented as word-frequency vectors, or other high-dimensional data, where the overall magnitude of a vector, like a document's total length, shouldn't affect how similar two things are considered to be, only their relative proportions/direction.

💡

Use cosine distance instead of Euclidean distance specifically when the relative proportions/direction of a vector matter more than its absolute magnitude — like comparing two documents' word-frequency vectors, where a long document and a short document covering the same topics in the same proportions should be considered similar, not distant.

editor.html
from scipy.spatial import distance

v1 = (1, 0)
v2 = (0, 1)
print(distance.cosine(v1, v2))
localhost:3000

2Practical Example

Here is a real-world application of spatial.distance.cosine() showing how it is used in production SciPy code.

editor.html
from scipy.spatial import distance

v1 = (1, 2, 3)
v2 = (2, 4, 6)
print(distance.cosine(v1, v2))
localhost:3000

3Best Practices

Follow these guidelines when working with spatial.distance.cosine():

1. Use cosine distance for text/document similarity and other high-dimensional data where the direction, not the magnitude, of a vector is what actually matters

2. Use Euclidean distance instead when the actual magnitude/scale of the data is meaningful and should factor into the notion of similarity

3. Remember cosine distance ranges from 0, identical direction, to 2, exactly opposite direction, unlike Euclidean distance, which has no fixed upper bound

⚠️

Tip: Use cosine distance instead of Euclidean distance specifically when the relative proportions/direction of a vector matter more than its absolute magnitude — like comparing two documents' word-frequency vectors, where a long document and a short document covering the same topics in the same proportions should be considered similar, not distant.

editor.html
from scipy.spatial import distance

v1 = (1, 0)
v2 = (0, 1)
print(distance.cosine(v1, v2))
localhost:3000

Examples

Example 01Basic Usage
from scipy.spatial import distance

v1 = (1, 0)
v2 = (0, 1)
print(distance.cosine(v1, v2))
Example 02Advanced Example
from scipy.spatial import distance

v1 = (1, 2, 3)
v2 = (2, 4, 6)
print(distance.cosine(v1, v2))

Best Practices

  • Use cosine distance for text/document similarity and other high-dimensional data where the direction, not the magnitude, of a vector is what actually matters
  • Use Euclidean distance instead when the actual magnitude/scale of the data is meaningful and should factor into the notion of similarity
  • Remember cosine distance ranges from 0, identical direction, to 2, exactly opposite direction, unlike Euclidean distance, which has no fixed upper bound

Interview Question

Why is the cosine distance between (1, 2, 3) and (2, 4, 6) exactly 0, even though these are clearly two different vectors?

Hint: Think about the relationship between these two specific vectors, geometrically.

The vector (2, 4, 6) is exactly (1, 2, 3) scaled by a factor of 2, every component is doubled, which means both vectors point in the exact same direction, just with different lengths. Since cosine distance measures only the angle between two vectors' directions and completely ignores their magnitude, any two vectors that are scalar multiples of each other, pointing the same way regardless of length, always have a cosine distance of exactly 0, correctly reflecting that they represent the same direction even though they're not literally identical vectors.

Exercises

MediumPractice using spatial.distance.cosine() in a real scenario.
View Solution
from scipy.spatial import distance

v1 = (1, 0)
v2 = (0, 1)
print(distance.cosine(v1, v2))

Frequently Asked Questions

Why is the cosine distance between (1, 2, 3) and (2, 4, 6) exactly 0, even though these are clearly two different vectors?

The vector (2, 4, 6) is exactly (1, 2, 3) scaled by a factor of 2, every component is doubled, which means both vectors point in the exact same direction, just with different lengths. Since cosine distance measures only the angle between two vectors' directions and completely ignores their magnitude, any two vectors that are scalar multiples of each other, pointing the same way regardless of length, always have a cosine distance of exactly 0, correctly reflecting that they represent the same direction even though they're not literally identical vectors.

Related Functions

spatial-distance-euclideannp-dotseries-str-contains