2014-04-12 30 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()之前轉換數據,它應該沒問題。