python 主成分分析:图解释了每个成分的方差,以确定最佳的成分数量和转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 主成分分析:图解释了每个成分的方差,以确定最佳的成分数量和转换相关的知识,希望对你有一定的参考价值。

""" PCA """

## PCA: transformation and plot variance vs principal components
def pca_explained_variance(FEATURES):
    print('PCA of %s'%list(FEATURES.columns))
    ## build algorithm
    from sklearn import decomposition
    pca = decomposition.PCA()
    X = FEATURES.as_matrix()
    pca.fit(X)

    ## plot explained variance per componenents
    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(figsize=(6,4))
    y = 100*pca.explained_variance_ratio_
    x = list(range(1,len(y)+1,1))
    plt.plot(x,y, linewidth=2)
    plt.axis('tight')
    plt.xlabel('n_components')
    plt.ylabel('explained_variance_(%)')
    for i, txt in enumerate(['%.2f%s'%(i,'%') for i in y]):
        ax.annotate(txt, (x[i]-0.1,y[i]+2.5))
    plt.show()
    
    # return
    return None

## PCA: transformation and extraction of principal of components
def PCA_transformation(FEATURES,n_selection=None):
	import pandas as pd

	## build algorithm
	from sklearn import decomposition
	pca = decomposition.PCA()
	X = FEATURES.as_matrix()
	pca.fit(X)

	## feature extraction
	if n_selection is None: Xtransform = pca.transform(X)
	else: Xtransform = pca.transform(X)[:,:n_selection] 

	# into DF
	PC = pd.DataFrame(Xtransform)
	lcol_pc = ['pc%s'%i for i in range(1,n_selection+1,1)]
	PC.columns = lcol_pc

	# return
	return [PC,pca]

以上是关于python 主成分分析:图解释了每个成分的方差,以确定最佳的成分数量和转换的主要内容,如果未能解决你的问题,请参考以下文章

SPSS分析中解释的总方差和旋转成分矩阵要怎么进行解释?就是说怎么对这个结果进行说明,然后写进论文里?

主成分分析(Principal components analysis)-最大方差解释

PCA(主成分分析)python实现

R语言plotly可视化:使用PCA算法进行数据降维使用plotly可视化PCA所有的主成分绘制散点图矩阵降维后的两个(三个)核心主成分的二维三维可视化图形方差解释的量载荷图等

特征工程-主成分分析PCA

主成分分析(PCA)