将 SOMbrero 包中的集群和节点添加到训练数据中

2023-12-28

我正在玩一点SOMbrero https://cran.r-project.org/web/packages/SOMbrero/SOMbrero.pdf包裹。我想附上像这样创建的簇号(取自here https://cran.r-project.org/web/packages/SOMbrero/vignettes/doc-numericSOM.html):

my.sc <- superClass(iris.som, k=3)

以及 SOM 节点到训练数据集的 X 和 Y 坐标。

在某些代码中,我使用 kohonen 包,创建如下集群:

range01 <- function(x){(x-min(x))/(max(x)-min(x))}

ind <- sapply(SubsetData, is.numeric)
SubsetData[ind] <- lapply(SubsetData[ind], range01)

TrainingMatrix <- as.matrix(SubsetData)

GridDefinition <- somgrid(xdim = 4, ydim = 4, topo = "rectangular", toroidal = FALSE)

SomModel <- som(
    data = TrainingMatrix,
    grid = GridDefinition,
    rlen = 10000,
    alpha = c(0.05, 0.01),
    keep.data = TRUE
)

nb <- table(SomModel$unit.classif)
groups = 5
tree.hc = cutree(hclust(d=dist(SomModel$codes[[1]]),method="ward.D2",members=nb),groups)

plot(SomModel, type="codes", bgcol=rainbow(groups)[tree.hc])

add.cluster.boundaries(SomModel, tree.hc)
result <- OrginalData
result$Cluster <- tree.hc[SomModel$unit.classif]
result$X <- SomModel$grid$pts[SomModel$unit.classif,"x"]
result$Y <- SomModel$grid$pts[SomModel$unit.classif,"y"]

write.table(result, file = "FinalData.csv", sep = ",", col.names = NA, quote = FALSE)

PS:

可以找到一些使用 iris 数据集的示例代码here https://github.com/cran/SOMbrero/blob/master/demo/numeric.R.

PPS:

我玩了一下上面引用的 iris 代码,认为我已经成功提取了集群、节点 ID 和原型(请参阅下面的代码)。缺少的是坐标 X 和 Y。我认为它们在这里:

iris.som$parameters$the.grid$coord

Code:

library(SOMbrero)

set.seed(100)
setwd("D:\\RProjects\Clustering")

#iris.som <- trainSOM(x.data=iris[,1:4],dimension=c(10,10), maxit=100000, scaling="unitvar", radius.type="gaussian")
iris.som <- trainSOM(x.data=iris[,1:4],dimension=c(3,3), maxit=100000, scaling="unitvar", radius.type="gaussian")

# perform a hierarchical clustering
## with 3 super clusters
iris.sc <- superClass(iris.som, k=3)
summary(iris.sc)

# compute the projection quality indicators
quality(iris.som)

iris1 <- iris
iris1$Cluster = iris.sc$cluster[iris.sc$som$clustering]
iris1$Node = iris.sc$som$clustering
iris1$Pt1Sepal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,1]
iris1$Pt2Sepal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,2]
iris1$Pt3Petal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,3]
iris1$Pt4Petal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,4]

write.table(iris1, file = "Iris.csv", sep = ",", col.names = NA, quote = FALSE)

我想我已经使用 iris 示例弄清楚了(请更正/改进代码!-我对 R 不太流利):

library(SOMbrero)

set.seed(100)
setwd("D:\\RProjects\\SomBreroClustering")

iris.som <- trainSOM(x.data=iris[,1:4],dimension=c(5,5), maxit=10000, scaling="unitvar", radius.type="letremy")

# perform a hierarchical clustering
# with 3 super clusters
iris.sc <- superClass(iris.som, k=3)
summary(iris.sc)

# compute the projection quality indicators
quality(iris.som)

iris1 <- iris
iris1$Cluster = iris.sc$cluster[iris.sc$som$clustering]
iris1$Node = iris.sc$som$clustering
iris1$Pt1Sepal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,1]
iris1$Pt2Sepal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,2]
iris1$Pt3Petal.Length = iris.sc$som$prototypes[iris.sc$som$clustering,3]
iris1$Pt4Petal.Width = iris.sc$som$prototypes[iris.sc$som$clustering,4]
iris1$X = iris.som$parameters$the.grid$coord[iris.sc$som$clustering,1]
iris1$Y = iris.som$parameters$the.grid$coord[iris.sc$som$clustering,2]

