7

I want to read a tab-delimited text file into a Breeze DenseMatrix. I see in the ScalaDoc that this should be possible and there are a whole set of I/O classes, but I can't find any examples and it's hard to digest the ScalaDoc.

Can someone provide a simple read/write example?

Brian
  • 20,195
  • 6
  • 34
  • 55
Dave DeCaprio
  • 2,051
  • 17
  • 31

2 Answers2

5

There's a way to read a csv file into densematrix

import breeze.linalg._
import java.io._
val matrix=csvread(new File("your file localtion"),',')

api:http://www.scalanlp.org/api/breeze/index.html#breeze.linalg.package

Ash
  • 336
  • 4
  • 19
dandandan
  • 69
  • 1
  • 5
  • 1
    scala is a case-sensitive language, the "file" should be "File". "val matrix=csvread(new File("your file localtion"),'$seperator')" – madeinQuant Feb 23 '18 at 05:33
3

You can use scala.io.Source to read in tab delimited data from file.

Some sample data:

0       1       2       3       4       5
6       7       8       9       10      11

One of the DenseMatrix constructors has this form new DenseMatrix(rows: Int, data: Array[V], offset: Int = 0) so I'll use that.

Get the number of rows:

scala> scala.io.Source.fromFile("TabDelimited.txt").getLines.size
res 0:Int = 2

Then get the data as an Array[Int]:

scala> scala.io.Source.fromFile("TabDelimited.txt").getLines.toArray.flatMap(_.split("\t")).map(_.toInt)
res1: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

Then res0 and res1 can be used to create a new DenseMatrix.

Brian
  • 20,195
  • 6
  • 34
  • 55
  • 2
    Thanks, although when reading it in the res1 was in row-major order and the constructor expects column-major. So the constructor i used was `DenseMatrix(res1.size/res0).t` – Dave DeCaprio Feb 15 '13 at 13:25