文本分析,DocumentTermMatrix在R中翻译成Python

2024-04-24 06:02:42 发布

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

我在R中有以下代码,并在Python中寻找等价的代码。我想做的是从文本中提取单词,清理它们(删除标点、小写、去除空白等),并以矩阵格式创建变量,用于预测模型。你知道吗

text<- c("amazing flight",
         "got there early",
         "great prices on flights??")
mydata_1<- data.frame(text)

library(tm)
corpus<- Corpus(DataframeSource(mydata_1))
corpus<- tm_map(corpus, content_transformer(tolower))
corpus<- tm_map(corpus, removePunctuation)
corpus<- tm_map(corpus, removeWords, stopwords("english"))
corpus<- tm_map(corpus, stripWhitespace)

dtm_1<- DocumentTermMatrix(corpus)
final_output<- as.matrix(dtm_1)

输出如下所示,“惊人”、“早期”等词现在是二进制输入变量,我可以在模型中使用:

Docs   amazing early flight flights got great prices
 1       1     0      1       0      0     0      0
 2       0     1      0       0      1     0      0
 3       0     0      0       1      0     1      1

如何在Python中实现这一点?你知道吗


Tags: 代码text模型mapcorpustmflightprices
1条回答
网友
1楼 · 发布于 2024-04-24 06:02:42

我找到了答案。Python中的DocumentTermMatrix等价物称为CountVectorizer

text= ["amazing flight","got there early","great prices on flights??"]

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

vectorizer= CountVectorizer() 
X= vectorizer.fit_transform(text)
Y= vectorizer.get_feature_names()
final_output= pd.DataFrame(X.toarray(),columns=Y)

结果如下:

       amazing  early  flight  flights  got  great  on  prices  there
0      1        0      1       0        0    0      0   0       0
1      0        1      0       0        1    0      0   0       1
2      0        0      0       1        0    1      1   1       0

相关问题 更多 >