【问题标题】:Cannot obtain Word Associations,Cluster dendograms and K-means clustering in R无法在 R 中获得单词关联、聚类树状图和 K-means 聚类
【发布时间】:2016-03-27 15:16:10
【问题描述】:

我正在处理“.txt”格式的数据。我正在尝试使用 R 中的“tm”库执行文本挖掘。我的问题是,无论数据足够大还是足够小,我总是得到一个稀疏度为 0% 的文档术语矩阵。我无法获得任何类型的单词关联,也无法获得可见的聚类树状图。尝试获取 K 均值聚类图来分析我的数据时,我收到一条错误消息。这是我使用的代码:

cname = file.path("F:","texts") #folder containing text data files
dir(cname)
library(tm)   
docs <- Corpus(DirSource(cname))   
## Preprocessing      
docs <- tm_map(docs, removePunctuation)   # *Removing punctuation:*    
docs <- tm_map(docs, removeNumbers)      # *Removing numbers:*    
docs <- tm_map(docs, tolower)   # *Converting to lowercase:*    
docs <- tm_map(docs, removeWords, stopwords("english"))   #Remove stopwords
library(SnowballC)   
docs <- tm_map(docs, stemDocument)   # *Removing common word endings* 
docs <- tm_map(docs, stripWhitespace)   # *Stripping whitespace   
docs <- tm_map(docs, PlainTextDocument)
### Staging the Data      
dtm <- DocumentTermMatrix(docs)   
tdm <- TermDocumentMatrix(dtm)
tdm     
freq <- colSums(as.matrix(dtm))   
#  removing sparse terms:   
dtms <- removeSparseTerms(dtm, 0.1)
# Word Frequency   
freq <- colSums(as.matrix(dtms))   
### Term Correlations
findAssocs(dtm, c("young","politics"), corlimit=0.8) 
### Hierarchal Clustering   
dtms <- removeSparseTerms(dtm, 0.15) 
library(cluster)   
d <- dist(t(dtms), method="euclidian")  
fit <- hclust(d=d, method="ward")   
plot.new()
plot(fit, hang=-1)
groups <- cutree(fit, k=5)   # "k=" defines the number of clusters used   
rect.hclust(fit, k=5, border="red") 
### K-means clustering   
library(fpc)   
library(cluster)  
dtms <- removeSparseTerms(dtm, 0.15) 
d <- dist(t(dtms), method="euclidian")   
kfit <- kmeans(d, 2)   
clusplot(as.matrix(d), kfit$cluster, color=T, shade=T, labels=2, lines=0)      

这是我检查术语文档矩阵时得到的输出:

<<DocumentTermMatrix (documents: 1, terms: 1850)>>
Non-/sparse entries: 1850/0
Sparsity           : 0%
Maximal term length: 23
Weighting          : term frequency (tf)

尝试获取 K 均值聚类图时出错:

“plot.window(...) 中的错误:需要有限的 'xlim' 值此外: 警告信息:In sqrt(detA * pmax(0, yl2 - y^2)) : NaNs 产生"

任何单词的相关输出始终为 0。 Cluster Dendogram plot unable to make sense

$young
numeric(0)

$politics
numeric(0)

我还附上了聚类树状图。

【问题讨论】:

  • 拥有reproducible example 会更容易?
  • 好的,我会更新代码
  • 这段代码可以接受还是我应该让它更短?这是我正在做的一个项目,因此非常感谢任何帮助。谢谢。
  • 好吧,我想通了。这是因为所有这些功能都需要语料库中的多个文档才能正常工作。我在语料库中使用了一个文件。感谢上帝!干杯!
  • 不错!你可以回答你自己的问题和/或关闭它。

标签: r algorithm plot


【解决方案1】:

只有当语料库包含多个文件时,相关性和其他功能才会起作用。我的语料库只有一个文件,所以他们没有产生输出。还是谢谢!

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 2015-02-15
    • 2016-05-29
    • 2013-02-07
    • 2015-04-11
    • 2019-03-16
    • 2018-02-26
    • 2018-11-11
    • 2020-08-27
    相关资源
    最近更新 更多