完成作业“

;
This commit is contained in:
2509165004
2026-04-21 11:21:47 +08:00
parent 080a0b4948
commit be0a8b60c9
3 changed files with 41 additions and 0 deletions

20
2.py Normal file
View File

@@ -0,0 +1,20 @@
import numpy as np
# 定义向量
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
# 1. 计算点积A·B
dot_product = np.dot(A, B)
print("A·B =", dot_product)
# 2. 计算余弦相似度
cos_sim = np.dot(A, B) / (np.linalg.norm(A) * np.linalg.norm(B))
print("余弦相似度 =", cos_sim)
# 3. A = [1,0], B = [0,1]的余弦相似度
A_new = np.array([1, 0])
B_new = np.array([0, 1])
cos_sim_new = np.dot(A_new, B_new) / (np.linalg.norm(A_new) * np.linalg.norm(B_new))
print("A=[1,0], B=[0,1]的余弦相似度 =", cos_sim_new)
print("原因两个向量正交点积为0方向完全垂直所以余弦相似度为0")