In [13]:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt 

# Modèle : chargement des données
df = sns.load_dataset('titanic') # csv de gitub

print(
    f"The dataset contains {df.shape[0]} samples"
    f" and {df.shape[1]} columns"
)

num_col=df.describe().columns
print("colonnes numériques: ")
for col in num_col:print(col)
'''
The dataset contains 48842 samples and 14 columns
colonnes numériques: 
age
education-num
capital-gain
capital-loss
hours-per-week
'''
cat_col=[col for col in df.columns if col not in num_col]
print("\ncolonnes catégorielles:")
for col in cat_col:print(col)
for col in cat_col: print("\ncol:", df[col].value_counts())


#histogrammes des données numériques 
# 2 histo par lignes, on compte le nb de lignes
# 2*4 pour 7 et 8 cat_col
# len + 1 dans tous les cas, // 2
nbl_histo = (len(num_col) + 1) // 2 
df.hist(figsize=(8 * 2, 4 * nbl_histo)) 


# on fait un df.histo pour les attributs catégoriels
nbl_histo = (len(cat_col) + 1) // 2

data_categoriel, axes = plt.subplots(nbl_histo, 2, figsize=(6 * 2, 4 * nbl_histo))
for icol in range(len(cat_col)):
   column=cat_col[icol]
   i=icol//2
   j=icol-2*i
   ax = axes[i, j]  # Accéder à chaque subplot individuellement
   category_counts = df[column].value_counts()
   category_counts.plot(kind='bar', ax=ax)
   ax.set_title(f"Répartition de la variable '{column}'")
   ax.set_xlabel(f"Valeurs de '{column}'")
   ax.set_ylabel("Nombre d'échantillons")
   ax.grid(True)
    
# pour effacer le dernier histogramme s'il est vide (len(cat_col) = 7)
if len(cat_col) % 2 == 1:
   ax = axes[nbl_histo-1, 1]
   ax.axis("off")
plt.tight_layout()
plt.show()    
The dataset contains 891 samples and 15 columns
colonnes numériques: 
survived
pclass
age
sibsp
parch
fare

colonnes catégorielles:
sex
embarked
class
who
adult_male
deck
embark_town
alive
alone

col: male      577
female    314
Name: sex, dtype: int64

col: S    644
C    168
Q     77
Name: embarked, dtype: int64

col: Third     491
First     216
Second    184
Name: class, dtype: int64

col: man      537
woman    271
child     83
Name: who, dtype: int64

col: True     537
False    354
Name: adult_male, dtype: int64

col: C    59
B    47
D    33
E    32
A    15
F    13
G     4
Name: deck, dtype: int64

col: Southampton    644
Cherbourg      168
Queenstown      77
Name: embark_town, dtype: int64

col: no     549
yes    342
Name: alive, dtype: int64

col: True     537
False    354
Name: alone, dtype: int64
In [ ]: