【问题标题】:Kmeans clustering in R combined with data.table packageR中的Kmeans聚类结合data.table包
【发布时间】:2021-07-18 15:11:49
【问题描述】:

我有一个希望在其上进行 kmeans 聚类的数据集。

基本上有很多 ReEDSregion,在每个 ReEDSregion 内有很多类(ReEDSregion 1,class 1;ReEDSregion 1,class 2;ReEDSregion 2,class 1,等等)。

我想对每个 ReEDSregion 和类组合(按 ReEDSregion 和类)的变量“grid_cost”进行 10 个集群的 kmeans 聚类。
一些 ReEDSregion 和类组合的观测值少于 10 个,因此集群的数量与观测值的数量一样多。我首先为此创建了一个函数:

make_bins <- function (dtbl, nbins) {
  # we only need to cluster if there are more unique values for the grid cost than the number of bins desired
  if (dtbl[,length(unique(grid_cost))] > nbins) {
    bins <- kmeans(dtbl[, grid_cost], nbins)$cluster
  } else {
    unique_vals <- sort(unique(dtbl[, grid_cost]))
    bins <- dtbl[,which(unique_vals==grid_cost)]
  }
  return(bins)
}

然后我用数据表调用函数:

nbins <- 10
scurve[, bin := make_bins(.SD, nbins), by=c("ReEDSregion", "class")]

但它显示错误:

[.data.table(scurve, , :=(bin, make_bins(.SD, nbins)) 中的错误,by = c("ReEDSregion", :
提供 3 个项目分配给“bin”列中大小为 7 的第 8 组。 RHS 长度必须为 1(单个值即可)或与 LHS 长度完全匹配。如果您希望“回收” RHS,请明确使用 rep() 以向您的代码读者明确这一意图。

这是什么意思?有人可以帮我吗?

我的数据表版本是1.12.2。

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    错误是由于代码中的else 部分引起的,此时您的值比 bin 少。

    在这种情况下你可以使用factor

    make_bins <- function (dtbl, nbins) {
      # we only need to cluster if there are more unique values for the grid cost than the number of bins desired
      if (dtbl[,length(unique(grid_cost))] > nbins) {
        bins <- kmeans(dtbl[, grid_cost], nbins)$cluster
      } else {
        bins <- dtbl[,dtbl[,as.numeric(factor(grid_cost))]]
      }
      return(bins)
    }
    

    使用mtcars进行测试:

    m <- mtcars
    setDT(m)
    m[,grid_cost:=wt]
    
    nbins <- 10
    m[, bin := make_bins(.SD, nbins),by=c("cyl")]
    m
    
         mpg cyl  disp  hp drat    wt  qsec vs am gear carb grid_cost bin
     1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4     2.620   1
     2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4     2.875   3
     3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1     2.320   2
     4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1     3.215   4
     5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2     3.440   7
     6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1     3.460   6
    ...
    

    【讨论】:

    • 非常感谢它有效!我认为我指出,当值的差异少于 bin 的数量时,bin 将只是“else”部分可用的唯一值的数量。我认为这是一个 data.table 版本问题。再次感谢!
    • @shuu,很高兴我能帮上忙。如果这回答了您的问题,请考虑accepting it
    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2013-07-06
    • 2012-04-02
    • 2017-03-06
    • 2015-04-29
    • 1970-01-01
    • 2021-02-04
    相关资源
    最近更新 更多