Spaces:
Sleeping
Sleeping
new
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import scipy.stats as stats
|
|
7 |
import statsmodels.api as sm
|
8 |
import statsmodels.formula.api as smf
|
9 |
from sklearn.cluster import KMeans
|
|
|
10 |
from statsmodels.stats.multicomp import pairwise_tukeyhsd
|
11 |
|
12 |
# 🏠 Titre de l'application
|
@@ -43,11 +44,17 @@ if uploaded_file is not None:
|
|
43 |
# 🔹 Test de normalité des résidus (Shapiro-Wilk)
|
44 |
model = smf.ols('Rating ~ C(Product_line) * C(Payment)', data=data).fit()
|
45 |
residuals = model.resid
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
st.write(f"✅ Test de Shapiro-Wilk (Normalité) : **p-value = {shapiro_test.pvalue:.4f}**")
|
48 |
|
49 |
# 🔹 Test d'homogénéité des variances (Levene)
|
50 |
-
group_list = [
|
51 |
levene_test = stats.levene(*group_list)
|
52 |
st.write(f"✅ Test de Levene (Homogénéité des variances) : **p-value = {levene_test.pvalue:.4f}**")
|
53 |
|
@@ -64,8 +71,12 @@ if uploaded_file is not None:
|
|
64 |
# ============================
|
65 |
|
66 |
st.subheader("📌 Comparaisons Post-Hoc (Tukey HSD)")
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
# ============================
|
71 |
# 📊 Visualisation des Résultats
|
@@ -98,8 +109,11 @@ if uploaded_file is not None:
|
|
98 |
# ============================
|
99 |
|
100 |
st.subheader("🎯 Clustering des Clients (K-Means)")
|
|
|
|
|
|
|
101 |
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
|
102 |
-
data['Cluster'] = kmeans.fit_predict(data[['Rating']])
|
103 |
|
104 |
# 🔹 Visualisation du Clustering
|
105 |
fig, ax = plt.subplots(figsize=(8, 5))
|
|
|
7 |
import statsmodels.api as sm
|
8 |
import statsmodels.formula.api as smf
|
9 |
from sklearn.cluster import KMeans
|
10 |
+
from sklearn.preprocessing import LabelEncoder
|
11 |
from statsmodels.stats.multicomp import pairwise_tukeyhsd
|
12 |
|
13 |
# 🏠 Titre de l'application
|
|
|
44 |
# 🔹 Test de normalité des résidus (Shapiro-Wilk)
|
45 |
model = smf.ols('Rating ~ C(Product_line) * C(Payment)', data=data).fit()
|
46 |
residuals = model.resid
|
47 |
+
|
48 |
+
if len(residuals) > 5000:
|
49 |
+
residuals_sample = residuals.sample(5000, random_state=42)
|
50 |
+
else:
|
51 |
+
residuals_sample = residuals
|
52 |
+
|
53 |
+
shapiro_test = stats.shapiro(residuals_sample)
|
54 |
st.write(f"✅ Test de Shapiro-Wilk (Normalité) : **p-value = {shapiro_test.pvalue:.4f}**")
|
55 |
|
56 |
# 🔹 Test d'homogénéité des variances (Levene)
|
57 |
+
group_list = [group.dropna().values for _, group in data.groupby('Product_line')['Rating']]
|
58 |
levene_test = stats.levene(*group_list)
|
59 |
st.write(f"✅ Test de Levene (Homogénéité des variances) : **p-value = {levene_test.pvalue:.4f}**")
|
60 |
|
|
|
71 |
# ============================
|
72 |
|
73 |
st.subheader("📌 Comparaisons Post-Hoc (Tukey HSD)")
|
74 |
+
|
75 |
+
if np.issubdtype(data['Rating'].dtype, np.number):
|
76 |
+
tukey = pairwise_tukeyhsd(data['Rating'], data['Product_line'])
|
77 |
+
st.write(tukey.summary())
|
78 |
+
else:
|
79 |
+
st.error("Erreur : La colonne 'Rating' doit être numérique pour le test de Tukey.")
|
80 |
|
81 |
# ============================
|
82 |
# 📊 Visualisation des Résultats
|
|
|
109 |
# ============================
|
110 |
|
111 |
st.subheader("🎯 Clustering des Clients (K-Means)")
|
112 |
+
|
113 |
+
encoder = LabelEncoder()
|
114 |
+
data['Product_line_encoded'] = encoder.fit_transform(data['Product_line'])
|
115 |
kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
|
116 |
+
data['Cluster'] = kmeans.fit_predict(data[['Rating', 'Product_line_encoded']])
|
117 |
|
118 |
# 🔹 Visualisation du Clustering
|
119 |
fig, ax = plt.subplots(figsize=(8, 5))
|