mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-08-16 11:14:44 +08:00
added similarity functions and ran precommit (#633)
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def cosine_similarity(a: List[float], b: List[float]):
|
||||||
|
"""Compute cosine similarity (dot product of two vectors divided by the product of their norms.)"""
|
||||||
|
norm_a = np.linalg.norm(a)
|
||||||
|
norm_b = np.linalg.norm(b)
|
||||||
|
if norm_a == 0 or norm_b == 0:
|
||||||
|
raise ZeroDivisionError("One of the vectors has a norm of zero.")
|
||||||
|
return np.dot(a, b) / (norm_a * norm_b)
|
||||||
|
|
||||||
|
|
||||||
|
def euclidean_distance(a: List[float], b: List[float]):
|
||||||
|
"""Compute euclidean distance (norm of the difference of two vectors.)"""
|
||||||
|
return np.linalg.norm(np.subtract(a, b))
|
||||||
Reference in New Issue
Block a user