Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import plotly.express as px
|
5 |
+
from sklearn.cluster import KMeans, AgglomerativeClustering, SpectralClustering
|
6 |
+
from sklearn.preprocessing import StandardScaler
|
7 |
+
from sklearn.decomposition import PCA
|
8 |
+
|
9 |
+
def perform_clustering(data_file, model_type, n_clusters):
|
10 |
+
# Lire les données
|
11 |
+
df = pd.read_csv(data_file.name)
|
12 |
+
|
13 |
+
# Nettoyage supplémentaire au cas où (suppression des NaN)
|
14 |
+
df = df.dropna()
|
15 |
+
|
16 |
+
# Standardisation des données
|
17 |
+
scaler = StandardScaler()
|
18 |
+
X_scaled = scaler.fit_transform(df)
|
19 |
+
|
20 |
+
# Réduction de dimension pour visualisation (si plus de 2 features)
|
21 |
+
if X_scaled.shape[1] > 2:
|
22 |
+
pca = PCA(n_components=2)
|
23 |
+
X_vis = pca.fit_transform(X_scaled)
|
24 |
+
else:
|
25 |
+
X_vis = X_scaled
|
26 |
+
|
27 |
+
# Application du clustering
|
28 |
+
if model_type == "KMeans":
|
29 |
+
model = KMeans(n_clusters=n_clusters, random_state=42)
|
30 |
+
elif model_type == "CAH":
|
31 |
+
model = AgglomerativeClustering(n_clusters=n_clusters)
|
32 |
+
elif model_type == "Spectral Clustering":
|
33 |
+
model = SpectralClustering(n_clusters=n_clusters, affinity='nearest_neighbors', random_state=42)
|
34 |
+
|
35 |
+
clusters = model.fit_predict(X_scaled)
|
36 |
+
|
37 |
+
# Création du dataframe de résultats
|
38 |
+
results_df = df.copy()
|
39 |
+
results_df['Cluster'] = clusters
|
40 |
+
|
41 |
+
# Visualisation
|
42 |
+
if X_vis.shape[1] >= 2:
|
43 |
+
fig = px.scatter(
|
44 |
+
x=X_vis[:, 0],
|
45 |
+
y=X_vis[:, 1],
|
46 |
+
color=clusters,
|
47 |
+
title=f"Visualisation des clusters ({model_type}, k={n_clusters})",
|
48 |
+
labels={'x': 'Composante 1', 'y': 'Composante 2'},
|
49 |
+
color_continuous_scale=px.colors.qualitative.Plotly
|
50 |
+
)
|
51 |
+
else:
|
52 |
+
fig = px.scatter(
|
53 |
+
x=range(len(X_vis)),
|
54 |
+
y=X_vis[:, 0],
|
55 |
+
color=clusters,
|
56 |
+
title=f"Visualisation des clusters ({model_type}, k={n_clusters})",
|
57 |
+
labels={'x': 'Index', 'y': 'Valeur'},
|
58 |
+
color_continuous_scale=px.colors.qualitative.Plotly
|
59 |
+
)
|
60 |
+
|
61 |
+
return fig, results_df
|
62 |
+
|
63 |
+
# Interface Gradio
|
64 |
+
iface = gr.Interface(
|
65 |
+
fn=perform_clustering,
|
66 |
+
inputs=[
|
67 |
+
gr.File(label="Uploader votre jeu de données (CSV)"),
|
68 |
+
gr.Dropdown(["KMeans", "CAH", "Spectral Clustering"], label="Modèle de clustering", value="KMeans"),
|
69 |
+
gr.Slider(2, 10, step=1, label="Nombre de clusters (k)", value=3)
|
70 |
+
],
|
71 |
+
outputs=[
|
72 |
+
gr.Plot(label="Visualisation des clusters"),
|
73 |
+
gr.Dataframe(label="Données avec labels de cluster")
|
74 |
+
],
|
75 |
+
title="Application de Clustering",
|
76 |
+
description="""Uploader un jeu de données nettoyé, choisissez un modèle de clustering
|
77 |
+
et le nombre de clusters pour obtenir une visualisation et les résultats."""
|
78 |
+
)
|
79 |
+
|
80 |
+
iface.launch()
|