arrays - R重塑数组以包含矩阵列表中的数据两次作为keras的准备
问题描述
我正在尝试创建一个 3D 数组,该数组复制元素(数据的副本),并且两者的行保持相同。这最终将成为时间序列的 keras/tensorflow lstm 输入数组(样本、时间步长、特征),由于不同的原因,我确定我的时间步长也是我的特征并且我的样本是固定的(行)。
一些数据:
perhaps <- matrix(sample(c(0:1), 20, replace = TRUE), nrow = 4,
ncol = 5)
print(perhaps)
perhaps
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 1 0 1
[2,] 0 0 0 0 1
[3,] 0 1 1 0 1
[4,] 1 0 0 1 1
perhaps_2_lst <- list(perhaps, perhaps)
all.equal(perhaps_2_lst[[1]], perhaps_2_lst[[2]])
[1] TRUE
#construct array from SOF inquestion:15213463
perhaps_arr <- array(
data = do.call(rbind, lapply(perhaps_2_lst, as.vector)),
dim = c(dim = c(dim(perhaps_2_lst[[1]])[1],
dim(perhaps_2_lst[[1]])[2], dim(perhaps_2_lst[[1]])[2]))
dim(perhaps_arr)
[1] 4 5 5
令人鼓舞。
print(perhaps_arr)
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 1 1
[2,] 0 0 0 1 1
[3,] 0 1 0 0 0
[4,] 0 1 0 0 0
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 1 1
[2,] 1 0 0 1 1
[3,] 0 0 1 1 1
[4,] 0 0 1 1 1
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 1 1
[2,] 0 0 0 1 1
[3,] 0 1 0 0 0
[4,] 0 1 0 0 0
, , 4
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 1 1
[2,] 1 0 0 1 1
[3,] 0 0 1 1 1
[4,] 0 0 1 1 1
, , 5
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 1 1
[2,] 0 0 0 1 1
[3,] 0 1 0 0 0
[4,] 0 1 0 0 0
哎呀。我猜对数组不太熟悉。我期待类似的东西:
, , 1
#perhaps
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 1 0 1
[2,] 0 0 0 0 1
[3,] 0 1 1 0 1
[4,] 1 0 0 1 1
, , 2
#perhaps
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 1 0 1
[2,] 0 0 0 0 1
[3,] 0 1 1 0 1
[4,] 1 0 0 1 1
IE。重复相同的数据。现在难住了。对于理解这里发生的事情的建议和澄清非常感谢。
解决方案
我们可以使用replicate
n <- 2
replicate(n, perhaps)
#, , 1
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 0 1 0 1
#[2,] 0 0 0 0 1
#[3,] 0 1 1 0 1
#[4,] 1 0 0 1 1
#, , 2
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 0 1 0 1
#[2,] 0 0 0 0 1
#[3,] 0 1 1 0 1
#[4,] 1 0 0 1 1
推荐阅读
- javascript - ReactJs 中的奇怪错误 - onClick 事件
- django - 使用 NGinx 路由网站(PHP 和 Django)
- apache-spark - 什么时候应该在 Spark 中使用 RDD 而不是 Dataset?
- python - 如何使用 python SSH Paramiko 运行 tmsh(F5) 命令
- pdf - PDF 内容流“TJ /Tj”拆分而不弄乱剩余的文本矩阵?
- javascript - 您如何将 getText 的结果存储在变量中以供以后在 Nightwatch 中使用?
- r - axis.text.x 中的文本非常大
- java - [请求处理失败;嵌套异常是 java.lang.NullPointerException]
- python - 在循环列表中的 2 个索引之间查找索引
- html - 如何在类css中为所有具有特定关键字的div添加样式