diff --git a/1.py b/1.py new file mode 100644 index 0000000..8eb2ebf --- /dev/null +++ b/1.py @@ -0,0 +1,17 @@ +import numpy as np + +# 定义向量 +A = np.array([3, 4]) +B = np.array([1, 2]) + +# 1. 计算A + B +add_result = A + B +print("A + B =", add_result) + +# 2. 计算2 × A +mul_result = 2 * A +print("2 × A =", mul_result) + +# 3. 计算A的长度(模) +norm_A = np.linalg.norm(A) +print("A的长度(模) =", norm_A) \ No newline at end of file diff --git a/2.py b/2.py new file mode 100644 index 0000000..b60072c --- /dev/null +++ b/2.py @@ -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") \ No newline at end of file diff --git a/260421-2509165004.py b/260421-2509165004.py new file mode 100644 index 0000000..961f22d --- /dev/null +++ b/260421-2509165004.py @@ -0,0 +1,4 @@ +s = "Hello" +for c in s: + print(c, ord(c)) + print(chr(65)) # 输出 A