删除CountVectorizer中单个单词的出现

问题描述 投票:-1回答:1

我正在使用CountVectorizer()创建项频矩阵。我想删除词汇表中频率不超过两个的所有术语。然后我使用tfidfTransformer()创建ti * idf矩阵

vectorizer=CountVectorizer()
X =vectorizer.fit_transform(docs) 

matrix_terms = np.array(vectorizer.get_feature_names())     
matrix_freq = np.asarray(X.sum(axis=0)).ravel()

tfidf_transformer=TfidfTransformer()     
tfidf_matrix = tfidf_transformer.fit_transform(X)

然后,我想使用LSA算法进行降维,将k均值用于聚类。但是我想使聚类不包含频率为两个或更少的项。有人可以帮我吗?

python k-means dimensionality-reduction
1个回答
0
投票

您只需要保留最大值小于两个的所有列:

import numpy as np

count_vec = np.random.randint(0, 3, (5, 10))
print(count_vec)
[[1 1 2 0 2 2 2 0 0 2]
 [0 1 0 2 1 1 0 1 0 0]
 [0 1 0 1 0 1 1 2 2 2]
 [0 0 2 1 1 1 0 0 0 2]
 [1 0 0 2 2 2 1 1 2 2]]

仅保留最大值小于2的列]

count_vec = count_vec[:, count_vec.max(axis=1) >= 2]
print(count_vec)
[[2 1 2 2 1 0 1 0 1]
 [1 1 0 2 0 0 2 0 1]
 [0 0 0 2 0 1 0 1 0]
 [1 0 2 0 2 2 2 1 2]
 [1 2 2 1 1 0 2 2 1]]
© www.soinside.com 2019 - 2024. All rights reserved.