K表示对R中的文本数据进行聚类

时间:2014-04-12 08:11:49

标签: r k-means

我有一个包含30个实例的book.csv文件。共有3个属性列:Title,Author,BookSummary。

示例如下所示:

Title, Author, BookSummary

The Da Vinci Code, Dan Brown, Louvre curator and Priory of Sion Grand Master Jacques Saunière is fatally shot one night at the museum by an albino Catholic monk named Silas, who is working on behalf of someone known only as the Teacher, who wishes to discover the location of the "keystone," an item crucial to the search for the Holy Grail.<br>

在这个示例中,我只是显示第一个实例。数据中有30行 我在R工具中对此数据集执行K-Means。我执行了以下命令: -

data<-read.csv("C:/Users/admin/Desktop/Experiment/book.csv")
corpus.tmp<-Corpus(VectorSource(data))
View(corpus)

corpus.tmp<- tm_map(corpus.tmp,removePunctuation)
corpus.tmp<- tm_map(corpus.tmp, stripWhitespace)
corpus.tmp<- tm_map(corpus.tmp, tolower)
corpus.tmp<- tm_map(corpus.tmp, removeWords, stopwords("english"))
TDM <- TermDocumentMatrix(corpus.tmp)
inspect(TDM)

tdm_tfxidf<-weightTfIdf(TDM)

m<- as.matrix(tdm_tfxidf)
rownames(m)<- 1:nrow(m)

norm_eucl<- function(m)
  m/apply(m,1,function(x) sum(x^2)^.5)

m_norm<-norm_eucl(m)

results<-kmeans(m_norm,5,5)

此代码聚集了使用DocumentTermMatrix()形成的术语数。但是,我想根据实例而不是基于术语进行聚类。

请告诉我怎么做。

1 个答案:

答案 0 :(得分:0)

我猜你的数据不是预期的格式。我想如果你在kmeans()之前调换数据,它应该没问题。

相关问题