上传文件至 /

This commit is contained in:
2026-04-23 15:52:49 +08:00
parent 7440261218
commit 1ebe58a7bf
2 changed files with 245 additions and 59 deletions

72
ljh.py
View File

@@ -1,67 +1,21 @@
print("题目1 ")
hello1 = "Hello"
hello2 = 'Hello'
print("双引号表示:", hello1)
print("单引号表示:", hello2)
from sklearn.feature_extraction.text import CountVectorizer
print("\n1. 'Hello' 每个字符的ASCII码:")
for c in hello1:
print(f"字符 '{c}' → ASCII: {ord(c)}")
docs = [
"Python 是 编程 语言",
"Java 是 编程 语言",
"Python Python Python"
]
print("\n2. 验证ASCII码65对应的字符:")
print(f"chr(65) → {chr(65)}")
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(docs)
import math
print("词表Vocabulary")
print(vectorizer.get_feature_names_out())
print("\n题目3")
A = [3, 4]
B = [1, 2]
A_plus_B = [A[0] + B[0], A[1] + B[1]]
print(f"1. A + B = {A_plus_B}")
two_times_A = [2 * A[0], 2 * A[1]]
print(f"2. 2 × A = {two_times_A}")
len_A = math.sqrt(A[0] ** 2 + A[1] ** 2)
print(f"3. 向量A的长度 = {len_A}")
print("\n题目4")
A3 = [1, 2, 3]
B3 = [4, 5, 6]
dot_product = A3[0]*B3[0] + A3[1]*B3[1] + A3[2]*B3[2]
print(f"1. 点积A·B = {dot_product}")
def cosine_similarity(a, b):
dot = sum(x*y for x,y in zip(a,b))
norm_a = math.sqrt(sum(x*x for x in a))
norm_b = math.sqrt(sum(x*x for x in b))
return dot / (norm_a * norm_b)
cos_sim = cosine_similarity(A3, B3)
print(f"2. 余弦相似度 = {cos_sim:.4f}")
A_test = [1, 0]
B_test = [0, 1]
cos_sim_test = cosine_similarity(A_test, B_test)
print(f"3. A=[1,0], B=[0,1]的余弦相似度 = {cos_sim_test}")
print("原因两个向量正交垂直方向完全不同所以余弦相似度为0")
print("\n每个文档的BoW向量")
for i, doc_vec in enumerate(X.toarray()):
print(f"Doc{i+1}: {doc_vec}")