Package 'sparsio'

Title: I/O Operations with Sparse Matrices
Description: Fast 'SVMlight' reader and writer. 'SVMlight' is most commonly used format for storing sparse matrices (possibly with some target variable) on disk. For additional information about 'SVMlight' format see <http://svmlight.joachims.org/>.
Authors: Dmitriy Selivanov [aut, cre], Felix Riedel [aut]
Maintainer: Dmitriy Selivanov <[email protected]>
License: GPL (>= 2) | file LICENSE
Version: 1.0.1
Built: 2024-11-11 05:30:20 UTC
Source: https://github.com/rexyai/sparsio

Help Index


Fast svmlight reader and writer

Description

Reads and writes svmlight files. Notice that current implementation can't handle comments in svmlight files during reading.

Usage

read_svmlight(file, type = c("CsparseMatrix", "RsparseMatrix",
  "TsparseMatrix"), zero_based = TRUE, ncol = NULL)

write_svmlight(x, y = rep(0, nrow(x)), file, zero_based = TRUE)

Arguments

file

string, path to svmlight file

type

target class for sparse matrix. CsparseMatrix is default value because it is main in R's Matrix package. However internally matrix first read into RsparseMatrix and then coerced with as() to target type. This is because smvlight format is essentially equal to CSR sparse matrix format.

zero_based

logical, whether column indices in file are 0-based (TRUE) or 1-based (FALSE).

ncol

number of columns in target matrix. NULL means that number of columns will be determined from file (as a maximum index). However it is possible that user expects matrix with a predefined number of columns, so function can override inherited from data value.

x

input sparse matrix. Should inherit from Matrix::sparseMatrix.

y

target values. Labels must be an integer or numeric of the same length as number of rows in x.

Examples

library(Matrix)
library(sparsio)
i = 1:8
j = 1:8
v = rep(2, 8)
x = sparseMatrix(i, j, x = v)
y = sample(c(0, 1), nrow(x), replace = TRUE)
f = tempfile(fileext = ".svmlight")
write_svmlight(x, y, f)
x2 = read_svmlight(f, type = "CsparseMatrix")
identical(x2$x, x)
identical(x2$y, y)