项目作者: eddelbuettel

项目描述 :
Rcpp bindings for NumPy files
高级语言: C++
项目地址: git://github.com/eddelbuettel/rcppcnpy.git
创建时间: 2013-11-28T16:28:26Z
项目社区:https://github.com/eddelbuettel/rcppcnpy

开源协议:GNU General Public License v2.0

下载


RcppCNPy: Rcpp bindings for NumPy files

CI
License
CRAN
Dependencies
Downloads
Last Commit
status
DOI

About

This package uses the cnpy library
written by Carl Rogers to provide read and write facilities for files created
with (or for) the NumPy extension for Python. Vectors and matrices of
numeric types can be read or written to and from files as well as compressed
files. Support for integer files is available if the package has been built
with -std=c++11 which is the default starting with release 0.2.3 following
the release of R 3.1.0, and available on all platforms following the release
of R 3.3.0 with the updated ‘Rtools’.

Example

The following Python code

  1. >>> import numpy as np
  2. >>> fm = np.arange(12).reshape(3,4) * 1.1
  3. >>> fm
  4. array([[ 0. , 1.1, 2.2, 3.3],
  5. [ 4.4, 5.5, 6.6, 7.7],
  6. [ 8.8, 9.9, 11. , 12.1]])
  7. >>> np.save("fmat.npy", fm)
  8. >>>
  9. >>> im = np.arange(12).reshape(3,4)
  10. >>> im
  11. array([[ 0, 1, 2, 3],
  12. [ 4, 5, 6, 7],
  13. [ 8, 9, 10, 11]])
  14. >>> np.save("imat.npy", im)
  15. >>>

saves two matrices in floating-point and integer representation.

With this R code we can read and assign the files:

  1. R> library(RcppCNPy)
  2. R> fmat <- npyLoad("fmat.npy")
  3. R> fmat
  4. [,1] [,2] [,3] [,4]
  5. [1,] 0.0 1.1 2.2 3.3
  6. [2,] 4.4 5.5 6.6 7.7
  7. [3,] 8.8 9.9 11.0 12.1
  8. R>
  9. R> imat <- npyLoad("imat.npy", "integer")
  10. R> imat
  11. [,1] [,2] [,3] [,4]
  12. [1,] 0 1 2 3
  13. [2,] 4 5 6 7
  14. [3,] 8 9 10 11
  15. R>

Going the opposite way by saving in R and reading in Python works equally
well. An extension not present in CNPy
allows reading and writing of gzip-compressed files.

The package has been tested and used on several architecture, and copes
correctly with little-vs-big endian switches.

More details are available in the package vignette.

Installation

The package is on CRAN and can be installed per:

  1. R> install.packages("RcppCNPy")

Status

On CRAN, stable and mostly
feature-complete.

Alternative: reticulate

The reticulate package can also provide easy and comprehensive access
to NumPy data; see the
additional vignette in RcppCNPy
for examples and more details.

Feedback

Contributions are welcome, please use the GitHub
issue tracker for
bug reports, feature requests or general discussions before sending
pull requests.

Author

Dirk Eddelbuettel and Wush Wu

License

GPL (>= 2)