上传文件至 /
This commit is contained in:
43
ljp.py
Normal file
43
ljp.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
# 1. 输出 "Hello" 每个字符的 ASCII 码
|
||||||
|
text = "Hello"
|
||||||
|
print([ord(c) for c in text])
|
||||||
|
|
||||||
|
# 2. chr() 验证 65 = A
|
||||||
|
print(chr(65))
|
||||||
|
|
||||||
|
# 3. 向量运算
|
||||||
|
A = (3, 4)
|
||||||
|
B = (1, 2)
|
||||||
|
print("A+B =", (A[0]+B[0], A[1]+B[1]))
|
||||||
|
print("2*A =", (2*A[0], 2*A[1]))
|
||||||
|
print("A的模长 =", (A[0]**2 + A[1]**2)**0.5)
|
||||||
|
|
||||||
|
# 4. 点积与余弦相似度
|
||||||
|
import math
|
||||||
|
A = [1,2,3]
|
||||||
|
B = [4,5,6]
|
||||||
|
dot = sum(a*b for a,b in zip(A,B))
|
||||||
|
print("点积 =", dot)
|
||||||
|
|
||||||
|
normA = math.sqrt(sum(x**2 for x in A))
|
||||||
|
normB = math.sqrt(sum(x**2 for x in B))
|
||||||
|
cos_sim = dot / (normA * normB)
|
||||||
|
print("余弦相似度 =", round(cos_sim, 4))
|
||||||
|
|
||||||
|
# 垂直向量余弦相似度
|
||||||
|
A2, B2 = [1,0], [0,1]
|
||||||
|
dot2 = sum(a*b for a,b in zip(A2,B2))
|
||||||
|
normA2 = math.sqrt(sum(x**2 for x in A2))
|
||||||
|
normB2 = math.sqrt(sum(x**2 for x in B2))
|
||||||
|
cos_sim2 = dot2 / (normA2 * normB2)
|
||||||
|
print("垂直向量余弦相似度 =", cos_sim2)
|
||||||
|
|
||||||
|
# 5. 词袋模型向量
|
||||||
|
vocab = ['java','python','编程','语言']
|
||||||
|
doc1 = [0,1,1,1]
|
||||||
|
doc2 = [1,0,1,1]
|
||||||
|
doc3 = [0,3,0,0]
|
||||||
|
print("词表:", vocab)
|
||||||
|
print("Doc1:", doc1)
|
||||||
|
print("Doc2:", doc2)
|
||||||
|
print("Doc3:", doc3)
|
||||||
Reference in New Issue
Block a user