Files
task-3-2-1-Text-Processing-…/25 3.py
2026-04-21 11:22:02 +08:00

21 lines
655 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import math
A = [1, 2, 3]
B = [4, 5, 6]
dot_product = A[0]*B[0] + A[1]*B[1] + A[2]*B[2]
print("A·B =", dot_product)
norm_A = math.sqrt(A[0]**2 + A[1]**2 + A[2]**2)
norm_B = math.sqrt(B[0]**2 + B[1]**2 + B[2]**2)
cos_sim = dot_product / (norm_A * norm_B)
print("余弦相似度 =", cos_sim)
A2 = [1, 0]
B2 = [0, 1]
dot_product2 = A2[0]*B2[0] + A2[1]*B2[1]
norm_A2 = math.sqrt(A2[0]**2 + A2[1]**2)
norm_B2 = math.sqrt(B2[0]**2 + B2[1]**2)
cos_sim2 = dot_product2 / (norm_A2 * norm_B2)
print("A=[1,0]与B=[0,1]的余弦相似度 =", cos_sim2)
print("原因两个向量正交垂直方向完全不同点积为0所以余弦相似度为0")