# 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()


• 5
点赞
• 1
评论
• 7
收藏
• 一键三连
• 扫一扫，分享海报

04-15

12-16
11-29
10-16 3891
01-28 1万+
08-20
12-14
03-30
07-02 1611
01-03 1813
04-25 8万+

Artisan.fu

¥2 ¥4 ¥6 ¥10 ¥20