41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
import numpy as np
|
||
# 第一部分题目1
|
||
s="Hello"
|
||
print("ASCII码分别为:",[ord(o) for o in s])
|
||
print(f"ASCII码65对应的字符是:{chr(65)}")
|
||
|
||
# 第二部分题目3
|
||
A=np.array([3,4])
|
||
B=np.array([1,2])
|
||
print(f"计算A+B的结果为:{A+B}")
|
||
print(f"计算A*2的结果为:{A*2}")
|
||
length=np.linalg.norm(A)
|
||
print(f"向量A的长度为:{length}")
|
||
|
||
# 第二部分题目4
|
||
A=np.array([1,2,3])
|
||
B=np.array([4,5,6])
|
||
dot=np.dot(A,B)
|
||
print(f"A·B点积为:{dot}")
|
||
def cosine_similarity(A, B):
|
||
dot = np.dot(A, B)
|
||
norm_a = np.linalg.norm(A)
|
||
norm_b = np.linalg.norm(B)
|
||
return dot / (norm_a * norm_b)
|
||
print(f"相似度 = {cosine_similarity(A, B):.3f}")
|
||
|
||
# 第三部分题目5
|
||
from sklearn.feature_extraction.text import CountVectorizer
|
||
docs = [
|
||
"Python 是 编程 语言",
|
||
"Java 是 编程 语言",
|
||
"Python Python Python"
|
||
]
|
||
vectorizer = CountVectorizer()
|
||
bow_matrix = vectorizer.fit_transform(docs)
|
||
print("词表:", vectorizer.get_feature_names_out())
|
||
print("BoW矩阵:")
|
||
print(bow_matrix.toarray())
|
||
|
||
# 第三部分题目6
|
||
print("BoW模型的缺点:忽略词序,所有词同等重要") |