write.table(iris1, file = "Iris.csv", sep = ",", col.names = NA, quote = FALSE)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 SOMbrero 包中的集群和节点添加到训练数据中 的相关文章

  • Rsolnp:在 cbind(temp, funv) 中:结果的行数不是向量长度的倍数(arg 1)

    我是 stackoverflow 的新手 搜索了很多 但找不到我的问题的答案 我正在尝试使用优化包 Rsolnp 来最小化以下问题 尽管求解器为我提供了解决方案 但每次运行代码时我都会收到以下警告消息 警告消息 1 在 cbind temp
  • 在 r 中导出矩阵

    我想在 R 中导出一个矩阵 并保留行和列的名称 当我使用 write table 或 write csv 时 我得到一个带有新列的矩阵 我该如何使用这个功能 感谢您的帮助 您不会获得新列 行名称将保存为文本文件中的第一列 因此 您可以指定在
  • 如何让 print() 将参数传递给 R 中用户定义的打印方法?

    我在 R 中定义了一个 S3 类 它需要自己的打印方法 当我创建这些对象的列表并打印它时 R 按其应有的方式对列表中的每个元素使用我的打印方法 我想对打印方法实际显示的数量进行一些控制 因此 我的类的 print 方法需要一些额外的参数 但
  • LaTex 中与 knit 和 xtable 交叉引用的问题

    我目前正在与 R Studio 合作 使用 LaTex 中的 R knitr 生成 PDF 文档 在这些文档中 我想在文本中引用的表格中展示我的部分结果 我使用 R 中的 xtable 包生成这些表 它运行良好并为我提供了正确的表 到目前为
  • 如何获得 STAN 中最大似然估计的标准误差?

    我在 Stan 中使用最大似然优化 但不幸的是optimizing 函数不报告标准错误 gt MLb4c lt optimizing get stanmodel fitb4c data win data init inits STAN OP
  • 替换列表列表中的元素

    The applyR 中的函数是简化 for 循环以获得输出的好方法 是否有一个等效的函数可以帮助人们在替换向量的值时避免 for 循环 通过示例可以更好地理解这一点 Take this list for example x list li
  • 将 Instagram/youtube 嵌入 Shiny R 应用程序

    我想通过点击图表来播放 Instagram 或 Youtube 视频 例如显示异常值等 到目前为止 明确告诉 Shiny 视频内容是有效的 require shiny require ggplot2 data df lt data fram
  • 计算例如具有多列 data.frames 的列表中的平均值

    我有几个 data frames 的列表 每个 data frame 有几列 通过使用mean mylist first dataframe a我可以得到这个 data frame 中 a 的平均值 但是我不知道如何计算列表中存储的所有 d
  • 如何在R中删除重复项

    我有一个非常大的数据集 如下所示 df lt data frame school c a a a b b c c c year c 3 3 1 4 2 4 3 1 GPA c 4 4 4 3 3 3 2 2 school year GPA
  • 删除ggplot2中的负图区域[重复]

    这个问题在这里已经有答案了 如何删除 ggplot2 中 x 轴和 y 轴下方的绘图区域 请参见下面的示例 我尝试了几个主题元素 panel border panel margin plot margin 但没有任何运气 p lt ggpl
  • 我无法下载 R 中的 reshape2 包 [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我在尝试安装 R 包时收到此响应 gt installed packages reshape2 Package LibPath V
  • 在 R 中向散点图添加线条

    如何向图表添加线条 我做了以下 dat lt data frame xvar 1 20 rnorm 20 sd 10 yvar 1 20 rnorm 20 sd 10 zvar 1 20 rnorm 20 sd 10 plot dat 1
  • 如何计算R中移动窗口内的平均斜率

    我的数据集包含2个变量y 和 t 05s y 每 05 秒测量一次 我正在尝试计算移动中的平均坡度20秒窗口 即计算第一个 20 秒斜率值后 窗口向前移动一个时间单位 05 秒 并计算下一个 20 秒窗口 在以下位置生成连续 20 秒斜率值
  • 将数据框中的每个 x 个字符拆分为字符串

    我知道这里有一些关于每隔一段时间分割一个字符串的答案nth字符 例如this one https stackoverflow com questions 23208490 split each character in r and this
  • R 中的快速 QR 分解

    我有大量矩阵 需要对其执行 QR 分解并存储生成的 Q 矩阵 进行归一化 以便 R 矩阵在其对角线上具有正数 除了使用之外还有其他方法吗qr 功能 这是工作示例 system time Parameters for the matrix t
  • 在 R 中创建虚拟变量,排除某些情况为 NA

    我的数据看起来像这样 V1 V2 A 0 B 1 C 2 D 3 E 4 F 5 G 9 我想创建一个虚拟变量R where 0 1 1 2 3 4 and NA 0 5 9 应该很简单 有人可以帮忙吗 我们可以转换V2 into a fa
  • R 中的列乘以子字符串

    假设我有一个数据框 其中包含多个组件及其在多个列中列出的属性 并且我想对这些列运行多个函数 我的方法是尝试将其基于每个列标题中的子字符串 但我无法弄清楚如何做到这一点 下面是数据框的示例 Basket F Type 1 F Qty 1 F
  • 将每列的值乘以 R 中另一个 data.frame 中的权重

    我有两个data frames df and weights 代码如下 df看起来像这样 id a b d EE f 1 this 0 23421153 0 02324956 0 5457353 0 73068586 0 5642554 2
  • 只读取选定的列

    谁能告诉我如何仅读取下面每年数据的前 6 个月 7 列 例如使用read table Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2009 41 27 25 31 31 39 2
  • 文本挖掘 pdf 文件/词频问题

    我正在尝试挖掘一篇具有丰富 pdf 编码和图表的文章的 pdf 我注意到 当我挖掘一些 pdf 文档时 我得到的高频词是 phi taeoe toe sigma gamma 等 它与某些 pdf 文档配合良好 但与其他文档配合使用时却得到这

随机推荐