<jupyter_start><jupyter_text>PCA方法- 函数原型: sklearn.decomposition.PCA(n_components=None, copy=True, whiten=False)- 官方文档: https://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html?highlight=pcasklearn.decomposition.PCA- 相关说明: https://www.jianshu.com/p/8642d5ea5389<jupyter_code>import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

np.random.seed(0)

X = np.linspace(-5, 5, 20)
X = X.reshape((X.shape[0], 1))
X = np.concatenate((X, 2*X), axis=1)
X = X + np.random.normal(loc=0, scale=1, size=X.shape)


# 计算关联系数矩阵
# np.cov中的bias=False表示除以N-1而不是N
R = np.cov(X.T, bias=False)
# print(R)
eigenvalues, eigenvectors = np.linalg.eig(R)
order = eigenvalues.argsort()[::-1]
eigenvalues_order = eigenvalues[order]
eigenvectors = eigenvectors[order]
eigenvalues[1] = -eigenvalues[1]

# PCA训练器
pca = PCA(n_components=2, copy=True, whiten=False)
# 训练并降维
pca.fit(X)
x0 = [pca.components_[:, 0]]
newX = pca.transform(x0)

print(pca.components_)
print()
print(np.matmul(x0, pca.components_*eigenvalues))
print(newX)
# 将降维后数据逆变换回原数据
pca.inverse_transform(newX)
# 各个特征的方差百分比
print()
print(pca.explained_variance_ratio_)
#print(pca.explained_variance_ratio_) # 即特征值百分比
#print(pca.explained_variance_) # 即特征值

newX = pca.transform(X)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.scatter(X[:,0], X[:, 1], color='b')
plt.xlim(xmin=-6, xmax=6)
plt.ylim(ymin=-8, ymax=8)
plt.title('Dimensionality Unreduced')
plt.subplot(1, 2, 2)
plt.scatter(newX[:,0], newX[:, 1], color='r')
plt.xlim(xmin=-10, xmax=10)
plt.ylim(ymin=-7, ymax=7)
plt.title('Dimensionality Reduced')<jupyter_output>[[ 0.41724458  0.90879423]
 [-0.90879423  0.41724458]]

[[1.16076484 0.        ]]
[[-0.96095731 -0.32068398]]

[0.97468327 0.02531673]
{'fraction_non_alphanumeric': 0.17824601366742596, 'fraction_numerical': 0.07630979498861047, 'mean_word_length': 6.084677419354839, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '556308', 'license': 'MIT', 'n_tokens_mistral': 907, 'n_tokens_neox': 844, 'n_words': 120, 'path': '.ipynb_checkpoints/10.2 主成分分析-checkpoint.ipynb', 'repo_name': 'ZDDWLIG/math-model', 'size': '1756'}