From 0e315ac98db8b4ad90f26cf11a5cab4e6439a8a6 Mon Sep 17 00:00:00 2001 From: 2509165028 <2509165028@student.edu.cn> Date: Thu, 23 Apr 2026 16:06:54 +0800 Subject: [PATCH] 3-2-1 --- 260421_2509165028.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/260421_2509165028.py b/260421_2509165028.py index ece70c4..85c6720 100644 --- a/260421_2509165028.py +++ b/260421_2509165028.py @@ -3,6 +3,7 @@ import numpy as np 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]) @@ -10,9 +11,31 @@ 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模型的缺点:忽略词序,所有词同等重要") \ No newline at end of file