无法在python(jupyter)中创建术语文档矩阵

时间:2018-11-03 08:39:01

标签: python text-mining

虽然在jupyter中运行以下代码来创建术语文档矩阵,但我收到一条错误消息,说名称错误:未定义名称'textmining'。

代码如下:

#create term document matrix
tdm = textmining.TermDocumentMatrix(post_corpus)

for i in post_corpus:
    #print(i)
    tdm.add_doc(i)

NameError: name 'textmining' is not defined

我通过运行以下代码检查是否安装了文本挖掘功能:

!pip install textmining

运行后,输出为:

已经满足要求:在c:\ users \ asus \ anaconda3 \ lib \ site-packages(1.0)中进行文本挖掘

已满足要求:源自c:\ users \ asus \ anaconda3 \ lib \ site-packages(1.0.1)

我应该如何解决在创建术语文档矩阵期间发生的名称错误?有没有其他创建此术语文档矩阵的方法?

1 个答案:

答案 0 :(得分:1)

您导入了吗?

import textmining

tdm = textmining.TermDocumentMatrix()
for post in post_corpus:
    tdm.add_doc(post)

更新:2018年8月11日 我们可以使用scikit-learn获得相同的结果

要求:

pip install -U numpy scipy scikit-learn pandas 

安装后:

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer

corpus =['John and Bob are brothers.'
    ,'John went to the store. The store was closed.'
    ,'Bob went to the store too.',]


vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)

df= pd.DataFrame(X.toarray(), columns=vectorizer.get_feature_names())

结果: enter image description here