【问题标题】:Getting outputs from a "for" loop in r into a matrix or data frame从 r 中的“for”循环中获取输出到矩阵或数据框中
【发布时间】:2020-12-29 17:52:33
【问题描述】:

我正在尝试执行以下循环:

  1. 将我的矩阵 X 的 n 行中的每一行与 X.prototype 矩阵进行比较。

  2. 找到 X.prototype 中与 X 的每一行最近的行。

  3. 如果两行具有相同的标签(y 和 y.prototype),则使值接近;否则,把它们放得更远。

我的代码:

for (i in (1:n)){ 
  
  closest.row <- which.min(colSums((t(X.prototype) - X[i,])^2))
  
  X.new.x1 <- ifelse(y.prototype[closest.row] == y[i],
                     X.prototype[closest.row,1]+(eta*(X[i,1]-X.prototype[closest.row,1])),
                     X.prototype[closest.row,1]-(eta*(X[i,1]-X.prototype[closest.row,1])))
  X.new.x2<- ifelse(y.prototype[closest.row] == y[i],
                    X.prototype[closest.row,2]+(eta*(X[i,2]-X.prototype[closest.row,2])),
                    X.prototype[closest.row,2]-(eta*(X[i,2]-X.prototype[closest.row,2])))
  X.new <- matrix(c(X.new.x1,X.new.x2),ncol=2) 

  plot(X.new.x2~X.new.x1)

 print(X.new)
  
}


----------


set.seed(123)                        # Set seed for reproducibility
n <- 100
X <- cbind(x1 = runif(n, -1.5, 1.5),
           x2 = runif(n, -1.5, 1.5)) # Generate random points
y <- as.integer(rowSums(X^2)<1)      # Determine whether inside the circle#
idx <- sample(100, 10)               # Mess up 10 class labels ...
y[idx] <- 1-y[idx]                   # ... by flipping the label

is <- c(sample(which(y==0),K), sample(which(y==1),K))
X.prototype <- X[is,]
y.prototype <- y[is]                 # Will be K times 0, followed by K times 1

K <- 10
eta <- 0.25
H <- 25

问题是结果以单个向量的形式出现,我无法将它们组合成一个 X.new 矩阵,用于新的预测并绘制它。

this is a link to how my output looks

This is how I'd like them to look

谢谢 S

【问题讨论】:

  • X.newclass 是什么?

标签: r loops if-statement matrix


【解决方案1】:

试试这些改变:

#Initial dataframe
X.df <- data.frame()
#Loop
for (i in (1:n)){ 
  
  closest.row <- which.min(colSums((t(X.prototype) - X[i,])^2))
  
  X.new.x1 <- ifelse(y.prototype[closest.row] == y[i],
                     X.prototype[closest.row,1]+(eta*(X[i,1]-X.prototype[closest.row,1])),
                     X.prototype[closest.row,1]-(eta*(X[i,1]-X.prototype[closest.row,1])))
  X.new.x2<- ifelse(y.prototype[closest.row] == y[i],
                    X.prototype[closest.row,2]+(eta*(X[i,2]-X.prototype[closest.row,2])),
                    X.prototype[closest.row,2]-(eta*(X[i,2]-X.prototype[closest.row,2])))
  X.new <- matrix(c(X.new.x1,X.new.x2),ncol=2) 

  #Bind
  X.df <- rbind(X.df,X.new)
    
  plot(X.new.x2~X.new.x1)
  
}
print(X.df)

输出:

head(X.df)
          V1         V2
1 -0.5464952  0.2673293
2  0.8649154 -0.5015294
3 -0.4554457  0.1837973
4  0.8687970  1.0649177
5  0.7507938 -0.6140885
6 -1.3711987  1.3144680

您需要一个空数据框和rbind 才能获得平方结构。

【讨论】:

  • 嗨@Duck,感谢您的回答。但是,即使进行了这些更改,我也会为每行得到 100 个矩阵:例如:[,1] [,2] [1,] -0.4458489 -0.796795 [,1] [,2] [1,] -0.1011919 0.5764171跨度>
  • 编辑:你是对的!这行得通!但是由于某种原因,它只绘制了矩阵的最后一个点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 2021-05-24
相关资源
最近更新 更多