Files
task-3-2-1-Text-Processing-…/import math.ini
2026-04-21 11:27:27 +08:00

21 lines
778 B
INI
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 = sum(a * b for a, b in zip(A, B))
print("点积 A · B", dot_product)
norm_A = math.sqrt(sum(a**2 for a in A))
norm_B = math.sqrt(sum(b**2 for b in B))
cos_similarity = dot_product / (norm_A * norm_B)
print("余弦相似度:", cos_similarity)
A_special = [1, 0]
B_special = [0, 1]
dot_special = sum(a * b for a, b in zip(A_special, B_special))
norm_A_special = math.sqrt(sum(a**2 for a in A_special))
norm_B_special = math.sqrt(sum(b**2 for b in B_special))
cos_special = dot_special / (norm_A_special * norm_B_special)
print("\n特殊情况的余弦相似度", cos_special)
print("原因:两个向量相互垂直(正交),点积为 0方向完全不同因此余弦相似度为 0")