scala 将csv文件加载到微风密集矩阵[Double]

68de4m5k  于 2023-01-30  发布在  Scala
关注(0)|答案(1)|浏览(170)

我有一个csv文件,我想加载到Breeze DenseMatrix[Double]
这段代码最终会起作用,但我认为这不是scala的做事方式:

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

有没有一种更优雅、更实用的方法来做到这一点?

sqxo8psd

sqxo8psd1#

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:_*)

好多了

相关问题