【问题标题】:presenting new data to fitted self-organizing map and assign rows to clusters将新数据呈现给拟合的自组织地图并将行分配给集群
【发布时间】:2017-05-19 06:57:10
【问题描述】:

我正在使用此代码,它适合自组织图 (SOM),然后对生成的原型向量进行聚类以定义聚类边界:

library(dplyr)
library(kohonen)

setwd('C:\\Users\\Bla\\Source\\Repos\\SomeExcitingRepo')

OrginalData <- read.table("IrisData.txt",
                   header = TRUE, sep = "\t")

SubsetData <- subset(OrginalData, select = c("SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
TrainingMatrix <- as.matrix(scale(SubsetData))

GridDefinition <- somgrid(xdim = 4, ydim = 4, topo = "hexagonal")

SomModel <- kohonen::supersom(data = TrainingMatrix, grid = GridDefinition, rlen = 1000, alpha = c(0.05, 0.01),
             keep.data = TRUE)
groups = 3
iris.hc = cutree(hclust(dist(SomModel$codes[[1]])), groups)

plot(SomModel, type = "codes", bgcol = rainbow(groups)[iris.hc])
add.cluster.boundaries(SomModel, iris.hc)

数据是 iris 数据集,但这只是一个示例。数据集格式如下:

Uid SepalLength SepalWidth  PetalLength PetalWidth  Species
1   5.1 3.5 1.4 0.2 setosa

现在让我们假设这是一个看不见的数据集。我想对其进行规范化并将其呈现给 SOM,然后在每一行中添加额外的列,指示 SOM 的簇号(1、2、3 参见上面的示例)以及获胜节点的 x 和 y 坐标。示例:

Uid SepalLength SepalWidth PetalLength PetalWidth Species Cluster X Y
1 5.1 3.5 1.4 0.2 setosa 3 3 4

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以使用unit.classif 来索引集群或网格点:

    result <- OrginalData
    result$Cluster <- iris.hc[SomModel$unit.classif]
    result$X <- SomModel$grid$pts[SomModel$unit.classif,"x"]
    result$Y <- SomModel$grid$pts[SomModel$unit.classif,"y"]
    
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species Cluster   X         Y
    1          5.1         3.5          1.4         0.2  setosa       1 1.5 2.5980762
    2          4.9         3.0          1.4         0.2  setosa       1 1.0 3.4641016
    3          4.7         3.2          1.3         0.2  setosa       1 1.0 3.4641016
    4          4.6         3.1          1.5         0.2  setosa       1 1.0 3.4641016
    5          5.0         3.6          1.4         0.2  setosa       1 1.0 1.7320508
    6          5.4         3.9          1.7         0.4  setosa       1 1.5 0.8660254
    

    虽然看起来不太好:

    points(jitter(result$X), jitter(result$Y), col=result$Species)
    legend(5,0, legend=unique(result$Species), col=unique(result$Species), pch=1)
    

    【讨论】:

    • 这很好,但您是否在此处展示未缩放的矢量,这可能是它看起来不太好的原因吗?
    • 对不起@csetzkorn,我不明白你的问题。我也认为这还不错,增加网格大小时你可以得到更好的结果
    • 我增加了 som 的大小并且没有缩放特征。此外,我增加了 rlen。现在看起来好多了。只是最后一个,我已经接受了,你知道是否可以将每个获胜节点的原型向量也添加到每一行?
    • 试试cbind(result, as.data.frame(SomModel$codes)[SomModel$unit.classif,])
    • 这会打印创建的数据框,但不会将代码附加到结果中,因此会将其与剩余的注释数据一起写入
    猜你喜欢
    • 2018-09-26
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    相关资源
    最近更新 更多