plot_pca_correlation_graph: 绘制原始特征与主成分之间的相关性图
A function to provide a correlation circle for PCA.
> from mlxtend.plotting import plot_pca_correlation_graph
在所谓的相关圆中,原始数据集特征与主成分之间的相关性通过坐标显示。
示例
以下相关性圆例子可视化了前两个主成分与原始虹膜数据集的四个特征之间的相关性。
- 具有正相关的特征将被聚集在一起。
- 完全不相关的特征彼此正交。
- 具有负相关的特征将在该图的相对象限上绘制。
from mlxtend.data import iris_data
from mlxtend.plotting import plot_pca_correlation_graph
import numpy as np
X, y = iris_data()
X_norm = X / X.std(axis=0) # 建议对特征列进行归一化处理。
feature_names = [
'sepal length',
'sepal width',
'petal length',
'petal width']
figure, correlation_matrix = plot_pca_correlation_graph(X_norm,
feature_names,
dimensions=(1, 2),
figure_axis_size=10)
correlation_matrix
Dim 1 | Dim 2 | |
---|---|---|
sepal length | -0.891224 | -0.357352 |
sepal width | 0.449313 | -0.888351 |
petal length | -0.991684 | -0.020247 |
petal width | -0.964996 | -0.062786 |
进一步指出,x轴和y轴上显示的百分比值表示原始数据集中每个主成分轴解释的方差比例。即,如果PC1列出72.7%而PC2列出23.0%如上所示,那么这两个主成分总共解释了95.7%的总方差。
API
plot_pca_correlation_graph(X, variables_names, dimensions=(1, 2), figure_axis_size=6, X_pca=None, explained_variance=None)
Compute the PCA for X and plots the Correlation graph
Parameters
-
X
: 2d array like.The columns represent the different variables and the rows are the samples of thos variables
-
variables_names
: array likeName of the columns (the variables) of X
dimensions: tuple with two elements. dimensions to be plotted (x,y)
figure_axis_size : size of the final frame. The figure created is a square with length and width equal to figure_axis_size.
-
X_pca
: np.ndarray, shape = [n_samples, n_components].Optional.
X_pca
is the matrix of the transformed components from X. If not provided, the function computes PCA automatically using mlxtend.feature_extraction.PrincipalComponentAnalysis Expectedn_componentes >= max(dimensions)
-
explained_variance
: 1 dimension np.ndarray, length = n_componentsOptional.
explained_variance
are the eigenvalues from the diagonalized covariance matrix on the PCA transformatiopn. If not provided, the function computes PCA independently Expectedn_componentes == X.shape[1]
Returns
matplotlib_figure, correlation_matrix
Examples
For usage examples, please see https://rasbt.github.io/mlxtend/user_guide/plotting/plot_pca_correlation_graph/