我正在使用Google Analytics(分析)并通过Bigquery处理数据,因此我需要执行matrix multiplication。
在Google Cloud中实现矩阵乘法的最可行方法是什么?可以直接在Bigquery中完成吗?
答案 0 :(得分:2)
假设MatrixA是具有以下各列的表:
i, k, value
和MatrixB-模式为
k, j, value
并假设两个表中的k值范围相同:
这将模仿以下矩阵:
Matrix A
2 -3 4
-1 0 2
Matrix B
-1 2 3
0 1 7
1 1 -2
以下用于乘法的代码用于BigQuery标准SQL
#standardSQL
WITH MatrixA AS (
SELECT 1 AS i, 1 AS k, 2 AS val UNION ALL
SELECT 1, 2, -3 UNION ALL
SELECT 1, 3, 4 UNION ALL
SELECT 2, 1, -1 UNION ALL
SELECT 2, 2, 0 UNION ALL
SELECT 2, 3, 2
), MatrixB AS (
SELECT 1 AS k, 1 AS j, -1 AS val UNION ALL
SELECT 1, 2, 2 UNION ALL
SELECT 1, 3, 3 UNION ALL
SELECT 2, 1, 0 UNION ALL
SELECT 2, 2, 1 UNION ALL
SELECT 2, 3, 7 UNION ALL
SELECT 3, 1, 1 UNION ALL
SELECT 3, 2, 1 UNION ALL
SELECT 3, 3, -2
)
SELECT i, j, SUM(a.val * b.val) val
FROM MatrixA AS a
CROSS JOIN MatrixB AS b
WHERE a.k = b.k
GROUP BY i, j
ORDER BY i, j
结果将如下所示
Row i j val
1 1 1 2
2 1 2 5
3 1 3 -23
4 2 1 3
5 2 2 0
6 2 3 -7
代表MatrixA * MatrixB
2 5 -23
3 0 -7
注意:您可以使用
FROM MatrixA AS a
JOIN MatrixB AS b
ON a.k = b.k
代替
FROM MatrixA AS a
CROSS JOIN MatrixB AS b
WHERE a.k = b.k
随您的喜好
答案 1 :(得分:0)
假设一个矩阵有两列,即路径和值列。您要获取邻接矩阵P P^t
。通过6条路径,您想要获得36个值或6x6矩阵,可以通过矩阵乘法来完成。
在给定一些元组(Path, value)
的情况下,我重构了Mikhail示例以用于路径邻接矩阵。该演示包含6条路径,因此生成的邻接矩阵的尺寸为6x6。现在是纯格式,但我想将其转换为6x6形式的矩阵形式或交叉表格式。
--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
现在here的整形问题是将直线表整形为更传统的矩阵乘法格式。