LoginSignup
2
0

More than 3 years have passed since last update.

Bigqueryで内積計算

Last updated at Posted at 2019-07-19

役に立つときがあるかもしれないので残しておく。

例えば [1, 1, 2] [1, 2, 3] の内積を求める場合こんな感じで出来る。


select
  sum(a.x * b.y)
from
  (select * from unnest([1, 1, 2]) as x with offset as i ) a
join
  (select * from unnest([1, 2, 3]) as y with offset as j ) b
on a.i = b.j

select 部分をこうすれば、2つの配列のcos類似度も出せる。

sum(a.x * b.y) / (sum(a.x * a.x) + sum(b.y * b.y))
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0