【发布时间】:2017-10-11 23:57:11
【问题描述】:
我有一个包含 3 条记录的数据框 (data):
id text
0001 The farmer plants grain
0002 The fisher catches tuna
0003 The police officer fights crime
我按 id 对该数据框进行分组:
data_grouped = data.groupby('id')
描述生成的 groupby 对象表明所有记录都保留。
然后我运行此代码以在 text 中找到 nGram,并将它们加入到 id:
word_vectorizer = CountVectorizer(stop_words=None, ngram_range=(2,2),
analyzer='word')
for id, group in data_grouped:
X = word_vectorizer.fit_transform(group['text'])
frequencies = sum(X).toarray()[0]
results = pd.DataFrame(frequencies, columns=['frequency'])
dfinner = pd.DataFrame(word_vectorizer.get_feature_names())
dfinner['id'] = id
final = results.join(dfinner)
当我一起运行所有这些代码时,word_vectorizer 会出现一个错误,指出“空词汇;也许文档只包含停用词”。我知道在许多其他问题中都提到了这个错误,但我找不到一个处理 Dataframe 的问题。
为了使问题进一步复杂化,错误并不总是出现。我正在从 SQL 数据库中提取数据,根据我提取的记录数,错误可能会出现,也可能不会出现。例如,拉入Top 10 记录会导致错误,但Top 5 不会。
编辑:
完整的回溯
Traceback (most recent call last):
File "<ipython-input-63-d261e44b8cce>", line 1, in <module>
runfile('C:/Users/taca/Documents/Work/Python/Text Analytics/owccomments.py', wdir='C:/Users/taca/Documents/Work/Python/Text Analytics')
File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/taca/Documents/Work/Python/Text Analytics/owccomments.py", line 38, in <module>
X = word_vectorizer.fit_transform(group['cleanComments'])
File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 839, in fit_transform
self.fixed_vocabulary_)
File "C:\Users\taca\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\feature_extraction\text.py", line 781, in _count_vocab
raise ValueError("empty vocabulary; perhaps the documents only"
ValueError: empty vocabulary; perhaps the documents only contain stop words
【问题讨论】:
-
你能再解释一下吗?当你说“当我一起运行所有这些代码时”是什么意思?同时发布错误的完整堆栈跟踪。
-
查看我的回溯编辑。我还尝试将
X = word_vectorizer.fit_transform(group['cleanComments'])更改为X = word_vectorizer.fit_transform(data['cleanComments']),这消除了错误但......显然它也丢失了分组,因此每个nGram 都被分配给每个id。此外,当我将print(final)添加到循环中时,输出会按照我的预期打印出来,每个id的数据帧只包含那个id的nGrams -
忽略我关于“一起运行代码”的评论,我的意思是当我执行脚本时,会发生此错误。
-
当数据框包含一行没有二元组(即只有一个单词)时,它看起来可能会挂起。有没有办法在 for 循环中放置另一个循环,上面写着
if group['text'] includes less than 1 word的内容然后忽略?我只是不确定如何用 Python 编写它。 -
或者,更好的是,如果 group['text'] 只有一个单词,是否有办法包含一元组?
标签: python pandas dataframe scikit-learn countvectorizer