Search code examples
pythonmachine-learningk-meanspca

ValueError: DataFrame constructor not properly called- After fitting a SparsePCA


After fitted a SparsePCA

from sklearn.decomposition import PCA
from sklearn.decomposition import TruncatedSVD
from scipy.sparse import random as sparse_random
from sklearn.decomposition import SparsePCA
from sklearn.random_projection import sparse_random_matrix


pca_a = SparsePCA(n_components=2, random_state=0) #grafico 2d
pca_review = pca_a.fit(R.toarray())

I tried to Convert it into a DataFrame with the following code

pca_review_df = pd.DataFrame(data= pca_review, columns= ['Component1','Component2'])
pca_name_review = pd.concat([pca_review_df, validation[['Kmeans_cluster']]],axis=1)

But i´m getting the following error:

ValueError: DataFrame constructor not properly called!

¿How can i fix it?


Solution

  • Your error occurs in the line

    pca_review_df = pd.DataFrame(data= pca_review, columns= ['Component1','Component2'])
    

    because pca_review is not an array, iterable, or dictionary, it's the SparsePCA object. To get the component values for your dataset, you need to transform the dataset by fitting and transforming (multiplying your data by your PCA projection matrix), like

    pca_review = pca_a.fit_transform(R.toarray())
    

    Then you can construct your dataframes.