文档来源: Correspondence analysis
参考:
对应分析的实质(理论很复杂,但是结果很明了简单)
对应分析的局限性
数据要求: 列联表的形式
需要先做卡方检验: Pearson卡方检验Sig.<0.05证明两个变量并不是没有关联的,并不是完全独立的 在做对应分析
对应分析图的正确解释:
随笔记python实现对应分析:
import prince
dataset = prince.datasets.load_french_elections()
dataset[['Le Pen', 'Macron', 'Mélenchon', 'Abstention']].head()
# 拟合
ca = prince.CA(
n_components=3,
n_iter=3,
copy=True,
check_input=True,
engine='sklearn',
random_state=42
)
ca = ca.fit(dataset)
数据样例可见:
其中:
prince.CA
一定需要指定engine='sklearn'
;n_components
是降维维度数量,如果你只有两列,只能降低到1维其他:
# Eigenvalues
ca.eigenvalues_summary
# 看最后的row维度的降维
ca.row_coordinates(dataset).head()
# 看最后的columns维度的降维
ca.column_coordinates(dataset).head()
可视化
ax1 = ca.plot(
dataset,
x_component=0,
y_component=1,
show_row_markers=True,
show_column_markers=True,
show_row_labels=False,
show_column_labels=False
)
ax1.save('ax1.html')
此时可以可视化并保存成Html