使用Prince包在Python中获取多重对应分析(MCA)图

kadbb459  于 2023-03-28  发布在  Python
关注(0)|答案(2)|浏览(635)

我正在尝试用Python绘制2D MCA图。我正在尝试复制Prince Github Repository https://github.com/MaxHalford/prince中的教程
我目前有以下工作:

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)

但是,当我运行plot命令时,即使包中有plot_coordinates函数,我也会收到以下错误。

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

如有任何协助,请纠正此事,不胜感激。谢谢。

myzjeezk

myzjeezk1#

您需要首先初始化MCA对象并将其与数据相匹配,以使用plot_coordinates函数。

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)

wfveoks0

wfveoks02#

问题是,对于新版本,没有“plot_coordinates”。我花了几个小时才发现你需要使用“plot”. Find here the documentation。示例:

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

相关问题