完成作业“
;
This commit is contained in:
17
1.py
Normal file
17
1.py
Normal file
@@ -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)
|
||||||
20
2.py
Normal file
20
2.py
Normal 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")
|
||||||
4
260421-2509165004.py
Normal file
4
260421-2509165004.py
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
s = "Hello"
|
||||||
|
for c in s:
|
||||||
|
print(c, ord(c))
|
||||||
|
print(chr(65)) # 输出 A
|
||||||
Reference in New Issue
Block a user