Search code examples
pythonalgorithmplotpcadimensionality-reduction

Plotting an array with size n*512 to the PC components of another array with size n*256


I have an array a with size n*512, I first want to plot it using PCA.

Next, I have another array b with size n*256, I want to plot it on the PCA components obtained above...

How can I do it?


Solution

  • Use sklearn to reduce the dimensionality of the data and then plot it. First fit the PCA model on your first array a and transform a to its two principal components, then transform array b using the PCA model fitted on a. Finally plot the transformed a and b on the same plot:

    import matplotlib.pyplot as plt
    from sklearn.decomposition import PCA
    
    a = 
    b =
    pca = PCA(n_components=2)
    a_transformed = pca.fit_transform(a)
    b_transformed = pca.transform(b)
    plt.scatter(a_transformed[:, 0], a_transformed[:, 1], color='blue', label='a')
    plt.scatter(b_transformed[:, 0], b_transformed[:, 1], color='red', label='b')
    plt.legend()
    plt.show()
    

    I'm maknig the assumption that a and b are numpy arrays.