Search code examples
pythonmca

Obtaining Multiple Correspondence Analysis (MCA) Plot in Python Using Prince Package


I am trying to plot a 2D MCA plot in Python. I am trying to replicate the tutorial found in the Prince Github Repository https://github.com/MaxHalford/prince

I currently have the following working:

import pandas as pd

X = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/balloons/adult+stretch.data')
X.columns = ['Color', 'Size', 'Action', 'Age', 'Inflated']

mca = prince.MCA(X,n_components=2)

However, when I run the plot command, I receive the following error even though there is a plot_coordinates function in the package.

mca.plot_coordinates(X = X)
AttributeError: 'MCA' object has no attribute 'plot_coordinates'

Any assistance to rectify this matter would be appreciated. Thank you.


Solution

  • You would need first initiate the MCA object and fit it with data to use plot_coordinates function.

    X = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/balloons/adult+stretch.data')
    X.columns = ['Color', 'Size', 'Action', 'Age', 'Inflated']
    fig, ax = plt.subplots()
    mc = prince.MCA(n_components=2).fit(X)
    mc.plot_coordinates(X=X, ax=ax)
    ax.set_xlabel('Component 1', fontsize=16)
    ax.set_ylabel('Component 2', fontsize=16)
    

    enter image description here