File size: 4,692 Bytes
adf12c1
 
 
 
 
 
 
 
 
9747b16
adf12c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edbe41b
adf12c1
 
 
 
 
 
 
 
 
 
 
 
 
 
9747b16
 
edbe41b
9747b16
 
 
 
adf12c1
 
 
9747b16
adf12c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9747b16
edbe41b
9747b16
 
 
edbe41b
adf12c1
 
 
 
 
 
 
 
 
 
 
 
 
 
edbe41b
adf12c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9747b16
 
 
adf12c1
9747b16
adf12c1
 
 
edbe41b
 
adf12c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import streamlit as st
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import statsmodels.api as sm
import statsmodels.formula.api as smf
from sklearn.cluster import KMeans
from sklearn.preprocessing import LabelEncoder
from statsmodels.stats.multicomp import pairwise_tukeyhsd

# 🏠 Titre de l'application
st.title("📊 Analyse des Évaluations des Clients avec ANOVA")

# 📂 Upload du fichier
uploaded_file = st.file_uploader("📂 Téléchargez le fichier 'supermarket_sales.csv'", type=["csv"])

if uploaded_file is not None:
    # 📖 Charger les données
    data = pd.read_csv(uploaded_file)

    # ✅ Renommer les colonnes pour éviter les erreurs de syntaxe
    data = data.rename(columns={'Product line': 'Product_line'})

    # ✅ Sélectionner les colonnes nécessaires
    data = data[['Product_line', 'Payment', 'Rating']]
    data.dropna(inplace=True)  # Supprimer les valeurs manquantes

    # ✅ Convertir en catégories
    data['Product_line'] = data['Product_line'].astype('category')
    data['Payment'] = data['Payment'].astype('category')
    data['Rating'] = pd.to_numeric(data['Rating'], errors='coerce')  # Convertir en numérique

    # 📌 Afficher un aperçu des données
    st.subheader("📊 Aperçu des Données")
    st.write(data.head())

    # ============================
    # 📌 Vérification des Hypothèses
    # ============================

    st.subheader("🧪 Vérification des Hypothèses")

    # 🔹 Test de normalité des résidus (Shapiro-Wilk)
    model = smf.ols('Rating ~ C(Product_line) * C(Payment)', data=data).fit()
    residuals = model.resid

    if len(residuals) > 5000:
        residuals_sample = pd.Series(residuals).sample(5000, random_state=42)
    else:
        residuals_sample = residuals

    shapiro_test = stats.shapiro(residuals_sample)
    st.write(f"✅ Test de Shapiro-Wilk (Normalité) : **p-value = {shapiro_test.pvalue:.4f}**")

    # 🔹 Test d'homogénéité des variances (Levene)
    group_list = [group.dropna().values for _, group in data.groupby('Product_line')['Rating']]
    levene_test = stats.levene(*group_list)
    st.write(f"✅ Test de Levene (Homogénéité des variances) : **p-value = {levene_test.pvalue:.4f}**")

    # ============================
    # 📌 ANOVA à Deux Facteurs
    # ============================

    st.subheader("📊 ANOVA à Deux Facteurs")
    anova_table = sm.stats.anova_lm(model, typ=2)
    st.write(anova_table)

    # ============================
    # 📌 Comparaisons Post-Hoc (Tukey HSD)
    # ============================

    st.subheader("📌 Comparaisons Post-Hoc (Tukey HSD)")
    
    if data['Rating'].isna().sum() == 0:  # Vérifie qu'il n'y a pas de NaN
        tukey = pairwise_tukeyhsd(data['Rating'], data['Product_line'])
        st.write(tukey.summary())
    else:
        st.error("Erreur : Des valeurs non numériques ont été détectées dans 'Rating'. Vérifiez votre fichier CSV.")

    # ============================
    # 📊 Visualisation des Résultats
    # ============================

    st.subheader("📊 Visualisation des Résultats")

    # 🔹 Boxplot
    fig, ax = plt.subplots(figsize=(10, 5))
    sns.boxplot(x='Product_line', y='Rating', hue='Payment', data=data, ax=ax)
    plt.xticks(rotation=45)
    st.pyplot(fig)

    # 🔹 Heatmap des Moyennes des Évaluations
    mean_ratings = data.groupby(['Product_line', 'Payment'])['Rating'].mean().unstack().fillna(0)
    fig, ax = plt.subplots(figsize=(8, 5))
    sns.heatmap(mean_ratings, annot=True, cmap='coolwarm', ax=ax)
    st.pyplot(fig)

    # ============================
    # 📌 Régression Linéaire Multiple
    # ============================

    st.subheader("📈 Régression Linéaire Multiple")
    lm_model = smf.ols('Rating ~ C(Product_line) + C(Payment)', data=data).fit()
    st.write(lm_model.summary())

    # ============================
    # 📌 Clustering des Clients (K-Means)
    # ============================

    st.subheader("🎯 Clustering des Clients (K-Means)")
    
    encoder = LabelEncoder()
    data['Product_line_encoded'] = encoder.fit_transform(data['Product_line'])
    kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
    data['Cluster'] = kmeans.fit_predict(data[['Rating', 'Product_line_encoded']])

    # 🔹 Visualisation du Clustering
    fig, ax = plt.subplots(figsize=(8, 5))
    sns.scatterplot(x='Product_line_encoded', y='Rating', hue=data['Cluster'].astype(str), palette='viridis', data=data, ax=ax)
    plt.xticks(ticks=range(len(encoder.classes_)), labels=encoder.classes_, rotation=45)
    st.pyplot(fig)