简体   繁体   English

使用apply从矩阵构建特征张量

[英]Using apply in building a tensor of features from a matrix

Consider the following code: 考虑以下代码:

EmbedFeatures <- function(x,w) {
     c_rev <- seq(from=w,to=1,by=-1)
     em <- embed(x,w)
     em <- em[,c_rev]
     return(em)
}

m=matrix(1:1400,100,14)

X.tr<-c()
F<-dim(m)[2]
W=16
for(i in 1:F){ X.tr<-abind(list(X.tr,EmbedFeatures(m[,i],W)),along=3)}

this builds an array of features, each row has W=16 timesteps. 这构建了一个特征数组,每行具有W = 16个时间步长。 The dimensions are: 尺寸为:

> dim(X.tr)
[1] 85 16 14

The following are the first samples: 以下是第一个示例:

> X.tr[1,,1]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
> X.tr[1,,2]
 [1] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
> X.tr[1,,3]
 [1] 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

I would like to use apply to build this array, but the following code does not work: 我想使用apply来构建此数组,但是以下代码不起作用:

X.tr <- apply(m,2,EmbedFeatures, w=W)

since it gives me the following dimensions: 因为它具有以下尺寸:

> dim(X.tr)
[1] 1360   14

How can I do it? 我该怎么做?

Firstly, thanks for providing a great reproducible example! 首先,感谢您提供了一个很好的可复制示例!

Now, as far as I know, you can't do this with apply . 现在,据我所知,您不能使用apply做到这一点。 You can, however, do it with a combination of plyr::aaply , which allows you to return multidimensional arrays, and base::aperm , which allows you to transpose multidimensional arrays. 但是,您可以使用plyr::aaplybase::aperm的组合来plyr::aaply ,该组合允许您返回多维数组,而base::aperm则可以对多维数组进行转置。

See here for aaply function details and here for aperm function details. 这里aaply功能的详细信息,并在这里aperm功能的详细信息。

After running your code above, you can do: 在上面运行代码后,您可以执行以下操作:

library(plyr)
Y.tr <- plyr::aaply(m, 2, EmbedFeatures, w=W)
Z.tr <- aperm(Y.tr, c(2,3,1))

dim(Y.tr)
[1] 14 85 16

dim(Z.tr)
[1] 85 16 14

I turned those two lines of code into a function. 我把这两行代码变成了一个函数。

using_aaply <- function(m = m) {
    Y.tr <- aaply(m, 2, EmbedFeatures, w=W)
    Z.tr <- aperm(Y.tr, c(2,3,1))
    return(Z.tr)
}

Then I did some microbenchmarking. 然后我做了一些微基准测试。

library(microbenchmark)
microbenchmark(for(i in 1:F){ X.tr<-abind(list(X.tr,EmbedFeatures(m[,i],W)),along=3)}, times=100)

Unit: milliseconds
                                                                                  expr
 for (i in 1:F) {     X.tr <- abind(list(X.tr, EmbedFeatures(m[, i], W)), along = 3) }
      min       lq     mean   median       uq      max neval
 405.0095 574.9824 706.0845 684.8531 802.4413 1189.845   100

microbenchmark(using_aaply(m=m), times=100)

Unit: milliseconds
               expr      min       lq     mean   median       uq      max
 using_aaply(m = m) 4.873627 5.670474 7.797129 7.083925 9.674041 19.74449
 neval
   100

It seems like it's loads faster using aaply and aperm compared to abind in a for-loop. 这似乎是它的加载速度较快,使用aaplyaperm相比abind在一个for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM