聚类我有以下dataset
(获得here):优化K均值使用遗传算法
----------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
包进行那里我想优化(最小化),戴维斯 - 尔丁(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
包来解决我的问题? - 我怎样才能确保随机产生的染色体包含相同数量的
1
S的对应k
号群(例如,如果k=3
那么染色体必须包含正好是三个1
S)的?
我不认为这种方法有任何意义。它可能不会工作,因为k-means常常收敛到完全相同的解决方案。 –
有没有什么建议可以提供这样的问题?我的数据集是否太小? –
我不认为GA + k-means *永远*是有意义的。 –