CCA(典型相关分析)

CCA(典型相关分析)

随机建立数据,对两组数据进行典型相关分析(CCA)

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_decomposition import  CCA

#设置随机种子
np.random.seed(0)
n = 500
l1 = np.random.normal(size=n)
l2 = np.random.normal(size=n)
# print(l1.shape, l2.shape)
latents = np.array([l1, l1, l2, l2]).T
# print(latents.shape)
#加噪处理
X = latents + np.random.normal(size=4 * n).reshape((n, 4))
Y = latents + np.random.normal(size=4 * n).reshape((n, 4))
print(X.shape)
#划分数据集
X_train = X[:n // 2]
Y_train = Y[:n // 2]
X_test = X[n // 2:]
Y_test = Y[n // 2:]
# print(X_train.shape)
# print(Y_train.shape)
# print(X_test.shape)
# print(Y_test.shape)
# print(X.T.shape)
# 打印相关矩阵
#保留小数点后2位
print("Corr(X)")
print(np.round(np.corrcoef(X.T), 2))
print("Corr(Y)")
print(np.round(np.corrcoef(Y.T), 2))
#建立模型
cca = CCA(n_components=2)
#训练数据
cca.fit(X_train, Y_train)
#降维操作
X_train_r, Y_train_r = cca.transform(X_train, Y_train)
# print(X_train_r.shape, Y_train_r.shape)
# print(X_train_r[:, 1].shape)
X_test_r, Y_test_r = cca.transform(X_test, Y_test)
print('test corr = %.2f' % np.corrcoef(X_test_r[:, 1], Y_test_r[:, 1])[0, 1])
# print(X_test_r.shape, Y_test_r.shape)
# print(X_test_r[:, 1].shape)
#画散点图
plt.figure('CCA', facecolor='lightgray')
plt.title('CCA', fontsize=16)
plt.scatter(X_train_r[:, 1], Y_train_r[:, 1], label="train_data",
            marker="o", c="dodgerblue", s=25, alpha=0.8)
plt.scatter(X_test_r[:, 1], Y_test_r[:, 1], label="test_data",
            marker="o", c="orangered", s=25, alpha=0.8)
plt.xlabel("x scores")
plt.ylabel("y scores")
plt.title('X vs Y (test corr = %.2f)' %
          np.corrcoef(X_test_r[:, 1], Y_test_r[:, 1])[0, 1])
plt.xticks(())
plt.yticks(())
plt.legend()
plt.tight_layout()
plt.show()

在这里插入图片描述

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
CCACanonical Correlation Analysis)典型相关分析是一种多元统计分析方法,用于研究两组变量之间的线性关系。其基本思想是将两组变量通过线性变换映射到低维空间中,使得两组变量在该空间中的相关性最大。具体来说,CCA通过构造Lagrangian等式,利用拉格朗日乘子法求解出两组变量的典型相关变量,即两组变量在低维空间中的投影向量,从而得到它们之间的典型相关系数。典型相关系数越大,说明两组变量之间的相关性越强。 在实际应用中,CCA可以用于数据降维、特征提取、模式识别等领域。例如,在故障检测中,可以利用CCA来分析传感器数据和故障模式之间的关系,从而实现故障检测和诊断。 代码示例: ```python import numpy as np from scipy.linalg import eig # 构造两组变量X和Y X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Y = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]]) # 计算X和Y的协方差矩阵 Cxx = np.cov(X.T) Cyy = np.cov(Y.T) Cxy = np.cov(X.T, Y.T) # 计算广义特征值和广义特征向量 eigvals, eigvecs = eig(np.dot(np.dot(np.linalg.inv(Cxx), Cxy), np.dot(np.linalg.inv(Cyy), Cxy.T))) # 取前k个最大的广义特征值对应的广义特征向量 k = 2 idx = np.argsort(eigvals)[::-1][:k] Wx = eigvecs[:, idx].real Wy = np.dot(np.dot(np.linalg.inv(Cyy), Cxy.T), Wx).real # 计算典型相关变量 U = np.dot(X, Wx) V = np.dot(Y, Wy) # 计算典型相关系数 R = np.corrcoef(U.T, V.T)[k:, :k] print("典型相关系数:", R) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值