python - 如何将 SCIKIT 中 CountVectoriser 的权重加倍以用于 TFIDF 矩阵

标签 python scikit-learn tf-idf

我有从我拥有的文本文件生成的 tf-idf 矩阵。我想更加重视一些词汇术语。 我写了下面的代码。我怎样才能将特定词汇术语的权重加倍。我需要将计数加倍还是仅将 TFIDF 的权重乘以 2。我想增加 d 中某些术语的重要性

from sklearn.feature_extraction.text import CountVectorizer

count_vectorizer = CountVectorizer(min_df=1,stop_words="english")
term_freq_matrix = count_vectorizer.fit_transform(vectoriser.mydoclist)
# print "Vocabulary:", count_vectorizer.vocabulary_

from sklearn.feature_extraction.text import TfidfTransformer

tfidf = TfidfTransformer(norm="l2")
tfidf.fit(term_freq_matrix)

tf_idf_matrix = tfidf.transform(term_freq_matrix)
print len(count_vectorizer.get_feature_names())

最佳答案

您可以将 TFIDF 或计数加倍,它们是等价的。

在你的情况下,我会做类似的事情

position = count_vectorizer.vocabulary_['the_important_word']
tf_idf_matrix[:, position] *= 2.0

关于python - 如何将 SCIKIT 中 CountVectoriser 的权重加倍以用于 TFIDF 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620153/

相关文章:

python - Python 中反向集合运算符的实际使用

numpy - 为什么SVM中支持向量的数量没有变化?

python - TfidfVectorizer stop_words 参数不起作用?

python - 查询矩阵中的行

python - 进行PCA前后的数据维度

elasticsearch - 在Elastic Search中忽略TF-IDF

python - 在 Pandas 中实现群体内的标准化

python - 如何使用 pywinauto 在文本框中设置值?

python - web2py适合大型公共(public)网站吗?

python - R lm 与 Python sklearn linear_model