【发布时间】:2017-06-08 18:41:15
【问题描述】:
我有以下dataset(获得here):
----------item survivalpoints weight
1 pocketknife 10 1
2 beans 20 5
3 potatoes 15 10
4 unions 2 1
5 sleeping bag 30 7
6 rope 10 5
7 compass 30 1
我可以使用二进制字符串作为我最初选择的中心,使用kmeans() 将此数据集聚类为三个聚类。例如:
## 1 represents the initial centers
chromosome = c(1,1,1,0,0,0,0)
## exclude first column (kmeans only support continous data)
cl <- kmeans(dataset[, -1], dataset[chromosome == 1, -1])
## check the memberships
cl$clusters
# [1] 1 3 3 1 2 1 2
使用这个基本概念,我尝试使用 GA 包进行搜索,以优化(最小化)Davies-Bouldin (DB) 索引。
library(GA) ## for ga() function
library(clusterSim) ## for index.DB() function
## defining my fitness function (Davies-Bouldin)
DBI <- function(x) {
## converting matrix to vector to access each row
binary_rep <- split(x, row(x))
## evaluate the fitness of each chromsome
for(each in 1:nrow(x){
cl <- kmeans(dataset, dataset[binary_rep[[each]] == 1, -1])
dbi <- index.DB(dataset, cl$cluster, centrotypes = "centroids")
## minimizing db
return(-dbi)
}
}
g<- ga(type = "binary", fitness = DBI, popSize = 100, nBits = nrow(dataset))
当然(我不知道发生了什么),我收到了错误消息
Warning messages:
Error in row(x) : a matrix-like object is required as argument to 'row'
这是我的问题:
- 如何正确使用
GA包解决我的问题? - 如何确保随机生成的染色体包含相同数量的
1s,对应于k的簇数(例如,如果k=3,那么染色体必须恰好包含三个1s)?
【问题讨论】:
-
我认为这种方法没有任何意义。它可能不起作用,因为 k-means 经常收敛到完全相同的解决方案。
-
有什么建议可以解决这样的问题吗?我的数据集太小了吗?
-
我认为 GA+k-means ever 没有意义。
标签: r cluster-analysis k-means genetic-algorithm