Files
task-3-2-1-Text-Processing-…/2.py
2509165004 be0a8b60c9 完成作业“
;
2026-04-21 11:21:47 +08:00

20 lines
631 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 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")