完成作业

This commit is contained in:
2509165025
2026-04-21 11:22:02 +08:00
parent 7428796d86
commit b04442bbf5
3 changed files with 39 additions and 0 deletions

21
25 3.py Normal file
View File

@@ -0,0 +1,21 @@
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")