【问题标题】:What does class specific means mean?特定类别意味着什么?
【发布时间】:2021-12-11 13:30:46
【问题描述】:

我正在浏览一些工作表,并且遇到了一个让我感到困惑的问题。所以问题要求通过 r 代码生成大小为 200 x 2 随机生成的数据的矩阵 x matrix(rnorm(300*2), ncol=2)。在矩阵中,每 100 个条目属于一个类,从 1 到 3。它还要求将变量 z 中的类特定方法定义为 matrix(c(0,0,3,0,3,0),3,2)。有一个向量 y,标签为 1 到 3。然后它要求仅使用 yz 为每个类的数据点分配类特定的方法。我对措辞有点困惑。向量y 就是 (1, 1, 1, 1, ....., 3, 3, 3, 3),其中 1、2、3 重复了 100 次。如何仅使用 yz 为数据点分配特定于类的方法?

我的最终目标是将特定类别的均值分配给 x 中的数据点并绘制 x 以显示它们是否是线性可分的。

编辑添加:x 矩阵由matrix(rnorm(300*2), ncol=2) 定义。我之前把它写成matrix(rnorm(200*2), ncol=2)

【问题讨论】:

  • y 的长度是 100 还是 300?
  • @Park y的长度为300
  • 那么x有200个样本。他们在哪个班?
  • @Park 请检查我编辑的帖子。我已经纠正了。它应该说matrix(rnorm(300*2), ncol=2)。我错误地把它写成 200 * 2

标签: r svm


【解决方案1】:

如果 y 的长度为 100 表示组,

x <- matrix(rnorm(300*2), ncol=2)

y <- c(rep(1,100),rep(2,100),rep(3,100))
x <- as.data.frame(x)
x$group <- y

基地R

z <- aggregate(x[,1:2], list(x$group), mean)
as.matrix(z[,-1])

                V1           V2
[1,]  0.0008374542 -0.154219125
[2,] -0.0902750516 -0.052787157
[3,] -0.0758869275  0.004063367

dplyr

library(dplyr)

z <- x %>%
  group_by(group) %>%
  summarize(m1 = mean(V1), 
            m2 = mean(V2)) %>%
  select(-group) %>%
  as.matrix

                m1           m2
[1,]  0.0008374542 -0.154219125
[2,] -0.0902750516 -0.052787157
[3,] -0.0758869275  0.004063367

如果您想在问题中使用z 的列名,请添加colnames(z) &lt;- NULL

【讨论】:

  • 谢谢。但是 z 矩阵已经被定义为matrix(c(0,0,3,0,3,0),3,2)。我不认为 3 可以被覆盖
  • @ambrosianium 请问z在你的问题中的作用?
  • 这部分问题可能会回答你To make these 300 entries distinctive and divide the data into 3 different classes, lets define class specific means in variable z as ”matrix(c(0,0,3,0,3,0),3,2)”. Also, generate a response vector y of size N that contains labels (1 to 3) for the data in x. Using z and y, assign class specific means to data points of each class and this operation will change the entries of the matrix x and divide it into three classes.
  • @ambrosianium 我的理解是,做成z的形式,matrix(3,2)...
猜你喜欢
  • 2013-01-16
  • 2023-03-10
  • 2018-07-05
  • 2018-10-10
  • 2021-05-17
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
相关资源
最近更新 更多