1# Example function using numpy:
2from numpy import dot
3from numpy.linalg import norm
4
5def cosine_similarity(list_1, list_2):
6 cos_sim = dot(list_1, list_2) / (norm(list_1) * norm(list_2))
7 return cos_sim
8# Note, the dot product is only defined for lists of equal length. You
9# can use your_list.extend() to add elements to the shorter list
10
11# Example usage with identical lists/vectors:
12your_list_1 = [1, 1, 1]
13your_list_2 = [1, 1, 1]
14cosine_similarity(your_list_1, your_list_2)
15--> 1.0 # 1 = maximally similar lists/vectors
16
17# Example usage with opposite lists/vectors:
18your_list_1 = [1, 1, 1]
19your_list_2 = [-1, -1, -1]
20cosine_similarity(your_list_1, your_list_2)
21--> -1.0 # -1 = maximally dissimilar lists/vectors