TO_scipy_sparse: conversion of an R sparse matrix to a scipy sparse matrix

Description Usage Arguments Details References Examples

View source: R/nmslib.R

Description

conversion of an R sparse matrix to a scipy sparse matrix

Usage

1
TO_scipy_sparse(R_sparse_matrix)

Arguments

R_sparse_matrix

an R sparse matrix. Acceptable input objects are either a dgCMatrix or a dgRMatrix.

Details

This function allows the user to convert either an R dgCMatrix or a dgRMatrix to a scipy sparse matrix (scipy.sparse.csc_matrix or scipy.sparse.csr_matrix). This is useful because the nmslibR package accepts besides an R dense matrix also python sparse matrices as input.

The dgCMatrix class is a class of sparse numeric matrices in the compressed, sparse, column-oriented format. The dgRMatrix class is a class of sparse numeric matrices in the compressed, sparse, column-oriented format.

References

https://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/dgCMatrix-class.html, https://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/dgRMatrix-class.html, https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.html#scipy.sparse.csc_matrix

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if (reticulate::py_available() && reticulate::py_module_available("scipy")) {

  if (Sys.info()["sysname"] != 'Darwin') {

    library(nmslibR)


    # 'dgCMatrix' sparse matrix
    #--------------------------

    data = c(1, 0, 2, 0, 0, 3, 4, 5, 6)

    dgcM = Matrix::Matrix(data = data, nrow = 3,

                          ncol = 3, byrow = TRUE,

                          sparse = TRUE)

    print(dim(dgcM))

    res = TO_scipy_sparse(dgcM)

    print(res$shape)


    # 'dgRMatrix' sparse matrix
    #--------------------------

    dgrM = as(dgcM, "RsparseMatrix")

    print(dim(dgrM))

    res_dgr = TO_scipy_sparse(dgrM)

    print(res_dgr$shape)
  }
}

nmslibR documentation built on March 13, 2020, 2 a.m.