3-1-2 衔接与拓展:从图像矩阵到文本向量

This commit is contained in:
2509165015
2026-04-16 15:58:15 +08:00
parent 90080fd4a7
commit 1379b2cffb
4 changed files with 61 additions and 0 deletions

17
0416+2509165015/4.py Normal file
View File

@@ -0,0 +1,17 @@
import numpy as np
corpus = [
"我 喜欢 编程",
"我 喜欢 学习 Python",
"编程 是 有趣 的"
]
vocab = sorted(list(set(" ".join(corpus).split())))
print("词汇表:", vocab)
def text_to_vector(text, vocab):
words = text.split()
vector = np.zeros(len(vocab), dtype=int)
for i, word in enumerate(vocab):
vector[i] = words.count(word)
return vector
vectors = np.array([text_to_vector(text, vocab) for text in corpus])
print("\n文本向量化结果:")
print(vectors)