1

I would like to plot a graph using python showing variables on X/Y axis after applying MCA. I tried this code but returns the following error:

AttributeError: 'DataFrame' object has no attribute 'plot_coordinates'

This is the code:

ax = mca.plot_coordinates(
X=X,
ax=None,
figsize=(6, 6),
show_row_points=True,
row_points_size=10,
show_row_labels=False,
show_column_points=True,
column_points_size=30,
show_column_labels=False,
legend_n_cols=1)

Can anyone help please?

Thanks,

3
  • 1
    What is mca? A pandas.DataFrame object? The error is accurate; pandas.DataFrame doesn't have an attribute plot_coordinates. Commented Aug 29, 2019 at 9:06
  • mca stands for multiple correspondence analysis. I have a dataframe named 'train' with many columns and I applied the mca on it using mca = mca.fit(train). This gives the principal components of the mca then I would like to plot the train variables on (x,y) graph while x is the 1st component and y is the second Commented Aug 29, 2019 at 9:13
  • 1
    It's always nice to provide a complete working example. In other cases, people are forced to guess. So, if we have to follow this route, what is the output of type(mca)? Commented Aug 29, 2019 at 10:54

2 Answers 2

1
mca = prince.MCA()

typeof mca is prince.mca.MCA ,it has the method . if you add the code,

mca = mca.fit(X) # same as calling ca.fs_r(1)
mca = mca.transform(X)

type of mca is DataFrame.it do not have the method plot_coordinates. The right code:

import pandas as pd 
import prince

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

print(X.head())

mca = prince.MCA()

mca = mca.fit(X) # same as calling ca.fs_r(1)
mca1 = mca.transform(X) # same as calling ca.fs_r_sup(df_new) for *another* test set.
print(mca1)

ax = mca.plot_coordinates(
     X=X,
     ax=None,
     figsize=(6, 6),
     show_row_points=True,
     row_points_size=10,
     show_row_labels=False,
     show_column_points=True,
     column_points_size=30,
     show_column_labels=False,
     legend_n_cols=1
     )

ax.get_figure().savefig('./mca_coordinates.svg')
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know if this helps but I ran into that same error. To fix it, I specified the X= to be the exact dataframe and columns/variables and it fixed the problem: X=train[variables] instead of X=X

ax = mca.plot_coordinates(
X=train[variables],
ax=None,
figsize=(6, 6),
show_row_points=True,
row_points_size=10,
show_row_labels=False,
show_column_points=True,
column_points_size=30,
show_column_labels=False,
legend_n_cols=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.