Scala 从本机矩阵格式转换,滚烫

Scala 从本机矩阵格式转换,滚烫,scala,hadoop,matrix,scalding,Scala,Hadoop,Matrix,Scalding,所以这个问题和这个问题有关 但是现在,我想做背部手术。所以我可以这样做: Tsv(in, ('row, 'col, 'v)) .read .groupBy('row) { _.sortBy('col).mkString('v, "\t") } .mapTo(('row, 'v) -> ('c)) { res : (Long, String) => val (row, v) = res v } .write(Tsv(out)) 但是,在这里,我们遇到了

所以这个问题和这个问题有关

但是现在,我想做背部手术。所以我可以这样做:

Tsv(in, ('row, 'col, 'v))
  .read
  .groupBy('row) { _.sortBy('col).mkString('v, "\t") }
  .mapTo(('row, 'v) -> ('c)) { res : (Long, String) =>
    val (row, v) = res
    v }
  .write(Tsv(out))
但是,在这里,我们遇到了零的问题。正如我们所知,滚烫跳过零值字段。例如,我们得到了矩阵:

1   0   8   
4   5   6   
0   8   9
滚烫格式为:

1   1   1
1   3   8
2   1   4
2   2   5
2   3   6
3   2   8
3   3   9
使用我上面写的函数,我们只能得到:

1   8
4   5   6
8   9
这是不正确的。那么,我该怎么处理呢?我看到两种可能的变体:

  • 找到方法,添加零(实际上,不知道如何插入数据)
  • 以自己的矩阵格式编写自己的操作(这是不可预测的,因为我对滚烫的矩阵操作感兴趣,不想自己编写所有操作)

  • Mb有一些方法,我可以避免跳过矩阵中的零?

    烫伤存储数据的稀疏表示。如果要输出密集矩阵(首先,这不会缩放,因为行在某个点上会超过内存中的容量),则需要枚举所有行和列:

    // First, I highly suggest you use the TypedPipe api, as it is easier to get
    // big jobs right generally
    
    val mat = // has your matrix in 'row1, 'col1, 'val1
    def zero: V = // the zero of your value type 
    val rows = IterableSource(0 to 1000, 'row)
    val cols = IterableSource(0 to 2000, 'col)
    rows.crossWithTiny(cols)
      .leftJoinWithSmaller(('row, 'col) -> ('row1, 'col1), mat)
      .map('val1 -> 'val1) { v: V =>
        if(v == null) // this value should be 0 in your type:
          zero
        else
          v
      }
      .groupBy('row) { 
        _.toList[(Int, V)](('col, 'val1) -> 'cols)
      }
      .map('cols -> 'cols) { cols: List[(Int, V)] =>
        cols.sortBy(_._1).map(_._2).mkString("\t")
      }
      .write(TypedTsv[(Int, String)]("output"))
    

    嗯,我明白了,我有一个枚举值的想法,但不知道如何。谢谢你的评论!是的,它可以导致我们的矩阵非常非常大,但对我来说非常有趣,我们如何才能使稀疏矩阵不是稀疏的。p、 我不相信是谁回答了我的问题。。。