简体   繁体   中英

Load a csv file into a Breeze DenseMatrix[Double]

I have a csv file and I want to load into a Breeze DenseMatrix[Double]

This code eventually will work but I think it's not the scala way of doing things:

  val resource = Source.fromResource("data/houses.txt")
  val lines: Iterator[String] = resource.getLines
  val tmp = lines.toArray
  val numRows: Int = tmp.size
  val numCols: Int = tmp(0).split(",").size
  val m = DenseMatrix.zeros[Double](numRows, numCols)
  //Now do some for loops and fill the matrix

Is there a more elegant and functional way of doing this?

  val resource = Source.fromResource("data/houses.txt")
  val lines: Iterator[String] = resource.getLines
  val tmp = lines.map(l => l.split(",").map(str => str.toDouble)).toList
  val m = DenseMatrix(tmp:_*)

much better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM