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

6k views Asked by At

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.

2

There are 2 answers

0
user3563929 On BEST ANSWER

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

0
ruben On

The problem is that for the new versions, there's no "plot_coordinates". I've lost few hours to discover that you need to use "plot". Find here the documentation. Example:

mca = prince.MCA(n_components = 2)
mca = mca.fit(df[cols_list])
ax = mca.plot(df[cols_list])
ax