【问题标题】:Optimizing K-means clustering using Genetic Algorithm使用遗传算法优化 K 均值聚类
【发布时间】: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'

这是我的问题:

  1. 如何正确使用GA包解决我的问题?
  2. 如何确保随机生成的染色体包含相同数量的1s,对应于k 的簇数(例如,如果k=3,那么染色体必须恰好包含三个1s)?

【问题讨论】:

  • 我认为这种方法没有任何意义。它可能不起作用,因为 k-means 经常收敛到完全相同的解决方案。
  • 有什么建议可以解决这样的问题吗?我的数据集太小了吗?
  • 我认为 GA+k-means ever 没有意义。

标签: r cluster-analysis k-means genetic-algorithm


【解决方案1】:

我无法评论将 k-means 与 ga 结合的意义,但我可以指出您的适应度函数存在问题。此外,当所有基因都打开或关闭时会产生错误,因此仅在不是这种情况时才计算适应度:

DBI <- function(x) {
  if(sum(x)==nrow(dataset) | sum(x)==0){
    score <- 0
  } else {
    cl <- kmeans(dataset[, -1], dataset[x==1, -1])
    dbi <- index.DB(dataset[,-1], cl=cl$cluster, centrotypes = "centroids")
    score <- dbi$DB
  }

  return(score)
}

g <- ga(type = "binary", fitness = DBI, popSize = 100, nBits = nrow(dataset))
plot(g)

g@solution
g@fitnessValue

看起来几个基因组合产生了相同的“最佳”适应度值

【讨论】:

  • 我无法告诉你我对这个答案有多感激。是的,我同意基因倾向于收敛到相同的解决方案,但是知道如何应用用户定义的适应度对我来说是一个很好的开始。非常感谢!
  • 干杯 - 如果不清楚,请确保您了解 ga 将尝试最大化适应度函数,而不是最小化 - 就像在其他优化算法中所做的那样基于成本函数的。
  • 这给我带来了另一个问题,既然ga 最大化适应度函数,为什么不需要将score 与-1 相乘以最小化它?我在其他示例中看到过,尤其是使用 genalg 包,它最小化了适应度函数,因此,为了最大化它,它们将返回值乘以 -1。
  • 在某些情况下,您可能正在拟合一个函数,在该函数中,您试图最小化观察值和预测值之间的差异。为了将其翻转以反映最大的适应度,您只需将您的例如乘以残差之和 -1。在这两种情况下,您的目标都是尽可能获得最佳 score=0 差异。
  • 嗨,Marc,我已经发送了一封电子邮件(在您的博客中找到),因为我无法在评论框中上传照片,所以我向您展示了一些供讨论的图表。介意看看吗?谢谢:)
猜你喜欢
  • 2014-04-25
  • 2011-09-06
  • 2015-02-18
  • 2013-07-02
  • 1970-01-01
  • 2020-07-08
相关资源
最近更新 更多