【发布时间】:2021-09-04 03:20:55
【问题描述】:
有没有办法从绝对频率矩阵开始获取相对频率矩阵(使用CountVectorizer方法获得)?这是使用的代码:
body = [
'the quick brown fox',
'the slow brown dog',
'the quick red dog',
'the lazy yellow fox'
]
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(stop_words='english')
bag_of_words = vectorizer.fit_transform(body)
from sklearn.decomposition import TruncatedSVD
svd = TruncatedSVD(n_components=2)
lsa = svd.fit_transform(bag_of_words)
我的目标是使用函数fit_transform()(在我的代码的最后一行)不是绝对频率矩阵,而是相对频率矩阵。特别是,我想找到一种方法将矩阵bag_of_words 的每一行除以行本身的总和。这对我来说不是即时的,因为矩阵是稀疏的。
任何意见或建议表示赞赏。谢谢。
【问题讨论】:
标签: python scikit-learn scipy countvectorizer