gpt4 book ai didi

scala - 从 native 矩阵格式转换,缩放

转载 作者:行者123 更新时间:2023-12-02 20:08:14 25 4
gpt4 key购买 nike

所以这个问题与问题Transforming matrix format, 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))

但是,在那里,我们遇到了零问题。众所周知,缩放将跳过零值字段。因此,例如,我们得到矩阵:
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

那是不对的。那么,我该如何处理呢?我看到两个可能的变体:
  • 找到方法,添加零(实际上,dunno如何插入数据)
  • 以自己的矩阵格式编写自己的操作(这是不可取的,因为我对缩放矩阵操作感兴趣,并且不想自己编写所有这些操作)

  • 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"))

    关于scala - 从 native 矩阵格式转换,缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032010/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com