Principal component analysis in Python

前端 未结 11 605
情深已故
情深已故 2020-11-30 16:49

I\'d like to use principal component analysis (PCA) for dimensionality reduction. Does numpy or scipy already have it, or do I have to roll my own using numpy.linalg.eigh?<

11条回答
  •  猫巷女王i
    2020-11-30 17:22

    You can use sklearn:

    import sklearn.decomposition as deco
    import numpy as np
    
    x = (x - np.mean(x, 0)) / np.std(x, 0) # You need to normalize your data first
    pca = deco.PCA(n_components) # n_components is the components number after reduction
    x_r = pca.fit(x).transform(x)
    print ('explained variance (first %d components): %.2f'%(n_components, sum(pca.explained_variance_ratio_)))
    

提交回复
热议问题