【发布时间】:2020-01-26 05:23:17
【问题描述】:
如何获取sklearn CountVectorizer返回的词频矩阵中任意给定列的总和?
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
corpus = [ 'This is a sentence',
'Another sentence is here',
'Wait for another sentence',
'The sentence is coming',
'The sentence has come'
]
x = vectorizer.fit_transform(corpus)
例如我想找出矩阵中sentence 的频率。所以我想要sentence 列的总和。我想不出办法:
- 例如,我尝试了
x['sentence'].sum(),但没有帮助 - 我还尝试将其转换为 pandas 数据帧并计算总和,但我不需要将此矩阵转换为数据帧。
【问题讨论】:
标签: python python-3.x scikit-learn countvectorizer