svmlight: Fast svmlight reader and writer

Description Usage Arguments Examples

Description

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

Usage

1
2
3
4
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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)

Example output

[1] TRUE
[1] TRUE

sparsio documentation built on March 26, 2020, 6:22 p.m.