Learn R Programming

nmslibR (version 1.0.1)

dgCMatrix_2scipy_sparse: conversion of an R dgCMatrix to a scipy sparse matrix

Description

conversion of an R dgCMatrix to a scipy sparse matrix

Usage

dgCMatrix_2scipy_sparse(dgc_mat_object)

Arguments

dgc_mat_object

an R sparse matrix of type dgCMatrix

Details

This function allows the user to convert an R dgCMatrix to a scipy sparse matrix (scipy.sparse.csc_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. In this implementation the non-zero elements in the columns are sorted into increasing row order. dgCMatrix is the <U+201C>standard<U+201D> class for sparse numeric matrices in the Matrix package.

References

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

Examples

Run this code
# NOT RUN {
if (reticulate::py_available() && reticulate::py_module_available("scipy")) {
  
  if (Sys.info()["sysname"] != 'Darwin') {

    library(nmslibR)
  
    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 = dgCMatrix_2scipy_sparse(dgcM)
  
    print(res$shape)
  }
}
# }

Run the code above in your browser using DataLab