Google Cloud: matrix multiplication with Bigquery or some other service?

前端 未结 2 973
[愿得一人]
[愿得一人] 2021-01-06 21:58

I am using Google Analytics and processing the data with Bigquery, I need to do a matrix multiplication.

What is the most feasible way of implementing matrix mu

2条回答
  •  [愿得一人]
    2021-01-06 22:10

    Suppose a matrix with two columns, a path and a value column. You want to get the adjacency matrix P P^t. With 6 paths, you wanna get 36 values or 6x6 matrix that can be done with matrix multiplication.

    I refactor Mikhail example for a path adjacency matrix given some tuple (Path, value). The demo contains 6 paths so the resulting adjacency matrix has dimensions 6x6. It is now in a straight format, but I would like to get it into a matrix formar or crosstab format with 6x6 form.

    --standardSQL
    WITH MatrixA AS (
      SELECT 1 AS p, 2 AS val UNION ALL
      SELECT 2, -3 UNION ALL
      SELECT 3, 4 UNION ALL
      SELECT 4, -1 UNION ALL
      SELECT 5, 0 UNION ALL
      SELECT 6, 2 
    ), MatrixB AS (
      SELECT 1 AS p, -1 AS val UNION ALL
      SELECT 2, 2 UNION ALL
      SELECT 3, 3 UNION ALL
      SELECT 4, 3 UNION ALL
      SELECT 5, 0 UNION ALL
      SELECT 6, 1
    ),
    matrixMultiplication AS
    (
    SELECT a.p AS ap, b.p as bp, SUM(a.val * b.val) val
    FROM MatrixA AS a
    CROSS JOIN MatrixB AS b
    GROUP BY a.p, b.p
    ORDER BY a.p, b.p
    )
    
    --36 elements for the 6x6 PATHS Matrix
    --TODO: how to shape it to 6x6 matrix?
    SELECT * FROM matrixMultiplication
    

    where the shaping question is now here about shaping the straight table into more traditional matrix multiplication format.

提交回复
热议问题