sklearn规范相关分析(CCA)的异常行为

时间:2019-06-20 11:22:29

标签: scikit-learn statistics linear-algebra

给定两个视图矩阵XY,CCA应该返回n_componentsX's向量中具有最大相关性的元素的Y's线性组合,即第一个成分具有最大的相关性,第二个成分在与第一个成分不相关的方向上具有第二大相关性,依此类推。但是,在下面的代码中,第二列显然更少与第三列相关:

from sklearn.cross_decomposition import CCA
import numpy as np
from scipy import stats

X = [[0.006061109337620652, 0.0392466675239141, -0.04312459861840733], [-0.6652995467596429, 0.2076410843346226, -0.7817536882379651], [-1.4060868112838942, -0.055029478343267685, -1.006415484608637], [-2.170613455952169, -0.15770102997315535, -1.5223958036356375], [-2.895702070412092, -0.20498481413822175, -1.8232022285963847], [-3.687452614812402, -0.543003880524402, -2.2952030829468533], [-4.206168972149556, -0.3365693935548624, -2.841946351795423], [-5.231288268781064, -0.8024321344988571, -3.40532581256557], [-6.095805742721522, -0.9381144689340173, -3.593752186094848], [-6.297988264542059, -0.7231985020991631, -3.9435579269998406], [-7.10897027952524, -0.8639925998765747, -4.264992629284153], [-8.116238092376772, -1.0123970020855437, -4.96858178622968], [-8.969468878952105, -1.0235782019578692, -5.617282941713933], [-9.839359511108077, -1.2819621078971968, -5.8901943190245625], [-10.181936322525571, -0.9904671991812529, -6.240811384647836]]
Y = [[0.032927114749911154, 0.21320841666565743, -0.23427536580450153], [1.431742605643286, 0.23963850202268067, 0.8438745303679628], [2.908798834568648, 0.7357229001312737, 1.325345683629048], [4.438824821921929, 0.9473643810538429, 2.35038560647864], [5.887201894166226, 1.0302756424934638, 2.964806513433767], [7.409049064480012, 1.3070946380395154, 4.347473875547982], [8.51501831350366, 1.3380108570442941, 4.9533251686263275], [10.57244384646805, 2.31627294094068, 6.028949244604159], [12.22872203222364, 2.1165257564864675, 6.923464021607424], [12.664660419747504, 1.8911363532121173, 7.398432173930664], [14.29235367239137, 2.2098221962551343, 8.000538342827351], [16.327977920399373, 2.643183255720207, 9.257671785118596], [18.081288169620517, 2.968898443090926, 10.221747267811098], [19.754046559146662, 3.051682253577557, 11.244435627784393], [20.466418131910004, 2.644933083198568, 11.752014917896375]]

cca = CCA(n_components=3)
cca.fit(X, Y)
X_transformed, Y_transformed = cca.transform(X, Y)

print(X_transformed)

[[ 1.64277244  0.08237031  0.11724683]
 [ 1.41457457 -0.11600721  0.07162219]
 [ 1.18686358  0.00666119  0.08822118]
 [ 0.94070119 -0.02504267 -0.0112954 ]
 [ 0.71314666  0.02919558  0.25990473]
 [ 0.46246267  0.05607036 -0.16424275]
 [ 0.28625435 -0.09546609 -0.02850206]
 [-0.03644528 -0.03407977 -0.55790786]
 [-0.30127324  0.12266269 -0.12622283]
 [-0.37581414 -0.01941656 -0.0343278 ]
 [-0.62900674  0.05973748  0.13448604]
 [-0.95400947  0.0082079  -0.05487306]
 [-1.23214839 -0.07548718 -0.08864002]
 [-1.50031366  0.05776429  0.00665019]
 [-1.61776455 -0.0571703   0.38788062]]

print(Y_transformed)

[[ 1.64131294  0.01428169  0.11343087]
 [ 1.41330957 -0.06169376  0.06929115]
 [ 1.18580457  0.04946041  0.0853499 ]
 [ 0.9398609   0.01613582 -0.01092778]
 [ 0.71251056  0.06356946  0.25144578]
 [ 0.46205587 -0.05371303 -0.15889725]
 [ 0.28599564 -0.05298207 -0.02757442]
 [-0.03641563  0.02220818 -0.53975   ]
 [-0.30099795  0.01004611 -0.12211474]
 [-0.37547807 -0.063071   -0.03321056]
 [-0.62844374  0.02679924  0.13010901]
 [-0.95315959  0.0185694  -0.05308714]
 [-1.23105629  0.03726576 -0.08575511]
 [-1.49897395  0.01483407  0.00643375]
 [-1.61632484 -0.04171028  0.37525653]]

X_transfomedY_transformed的第三列之间的相关性约为1,而第二列之间的相关性仅为0.389。当我使用n_components=2运行CCA时,我得到第一列和第二列,而我应该获得第一列和第三列。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是因为数据是线性相关的,所以观察到退化的分量。 换句话说,您在Y中有一列等于X中的一列,这将给出条件很差的协方差矩阵,CCA算法正试图对它进行逆运算。

我猜在现实世界中,添加噪声实际上会破坏这种对称性。在您的示例中,您可以使用数据集之一的任何非线性映射来破坏对称性,例如通过对Y求平方:

cca.fit(X, np.asarray(Y)**2)
X_transformed, Y_transformed = cca.transform(X, np.asarray(Y)**2)

现在,我们可以看到CCA正确地找到了CC对,并且每对之间的相关系数都减小了(有关如何从sklearn的CCA中获得分数的信息,请参见我的答案here

np.corrcoef(X_transformed, Y_transformed, rowvar=False).diagonal(3)

# output: array([0.98010177, 0.97194272, 0.26594686])