pca.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:pylspm 作者: lseman 项目源码 文件源码
def PCAdo(block, name):
    cor_ = np.corrcoef(block.T)
    eig_vals, eig_vecs = np.linalg.eig(cor_)
    tot = sum(eig_vals)
    var_exp = [(i / tot) * 100 for i in sorted(eig_vals, reverse=True)]
    cum_var_exp = np.cumsum(var_exp)
    loadings = (eig_vecs * np.sqrt(eig_vals))

    eig_vals = np.sort(eig_vals)[::-1]
    print('Eigenvalues')
    print(eig_vals)
    print('Variance Explained')
    print(var_exp)
    print('Total Variance Explained')
    print(cum_var_exp)
    print('Loadings')
    print(abs(loadings[:, 0]))

    PAcorrect = PA(block.shape[0], block.shape[1])

    print('Parallel Analisys')
    pa = (eig_vals - (PAcorrect - 1))
    print(pa)

    print('Correlation Matrix')
    print(pd.DataFrame.corr(block))

    plt.plot(range(1,len(pa)+1), pa, '-o')
    plt.grid(True)
    plt.xlabel('Fatores')
    plt.ylabel('Componentes')

    plt.savefig('imgs/PCA' + name, bbox_inches='tight')
    plt.clf()
    plt.cla()
#    plt.show()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号