基于Pandas数据框架的文档语料库词数矩阵

2024-04-16 16:51:39 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有一个2000多个文本文档的语料库,我正试图用最优雅的方式用pandas数据帧制作一个矩阵。矩阵如下所示:

df=pd.DataFrame(index=['Doc1_name','Doc2_name','Doc3_name','...','Doc2000_name']
                , columns=['word1','word2','word3','...','word50956'])
df.iloc[:,:] = 'count_word'
print(df)

我已经在一个名为“文本”的列表中找到了所有的全文文档 我不知道我的问题是否足够清楚。在


Tags: 数据namedataframepandasdfindex方式doc1
2条回答

对于任何不小的文本语料库,我强烈建议使用scikit-learnCountVectorizer。在

简单到:

from sklearn.feature_extraction.text import CountVectorizer

count_vectorizer = CountVectorizer()
word_counts = count_vectorizer.fit_transform(corpus) # list of documents (as strings)

它并没有为您提供所需结构中的dataframe,但是使用count_vectorizervocabulary_属性来构造它,该属性包含了该项到结果矩阵中其索引的映射。在

使用sklearn的CountVectorizer

from sklearn.feature_extraction.text import CountVectorizer


df = pd.DataFrame({'texts': ["This is one text (the first one)",
                             "This is the second text",
                             "And, finally, a third text"
                            ]})

cv = CountVectorizer()
cv.fit(df['texts'])

results = cv.transform(df['texts'])

print(results.shape) # Sparse matrix, (3, 9)

如果语料库足够小,可以放入您的内存(2000+足够小),您可以将稀疏矩阵转换为pandas数据帧,如下所示:

^{pr2}$

df_res是您想要的结果:

df_res
index and   finally first   is  one second  text    the third   this
0     0     0       1       1   2   0       1       1   0       1
1     0     0       0       1   0   1       1       1   0       1
2     1     1       0       0   0   0       1       0   1       0

如果您得到一个MemoryError,您可以减少单词的词汇表,以考虑使用CountVectorizer的不同参数:

  1. 将参数stop_words='english'设置为忽略英文非字词(如the和`and)
  2. 使用min_df和{},这使得CountVectorizer根据文档频率忽略一些单词(太频繁或很少出现的单词,这可能是无用的)
  3. 使用max_features,只使用最常见的n单词。在

相关问题 更多 >