2014-04-12 28 views
-1

我有一个包含30个实例的book.csv文件。有3个属性列:标题,作者,BookSummary。K意味着在R中的文本数据上进行聚类

样本如下所示:

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> 

在此示例中,我只是表示第一instance.There是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()形成的项数。但是,我想根据实例进行聚类,而不是根据术语进行聚类。

请告诉我如何做到这一点。

+0

如何合并属于特定实例的术语。这样我可以群集实例? – r4sn4

回答

0

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