No account yet ?
Multiple Correspondence Analysis (MCA) generalizes Correspondence Factor Analysis (FCA) to any number of variables and therefore allows the response modalities of more than two variables to be represented on the same mapping.
As with Principal Component Analysis (PCA), the purpose of these analyzes is to identify hidden dimensions contained in the responses to the selected variables to facilitate the interpretation of tables that are not always readable at the start.
The MCA starts from a complete disjunctive table (burt table) which presents the individuals in rows and in columns all the modalities of the qualitative variables retained.
The intersection boxes contain the value 1 if the individual meets the criterion in the column and 0 otherwise.
As in PCA, the first two axes provide a generally important part of the information contained in the initial table, the horizontal axis being, by convention, the most significant.
The proximity of the points provides information, a priori, on their associations.
The arrangement of the modalities of each variable in relation to each other helps to give meaning to each axis.
Multiple Correspondence Analysis (MCA) in Python
titanic.csv
"Class","Sex","Age","Survived"
"1st","Male","Adult","No"
"3rd","Male","Adult","Yes"
"Crew","Male","Adult","No"
"Crew","Male","Adult","Yes"
"2nd","Male","Adult","No"
"3rd","Male","Adult","No"
"2nd","Male","Adult","No"
"Crew","Male","Adult","No"
"Crew","Male","Adult","No"
"2nd","Male","Child","Yes"
"1st","Male","Adult","Yes"
"Crew","Male","Adult","Yes"
"3rd","Female","Child","No"
"Crew","Male","Adult","No"
"Crew","Male","Adult","No"
"1st","Male","Adult","No"
"1st","Female","Adult","Yes"
"Crew","Male","Adult","No"
"Crew","Male","Adult","No"
"1st","Female","Adult","Yes"
"3rd","Female","Adult","Yes"
"2nd","Male","Adult","No"
"Crew","Male","Adult","No"
"Crew","Male","Adult","No"
"Crew","Male","Adult","Yes"
"Crew","Male","Adult","Yes"
"3rd","Female","Adult","Yes"
"Crew","Male","Adult","No"
"Crew","Male","Adult","Yes"
...
Tested in Anaconda and Python 3.7
# -*- coding: utf-8 -*- """ Created on Wed Jun 22 13:16:15 2022 @author: Ricore """ #Librairies utilisées import pandas import numpy import matplotlib.pyplot as plt import seaborn seaborn.set_style("white") from prince import MCA #Données utilisées df = pandas.read_csv("titanic.csv") df.head() print(df) #Calcul de ACM mca = MCA(n_components = 10) mca.fit(df) print(mca) #Valeurs propres print(mca.eigenvalues_) print(mca.total_inertia_) print(mca.explained_inertia_) eig = pandas.DataFrame( { "Dimension" : ["Dim" + str(x + 1) for x in range(10)], "Valeur propre": mca.eigenvalues_, "% variance expliquée": numpy.round(mca.explained_inertia_, 4) * 100, "% variance expliquée cumulée": numpy.round(numpy.cumsum(mca.explained_inertia_), 4) * 100, } ) print(eig) #Représentation des individus df_ind = pandas.DataFrame(mca.row_coordinates(df)).rename(columns = {i: "Dim"+str(i+1) for i in range(10)}) df_ind.head() print(df_ind) g_ind = seaborn.lmplot(x = "Dim1", y = "Dim2", data = df_ind, fit_reg = False, height = 4, aspect = 3) g_ind.fig.suptitle("Modalités en lignes") plt.show() #Représentation des variables df_var = pandas.DataFrame(mca.column_coordinates(df)).rename(columns = {i: "Dim"+str(i+1) for i in range(10)}) print(df_var) g_var = seaborn.lmplot(x = "Dim1", y = "Dim2", data = df_var, fit_reg = False, height = 4, aspect = 3) g_var.fig.suptitle("Modalités en colonnes") for i in df_var.index: plt.text(df_var.loc[i].Dim1, df_var.loc[i].Dim2, i, size = "xx-large") plt.show() #Représentation simultanée mca.plot_coordinates(df, figsize=(16, 8)) plt.show() fig = plt.figure(figsize = (16,8)) g_simult = seaborn.lmplot(x = "Dim1", y = "Dim2", data = df_ind, fit_reg = False, height = 4, aspect = 3) for i in df_var.index: plt.scatter(df_var.loc[i].Dim1, df_var.loc[i].Dim2, alpha = .25, c = "black") plt.text(df_var.loc[i].Dim1, df_var.loc[i].Dim2, i, size = "xx-large", color = "darkred", ha = "center") g_simult.fig.suptitle("Représentation conjointe") plt.show()
Analyse des Correspondances Multiples (ACM) - Mastère ESD - Introduction au Machine Learning - GitHub
Class Sex Age Survived
0 1st Male Adult No
1 3rd Male Adult Yes
2 Crew Male Adult No
3 Crew Male Adult Yes
4 2nd Male Adult No
... ... ... ...
2196 Crew Male Adult Yes
2197 3rd Male Adult Yes
2198 Crew Male Adult No
2199 3rd Male Adult Yes
2200 3rd Male Adult No
[2201 rows x 4 columns]
MCA(n_components=10)
[0.4450794730527672, 0.3050437322075411, 0.2500060010969506, 0.20503730575469145, 0.17851515983455574, 0.11631832805349443, 1.469148030108288e-33, 3.1440898071945182e-34, 9.419089386194459e-35, 1.945831756535725e-35]
1.5
[0.2967196487018448, 0.20336248813836075, 0.16667066739796707, 0.1366915371697943, 0.1190101065563705, 0.07754555203566295, 9.794320200721919e-34, 2.0960598714630123e-34, 6.27939292412964e-35, 1.2972211710238166e-35]
Dimension Valeur propre % variance expliquée % variance expliquée cumulée
0 Dim1 4.450795e-01 29.67 29.67
1 Dim2 3.050437e-01 20.34 50.01
2 Dim3 2.500060e-01 16.67 66.68
3 Dim4 2.050373e-01 13.67 80.34
4 Dim5 1.785152e-01 11.90 92.25
5 Dim6 1.163183e-01 7.75 100.00
6 Dim7 1.469148e-33 0.00 100.00
7 Dim8 3.144090e-34 0.00 100.00
8 Dim9 9.419089e-35 0.00 100.00
9 Dim10 1.945832e-35 0.00 100.00
Dim1 Dim2 Dim3 ... Dim8 Dim9 Dim10
0 0.055104 -0.541784 -0.446235 ... 0.004187 -0.586546 0.151987
1 0.263386 0.233400 -0.324961 ... 0.004187 -0.586546 0.151987
2 -0.652721 -0.202892 0.039900 ... 0.004187 -0.586546 0.151987
3 -0.061709 -0.469458 0.045706 ... 0.004187 -0.586546 0.151987
4 -0.132518 0.129916 1.194787 ... 0.004187 -0.586546 0.151987
... ... ... ... ... ... ...
2196 -0.061709 -0.469458 0.045706 ... 0.004187 -0.586546 0.151987
2197 0.263386 0.233400 -0.324961 ... 0.004187 -0.586546 0.151987
2198 -0.652721 -0.202892 0.039900 ... 0.004187 -0.586546 0.151987
2199 0.263386 0.233400 -0.324961 ... 0.004187 -0.586546 0.151987
2200 -0.327626 0.499967 -0.330767 ... 0.004187 -0.586546 0.151987
[2201 rows x 10 columns]
Dim1 Dim2 Dim3 ... Dim8 Dim9 Dim10
Class_1st 1.151941 -1.231418 -0.890008 ... -0.326666 0.645533 0.122162
Class_2nd 0.651259 0.252522 2.392076 ... -0.326666 0.645533 0.122162
Class_3rd 0.130599 1.070050 -0.659068 ... -0.326666 0.645533 0.122162
Class_Crew -0.736941 -0.482727 0.082275 ... -0.326666 0.645533 0.122162
Sex_Female 1.574794 0.008927 -0.009280 ... -0.326666 0.645533 0.122162
Sex_Male -0.427587 -0.002424 0.002520 ... -0.326666 0.645533 0.122162
Age_Adult -0.067828 -0.153321 -0.001242 ... -0.326666 0.645533 0.122162
Age_Child 1.301802 2.942646 0.023828 ... -0.326666 0.645533 0.122162
Survived_No -0.509477 0.190238 -0.003751 ... -0.326666 0.645533 0.122162
Survived_Yes 1.067680 -0.398669 0.007861 ... -0.326666 0.645533 0.122162
[10 rows x 10 columns]
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License