如何在执行 PCA 后绘制每个变量的主向量?
How to plot the pricipal vectors of each variable after performing PCA?
我的问题主要来自于此post
:https://stats.stackexchange.com/questions/53/pca-on-correlation-or-covariance
作者在文章中绘制了每个变量的向量方向和长度。根据我的理解,在执行 PCA 之后。我们得到的只是特征向量和特征值。对于维度为 M x N 的数据集,每个特征值应该是一个 1 x N 的向量。所以,我的问题是向量的长度可能是特征值,但是如何为每个数学变量找到向量的方向?向量长度的物理意义是什么?
此外,如果可能的话,我可以在 python 中使用 scikit PCA 函数做类似的工作吗?
谢谢!
此图称为 biplot,它对理解 PCA 结果非常有用。 向量的长度就是每个 feature/variable 在每个主成分上的值,也就是 PCA 载荷。
示例:
这些加载可通过 print(pca.components_)
访问。使用 Iris 数据集,负载为:
[[ 0.52106591, -0.26934744, 0.5804131 , 0.56485654],
[ 0.37741762, 0.92329566, 0.02449161, 0.06694199],
[-0.71956635, 0.24438178, 0.14212637, 0.63427274],
[-0.26128628, 0.12350962, 0.80144925, -0.52359713]])
这里,每行一台PC,每列对应一台variable/feature。 因此 feature/variable 1,在 PC1 上的值为 0.52106591,在 PC2 上的值为 0.37741762。 这些值用于绘制您在双标图中看到的向量。请参阅下面 Var1
的坐标。正是那些(以上)值!!
最后,要在 python 中创建此图,您可以使用 sklearn
:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
iris = datasets.load_iris()
X = iris.data
y = iris.target
#In general it is a good idea to scale the data
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
pca = PCA()
pca.fit(X,y)
x_new = pca.transform(X)
def myplot(score,coeff,labels=None):
xs = score[:,0]
ys = score[:,1]
n = coeff.shape[0]
plt.scatter(xs ,ys, c = y) #without scaling
for i in range(n):
plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
if labels is None:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color = 'g', ha = 'center', va = 'center')
else:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()
#Call the function.
myplot(x_new[:,0:2], pca.components_.T)
plt.show()
另请参阅此 post:
和
试试 pca library。这将绘制解释方差,并创建双标图。
pip install pca
一个小例子:
from pca import pca
# Initialize to reduce the data up to the number of componentes that explains 95% of the variance.
model = pca(n_components=0.95)
# Or reduce the data towards 2 PCs
model = pca(n_components=2)
# Load example dataset
import pandas as pd
import sklearn
from sklearn.datasets import load_iris
X = pd.DataFrame(data=load_iris().data, columns=load_iris().feature_names, index=load_iris().target)
# Fit transform
results = model.fit_transform(X)
# Plot explained variance
fig, ax = model.plot()
# Scatter first 2 PCs
fig, ax = model.scatter()
# Make biplot with the number of features
fig, ax = model.biplot(n_feat=4)
结果是一个包含许多 PC、负载等统计数据的字典
我的问题主要来自于此post :https://stats.stackexchange.com/questions/53/pca-on-correlation-or-covariance
作者在文章中绘制了每个变量的向量方向和长度。根据我的理解,在执行 PCA 之后。我们得到的只是特征向量和特征值。对于维度为 M x N 的数据集,每个特征值应该是一个 1 x N 的向量。所以,我的问题是向量的长度可能是特征值,但是如何为每个数学变量找到向量的方向?向量长度的物理意义是什么?
此外,如果可能的话,我可以在 python 中使用 scikit PCA 函数做类似的工作吗?
谢谢!
此图称为 biplot,它对理解 PCA 结果非常有用。 向量的长度就是每个 feature/variable 在每个主成分上的值,也就是 PCA 载荷。
示例:
这些加载可通过 print(pca.components_)
访问。使用 Iris 数据集,负载为:
[[ 0.52106591, -0.26934744, 0.5804131 , 0.56485654],
[ 0.37741762, 0.92329566, 0.02449161, 0.06694199],
[-0.71956635, 0.24438178, 0.14212637, 0.63427274],
[-0.26128628, 0.12350962, 0.80144925, -0.52359713]])
这里,每行一台PC,每列对应一台variable/feature。 因此 feature/variable 1,在 PC1 上的值为 0.52106591,在 PC2 上的值为 0.37741762。 这些值用于绘制您在双标图中看到的向量。请参阅下面 Var1
的坐标。正是那些(以上)值!!
最后,要在 python 中创建此图,您可以使用 sklearn
:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
iris = datasets.load_iris()
X = iris.data
y = iris.target
#In general it is a good idea to scale the data
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
pca = PCA()
pca.fit(X,y)
x_new = pca.transform(X)
def myplot(score,coeff,labels=None):
xs = score[:,0]
ys = score[:,1]
n = coeff.shape[0]
plt.scatter(xs ,ys, c = y) #without scaling
for i in range(n):
plt.arrow(0, 0, coeff[i,0], coeff[i,1],color = 'r',alpha = 0.5)
if labels is None:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, "Var"+str(i+1), color = 'g', ha = 'center', va = 'center')
else:
plt.text(coeff[i,0]* 1.15, coeff[i,1] * 1.15, labels[i], color = 'g', ha = 'center', va = 'center')
plt.xlabel("PC{}".format(1))
plt.ylabel("PC{}".format(2))
plt.grid()
#Call the function.
myplot(x_new[:,0:2], pca.components_.T)
plt.show()
另请参阅此 post:
和
试试 pca library。这将绘制解释方差,并创建双标图。
pip install pca
一个小例子:
from pca import pca
# Initialize to reduce the data up to the number of componentes that explains 95% of the variance.
model = pca(n_components=0.95)
# Or reduce the data towards 2 PCs
model = pca(n_components=2)
# Load example dataset
import pandas as pd
import sklearn
from sklearn.datasets import load_iris
X = pd.DataFrame(data=load_iris().data, columns=load_iris().feature_names, index=load_iris().target)
# Fit transform
results = model.fit_transform(X)
# Plot explained variance
fig, ax = model.plot()
# Scatter first 2 PCs
fig, ax = model.scatter()
# Make biplot with the number of features
fig, ax = model.biplot(n_feat=4)
结果是一个包含许多 PC、负载等统计数据的字典