pythonpca

Prince column_correlations error: DataFrame is not callable


The code is basic and should function on google colab but I have an error.

import pandas as pd
import prince

# Example data
data = {
    'Feature1': [1, 2, 3, 4, 5],
    'Feature2': [5, 4, 3, 2, 1],
    'Feature3': [2, 3, 4, 5, 6]
}

choco = pd.DataFrame(data)

#Initialisation du modèle PCA de prince
pca = prince.PCA(n_components=2)

#Ajustement du modèle PCA aux données
pca = pca.fit(choco)

#Calcul des corrélations des colonnes avec les composantes principales
correlations = pca.column_correlations(choco)
print(correlations)

The error message is:

---------------------------------------------------------------------------
TypeError Traceback
     17 # Calculate column correlations with the principal components
---> 18 correlations = pca.column_correlations(choco)
     19 print(correlations)

TypeError: 'DataFrame' object is not callable

The dataframe is basic and the type of the data entered should be dataframe and not numpy.

Any idea?

I cannot go around it and use sklearn, it is for educational purposes


Solution

  • The API changed somewhere between 0.7 and 0.10.

    PCA.column_correlations changed from a method, which you need to call, to a property that returns you the DataFrame correctly:

    Old:

        def column_correlations(self, X):
            self._check_is_fitted()
            ...
            return pd.DataFrame({
                component: {
                    feature: row_pc[component].corr(X[feature])
                    for feature in X.columns
                }
                for component in row_pc.columns
            }).sort_index()
    

    New:

        @property
        @utils.check_is_fitted
        def column_correlations(self):
            return self.column_coordinates_.div(self._column_dist**0.5, axis=0)
    

    If you work with different versions of prince you check for if isinstance(pca.column_correlations, pd.DataFrame) # new or callable(pca.column_correlations) # old , alternatively you can also try to check prince.__version__.

    If you work with only the latest one, update to the new property interface and do not call it.