Cosine Similarity Between Vectors
Vectors & Geometry DS practice problem on Onlearn.
Difficulty: easy.
Topics: Understanding Calculate Cosine Similarity Between Vectors, L2 Normalization, Vector Magnitude, Scalar Product, Angular Distance, Floating Point Precision, Similarity Calculation, Linear Algebra, Numerical Computation, Geometry, Information Retrieval, Statistics, Vector Operations, Vector Norms, Dot Product, Similarity Measures, Metric Spaces.
Task: Implement Cosine Similarity In this task, you need to implement a function cosine similarity(v1, v2) that calculates the cosine similarity between two vectors. Cosine similarity measures the cosine of the angle between two vectors, indicating their directional similarity. Input: v1 and v2: Numpy arrays representing the input vectors. Output: A float representing the cosine similarity, rounded to three decimal places. Constraints: Both input vectors must have the same shape. Input vectors cannot be empty or have zero magnitude.