聚類我有以下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 *永遠*是有意義的。 –