基于PSO和K-means的文本文档聚类在R中

时间:2017-07-11 11:15:29

标签: r optimization k-means particle-swarm

我是粒子群优化的新手。我阅读了关于基于PSO和K-means的聚类的研究论文,但我没有找到任何相同的工作实例。任何形式的帮助都非常感谢。提前致谢!

我想在R中使用PSO和K-means执行文本文档聚类。我有一个基本的想法,即第一个PSO会给我优化的聚类质心值,然后我必须使用那些优化的聚类质心值PSO作为k-means的初始集群质心来获取文档集群。

以下代码描述了我到目前为止所做的工作!

#Import library
library(pdist)
library(hydroPSO)

#Create matrix and suppose it is our document term matrix which we get after
the cleaning of corpus

(在我的实际数据中,我有20个文件,951个术语,即 dim(dtm)= 20 * 951

matri <- matrix(data = seq(1, 20, 1), nrow = 4, ncol = 7, byrow = TRUE)  
matri
      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    1    2    3    4    5    6    7
[2,]    8    9   10   11   12   13   14
[3,]   15   16   17   18   19   20    1
[4,]    2    3    4    5    6    7    8

#Initially select first and second row as centroids
cj <- matri[1:2,]

#Calculate Euclidean Distance of each data point from centroids
vm <- as.data.frame(t(as.matrix(pdist(matri, cj))))
vm
     V1       V2       V3        V4
1  0.00000  18.52026 34.81379  2.645751
2 18.52026  0.00000  21.51744 15.874508

#Create binary matrix S in which 1 means Instance Ii is allocated to the cluster Cj otherwise 0.  
S <- matrix(data = NA, nrow = nrow(vm), ncol = ncol(vm))

for(i in 1:nrow(vm)){
  for(j in 1:ncol(vm)){
       cd <- which.min(vm[, j])
       ifelse(cd==i,  S[i,j] <-1, S[i,j] <-0)

     }
  }

S
      [,1] [,2] [,3] [,4]
[1,]    1    0    0    1
[2,]    0    1    1    0

#Apply `hydroPSO()` to get optimised values of centroids.
set.seed(5486)
D <- 4 # Dimension
lower <- rep(0, D)
upper <- rep(10, D)
m_s <- matrix(data = NA, nrow = nrow(S), ncol = ncol(matri))
Fn= function(y) {  #Objective Function which has to be minimised

for(j in 1:ncol(matri)){
    for(i in 1:nrow(matri)){
        for(k in 1:nrow(y)){
            for(l in 1:ncol(y)){
                m_s[k,] <- colSums(matri[y[k,]==1,])/sum(y[k,])
            }
        }
     }
}

  sm <- sum(m_s)/ nrow(S)
  return(sm)

  }

hh1 <- hydroPSO(S,fn=Fn, lower=lower, upper=upper,
                control=list(write2disk=FALSE, npart=3))

但上述hydroPSO()功能无效。它在1:nrow(y):长度为0的参数中给出错误错误。我搜索了它,但没有得到任何适合我的解决方案。

我也对我的目标函数进行了一些更改,这次hydroPSO()工作但我觉得不正确。我将初始质心矩阵作为参数传递,其尺寸为2 * 7,但该函数仅返回1 * 7优化值。我没理由。

set.seed(5486)
D <- 7# Dimension
lower <- rep(0, D)
upper <- rep(10, D)

Fn = function(x){
vm <- as.data.frame(t(as.matrix(pdist(matri, x))))

S <- matrix(data = NA, nrow = nrow(vm), ncol = ncol(vm))

for(i in 1:nrow(vm)){
  for(j in 1:ncol(vm)){
       cd <- which.min(vm[, j])
       ifelse(cd==i,  S[i,j] <-1, S[i,j] <-0)

     }
  }

  m_s <- matrix(data = NA, nrow = nrow(S), ncol = ncol(matri))

 for(j in 1:ncol(matri)){
    for(i in 1:nrow(matri)){
        for(k in 1:nrow(S)){
            for(l in 1:ncol(S)){
                m_s[k,] <- colSums(matri[S[k,]==1,])/sum(S[k,])
            }
        }
    }
  }

sm <- sum(m_s)/ nrow(S)
return(sm)

}
hh1 <- hydroPSO(cj,fn=Fn, lower=lower, upper=upper,
                  control=list(write2disk=FALSE, npart=2, K=2))  

输出上述功能。

## $par
## Param1    Param2    Param3    Param4    Param5    Param6  Param7 
## 8.6996174 2.1952303 5.6903588 0.4471795 3.7103161 1.6605425 8.2717574 
## 
## $value
## [1] 61.5
## 
## $best.particle
## [1] 1
## 
## $counts
## function.calls     iterations    regroupings 
##           2000           1000              0 
## 
## $convergence
## [1] 3
## 
## $message
## [1] "Maximum number of iterations reached"

我想我是以错误的方式将参数传递给hydroPSO()。请纠正我在哪里做错了。

非常感谢!

1 个答案:

答案 0 :(得分:0)

我没有将 cj 传递给hydroPSO(),而是在我的第二种方法中使用了 as.vector(t(cj)),而且它对我来说很好。我得到了14个优化值