我想使用python来集群文档。首先,我使用tf-idf得分生成文档x术语矩阵,如下所示:
tfidf_vectorizer_desc = TfidfVectorizer(min_df=1, max_df=0.9,use_idf=True, tokenizer=tokenize_and_stem)
%time tfidf_matrix_desc = tfidf_vectorizer_desc.fit_transform(descriptions) #fit the vectorizer to text
desc_feature_names = tfidf_vectorizer_desc.get_feature_names()
矩阵形状为(1510,6862)
第一份文件的每个条款的分数:
dense = tfidf_matrix_desc.todense()
print(len(dense[0].tolist()[0]))
dataset0 = dense[0].tolist()[0]
phrase_scores = [pair for pair in zip(range(0, len(dataset0)), dataset0) if pair[1] > 0]
print(len(phrase_scores))
输出:
现在,我想确定矩阵中给定数据集的0 tfidf得分的所有要素(术语)。我怎样才能做到这一点?
for col in tfidf_matrix_desc.nonzero()[1]:
print(feature_names[col], ' - ', tfidf_matrix[0, col])
答案 0 :(得分:1)
如果有人需要类似的东西,我使用的是以下内容:
# Xtr is the output sparse matrix from TfidfVectorizer
# min_tfidf is a threshold for defining the "new" 0
def remove_zero_tf_idf(Xtr, min_tfidf=0.04):
D = Xtr.toarray() # convert to dense if you want
D[D < min_tfidf] = 0
tfidf_means = np.mean(D, axis=0) # find features that are 0 in all documents
D = np.delete(D, np.where(tfidf_means == 0)[0], axis=1) # delete them from the matrix
return D