1
votes

I have generated a libsvm file in scala using the org.apache.spark.mllib.util.MLUtils package.

The file format is as follows:

49.0 109:2.0 272:1.0 485:1.0 586:1.0 741:1.0 767:1.0
49.0 109:2.0 224:1.0 317:1.0 334:1.0 450:1.0 473:1.0 592:1.0 625:1.0 647:1.0 
681:1.0 794:1.0
17.0 26:1.0 109:1.0 143:1.0 198:2.0 413:1.0 476:1.0 582:1.0 586:1.0 611:1.0 
629:1.0 737:1.0
12.0 255:1.0 394:1.0
etc etc

I read the file into r using e1071 package as follows:

m= read.matrix.csr(filename)

The structure of the resultant matrix.csr is as follows:

 $ x:Formal class 'matrix.csr' [package "SparseM"] with 4 slots
  .. ..@ ra       : num [1:31033] 2 1 1 1 1 1 2 1 1 1 ...
  .. ..@ ja       : int [1:31033] 109 272 485 586 741 767 109 224 317 334 ...
  .. ..@ ia       : int [1:2996] 1 7 18 29 31 41 49 65 79 83 ...
  .. ..@ dimension: int [1:2] 2995 796
 $ y: Factor w/ 51 levels "0.0","1.0","10.0",..: 45 45 10 5 42 25 23 41 23 25 ...

When I convert to a dense matrix with as.matrix(m) it produces one column and two rows, each with an uninterpretable (by me) object in it.

When I simply try to save the matrix.csr back to file (without doing any intermediate processing), I get the following error:

Error in abs(x) : non-numeric argument to mathematical function

I am guessing that the libsvm format is incompatible but I'm really not sure. Any help would be much appreciated.

1

1 Answers

1
votes

OK, the short of it:

m= read.matrix.csr(filename)$x

because read.matrix.csr is a list with two elements; the matrix and a vector. In other words, the target/label/class is separated out from the features matrix.

NOTE for fellow r neophytes: In Cran documents, it seems that the "Value" subheading refers to the return values of the function

Value

If the data file includes no y variable, read.matrix.csr returns an object of class matrix.csr,

else a list with components:

x object of class matrix.csr

y vector of numeric values or factor levels, depending on fac