PCA projection and reconstruction in scikit-learn

前端 未结 2 2020
予麋鹿
予麋鹿 2021-01-30 05:43

I can perform PCA in scikit by code below: X_train has 279180 rows and 104 columns.

from sklearn.decomposition import PCA
pca = PCA(n_components=30)
X_train_pca          


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-30 06:20

    Adding on @eickenberg's post, here is how to do the pca reconstruction of digits' images:

    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.datasets import load_digits
    from sklearn import decomposition
    
    n_components = 10
    image_shape = (8, 8)
    
    digits = load_digits()
    digits = digits.data
    
    n_samples, n_features = digits.shape
    estimator = decomposition.PCA(n_components=n_components, svd_solver='randomized', whiten=True)
    digits_recons = estimator.inverse_transform(estimator.fit_transform(digits))
    
    # show 5 randomly chosen digits and their PCA reconstructions with 10 dominant eigenvectors
    indices = np.random.choice(n_samples, 5, replace=False)
    plt.figure(figsize=(5,2))
    for i in range(len(indices)):
        plt.subplot(1,5,i+1), plt.imshow(np.reshape(digits[indices[i],:], image_shape)), plt.axis('off')
    plt.suptitle('Original', size=25)
    plt.show()
    plt.figure(figsize=(5,2))
    for i in range(len(indices)):
        plt.subplot(1,5,i+1), plt.imshow(np.reshape(digits_recons[indices[i],:], image_shape)), plt.axis('off')
    plt.suptitle('PCA reconstructed'.format(n_components), size=25)
    plt.show()
    

提交回复
热议问题