完成作业
This commit is contained in:
21
25 3.py
Normal file
21
25 3.py
Normal 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")
|
||||
Reference in New Issue
Block a user