R语言文本分析(5)

来源:互联网 发布:数据库实例是什么意思 编辑:程序博客网 时间:2024/06/05 16:56

R语言文本分析(5)

采用移除稀疏项目的方法,将稀疏项目移除,得到新的项目文档矩阵,并对裁剪过的项目进行聚类分析。
通过剪枝合并的方法可以获得几个类团。也可以采用k-means进行聚类分析。

# 移除sparse项目myTdm2 <- removeSparseTerms(myTdm, sparse = 0.95)m2 <- as.matrix(myTdm2)# Cluster termsdistMatrix <- dist(scale(m2))fit <- hclust(distMatrix, method = "ward.D2")plot(fit)# cut tree into 10 clustersrect.hclust(fit, k=10)(groups <- cutree(fit, k=10))# Clustering the tweets with the k-means algorithmm3 <- t(m2)# set a fix random seedset.seed(222)# k-means clustering of tweetsk <- 8kmeansResult <- kmeans(m3, k)# cluster centersround(kmeansResult$centers, digits = 3)# check the top 3 words in every clusterfor (i in 1:k) {  cat(paste("cluster ", i, ": ", sep = " "))  s <- sort(kmeansResult$centers[i,], decreasing = T)  cat(names(s)[1:3], "\n")  # print the tweets of every cluster  # print(rdmTweets[which(kmeansResult$cluster == i)])}
0 0