Search code examples
scikit-learnpcamatrix-decomposition

X_transformed_fit_ attribute error: AttributeError: 'KernelPCA' object has no attribute 'X_transformed_fit_'


I am trying to obtain which features in my dataset affects the principal components, and trying to observe how my data fitted in my Kernel PCA algorithm. I tried to use X_transformed_fit_ attribute which exists in documentary but I got this error: AttributeError: 'KernelPCA' object has no attribute 'X_transformed_fit_'

My code for KPCA is below:

from sklearn.decomposition import KernelPCA
kpca = KernelPCA(n_components = 2, kernel = 'cosine', fit_inverse_transform = False)
X = kpca.fit_transform(X)
kpca.X_transformed_fit_

If it is not the way I can obtain how to interpret the composition of my KPCA, then how am I going to understand that these principal components are constructed? The reason why I am investigating is that I will continue to this process with clustering algorithm implementation (K-means, agglomerative HC), and I want to understand the characters of my distinct clusters that will be derived from the algorithms at the end (by understanding the structure of the principal components).


Solution

  • The attribute X_transformed_fit_ is only available when you set the parameter fit_inverse_transform to True.

    Try:

    kpca = KernelPCA(n_components = 2, kernel = 'cosine', fit_inverse_transform = True)
    X = kpca.fit_transform(X)
    kpca.X_transformed_fit_