构造相关矩阵的代码说明

Explanation of code that constructs correlation matrix

我指的是this answer

给定 table 列构造相关矩阵的代码是

u cor/:\:u:flip t 其中 t 是 table.

从右到左阅读,我理解到 u:flip t。我可以要求解释一下其余代码的作用吗?

谢谢

如果你用一个能提供更多视觉输出的东西来代替,比如连接两个向量,应该更容易看到派生函数 cor/:\: 在做什么

q)"123","abc"       // simple join
"123abc"

q)"123",/:"abc"     // join left arg to each item of right arg
"123a"
"123b"
"123c"

q)"123",/:\:"abc"   // join each item of left arg to each item of right
"1a" "1b" "1c"
"2a" "2b" "2c"
"3a" "3b" "3c"

回到cor

的简单例子
q)show t:([]a:3?1.0;b:3?1.0;c:3?1.0)
a         b          c
-------------------------------
0.7935513 0.6377554  0.3573039
0.2037285 0.03845637 0.02547383
0.7757617 0.8972357  0.688089

q)u cor/:\:u:flip t
 | a         b         c
-| -----------------------------
a| 1         0.9474878 0.8529413
b| 0.9474878 1         0.975085
c| 0.8529413 0.975085  1

q)show data:value flip t;           // extract the data for clarity
0.7935513 0.2037285  0.7757617
0.6377554 0.03845637 0.8972357
0.3573039 0.02547383 0.688089

q)cor[data 0;]each data             // first row cor each row
1 0.9474878 0.8529413
q)cor[data 1;]each data             // second row cor each row
0.9474878 1 0.975085
q)cor[data 2;]each data             // last row cor each row
0.8529413 0.975085 1

q){cor[x]each data}each data        // all at once
1         0.9474878 0.8529413
0.9474878 1         0.975085
0.8529413 0.975085  1

q)data cor/:\:data                  // derived function much nicer
1         0.9474878 0.8529413
0.9474878 1         0.975085
0.8529413 0.975085  1

如果您正在研究相关矩阵,那么了解它们是什么可能是个好主意,这可能会为 inputs/outputs/code.

提供一些背景信息

https://www.displayr.com/what-is-a-correlation-matrix/?msclkid=f68768aeab8e11ecbca30d34e2ba880f

在这种情况下,我们正在寻找一些 matrix/table u:flip t 与其自身之间的相关性。

查询的其余部分由函数 cor 和两个 kdb+ 迭代器组成,每个迭代器向右 /: 向左 \:
https://code.kx.com/q/ref/cor/?msclkid=748e645bab8d11ecab715a544f547398 https://code.kx.com/q/wp/iterators/?msclkid=d2172906ab8c11ecac2b46902bbe505d

每个权利将 right-hand 参数的每个项目应用于每个 left-hand 参数

q)1 ,/: 10 20 30
1 10
1 20
1 30

While each left 会将 left-hand 参数的每一项应用于每个 right-hand 参数

q)1 2 3 ,\: 10
1 10
2 10
3 10

如果我们同时使用两者,如下图所示,我们将 , left-hand 列表的每个元素 \: 与 right-hand 列表的每个元素 [=15] =]

q)1 2 3,/:\:10 20 30
1 10  1 20  1 30
2 10  2 20  2 30
3 10  3 20  3 30

那么u cor/:\:u:flip t可以理解为取u中的每一个元素,通过cor/:\:来求出它与u中每一个元素的相关性。