eddelbuettel / rcppcnpy

Rcpp bindings for NumPy files
GNU General Public License v2.0
26 stars 16 forks source link

Addition to vignette for 3D examples #23

Closed cranedroesch closed 5 years ago

cranedroesch commented 5 years ago

Thanks for the help over on SO. Here's an example that you could add to the vignette for 3d examples:

Define (and look at) a 3D array in python:

import numpy as np
arr = np.array(list(range(24))).reshape(4,3,2)
np.save("arr", arr)
print(arr[:,:,0])
print(arr[:,:,1])
[[ 0  2  4]
 [ 6  8 10]
 [12 14 16]
 [18 20 22]]
[[ 1  3  5]
 [ 7  9 11]
 [13 15 17]
 [19 21 23]]

Open it in R:

> library(reticulate)
> np <- import("numpy")
> # data reading
> arr <- np$load("arr.npy")
> arr
, , 1

     [,1] [,2] [,3]
[1,]    0    2    4
[2,]    6    8   10
[3,]   12   14   16
[4,]   18   20   22

, , 2

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    7    9   11
[3,]   13   15   17
[4,]   19   21   23