File size: 7,128 Bytes
3a61454
 
 
 
 
 
b1e8012
d0ec537
b1e8012
 
1d486bb
 
 
b1e8012
1d486bb
 
d0ec537
1d486bb
 
 
 
 
b1e8012
1d486bb
 
 
 
 
 
 
 
 
 
 
b1e8012
ad2c4e2
 
1d486bb
 
2644c4d
1d486bb
 
ad2c4e2
 
1d486bb
 
 
 
a9345e7
1d486bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9345e7
1d486bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8384234
1d486bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9345e7
1d486bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42fa5c8
 
ad2c4e2
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, confusion_matrix
import plotly.express as px
import plotly.graph_objects as go

# Configuration de la page
st.set_page_config(layout="wide", page_title="ML Dashboard")

# Fonction pour charger les données
@st.cache_data
def load_data(file):
	data = pd.read_csv(file)
	return data

# Fonction pour entraîner les modèles
def train_model(X_train, y_train, model_name):
	models = {
    	"Logistic Regression": LogisticRegression(),
    	"Decision Tree": DecisionTreeClassifier(),
    	"Random Forest": RandomForestClassifier(),
    	"Gradient Boost": GradientBoostingClassifier()
	}
	model = models[model_name]
	model.fit(X_train, y_train)
	return model

def app():
    # Sidebar pour la navigation
st.sidebar.title("Navigation")
page = st.sidebar.radio("", ["📊 Vue d'ensemble", "🎯 Prédiction", "🔍 Interprétation", "⚙️ Entraînement"])

# Charger les données par défaut
if 'data' not in st.session_state:
    try:
        st.session_state.data = load_data('exported_named_train_good.csv')
    	st.session_state.test_data = load_data('exported_named_test_good.csv')
	except:
    	st.session_state.data = None
    	st.session_state.test_data = None

# Vue d'ensemble
if page == "📊 Vue d'ensemble":
	st.title("Tableau de bord ML")
    
	# Layout en colonnes
	col1, col2 = st.columns([2, 1])
    
	with col1:
    	# Upload de données
    	uploaded_file = st.file_uploader("Charger de nouvelles données", type=['csv'])
    	if uploaded_file is not None:
        	st.session_state.data = load_data(uploaded_file)
    
	with col2:
    	# Sélection du modèle
    	model_name = st.selectbox(
        	"Sélectionner un modèle",
        	["Logistic Regression", "Decision Tree", "Random Forest", "Gradient Boost"]
    	)
    
	if st.session_state.data is not None:
    	# Métriques principales
    	col1, col2, col3, col4, col5 = st.columns(5)
   	 
    	# Supposons que nous avons déjà un modèle entraîné
    	X = st.session_state.data.drop("Target", axis=1)
    	y = st.session_state.data["Target"]
    	model = train_model(X, y, model_name)
    	y_pred = model.predict(X)
   	 
    	with col1:
        	st.metric("Accuracy", f"{accuracy_score(y, y_pred):.2%}")
    	with col2:
        	st.metric("Precision", f"{precision_score(y, y_pred):.2%}")
    	with col3:
        	st.metric("Recall", f"{recall_score(y, y_pred):.2%}")
    	with col4:
        	st.metric("F1-Score", f"{f1_score(y, y_pred):.2%}")
    	with col5:
        	st.metric("ROC AUC", f"{roc_auc_score(y, y_pred):.2%}")
   	 
    	# Graphiques
    	col1, col2 = st.columns(2)
   	 
    	with col1:
        	st.subheader("Importance des features")
        	if hasattr(model, 'feature_importances_'):
            	importances = pd.DataFrame({
                	'feature': X.columns,
                	'importance': model.feature_importances_
            	}).sort_values('importance', ascending=True)
            	fig = px.bar(importances, x='importance', y='feature', orientation='h')
            	st.plotly_chart(fig, use_container_width=True)
   	 
    	with col2:
        	st.subheader("Matrice de confusion")
        	cm = confusion_matrix(y, y_pred)
        	fig = px.imshow(cm,
                      	labels=dict(x="Prédit", y="Réel"),
                      	text=cm)
        	st.plotly_chart(fig, use_container_width=True)

elif page == "🎯 Prédiction":
	st.title("Prédiction")
    
	if st.session_state.data is not None:
    	X = st.session_state.data.drop("Target", axis=1)
   	 
    	# Interface de prédiction
    	st.subheader("Entrer les valeurs pour la prédiction")
   	 
    	input_values = {}
    	cols = st.columns(3)
    	for idx, feature in enumerate(X.columns):
        	with cols[idx % 3]:
            	if X[feature].dtype == 'object':
                	input_values[feature] = st.selectbox(
                    	f"{feature}",
                    	options=X[feature].unique()
                	)
            	else:
                	input_values[feature] = st.number_input(
                    	f"{feature}",
                    	value=float(X[feature].mean())
                	)
   	 
    	if st.button("Prédire"):
        	model = train_model(X, st.session_state.data["Target"], "Random Forest")
        	pred = model.predict_proba(pd.DataFrame([input_values]))
       	 
        	st.subheader("Résultat de la prédiction")
        	proba_df = pd.DataFrame({
            	'Classe': ['0', '1'],
            	'Probabilité': pred[0]
        	})
        	fig = px.bar(proba_df, x='Classe', y='Probabilité')
        	st.plotly_chart(fig)

elif page == "🔍 Interprétation":
	st.title("Interprétation du modèle")
    
	if st.session_state.data is not None:
    	# SHAP values ou autres méthodes d'interprétation
    	st.subheader("Analyse des features")
    	X = st.session_state.data.drop("Target", axis=1)
    	y = st.session_state.data["Target"]
   	 
    	feature_1 = st.selectbox("Sélectionner la première feature", X.columns)
    	feature_2 = st.selectbox("Sélectionner la deuxième feature", X.columns)
   	 
    	fig = px.scatter(st.session_state.data,
                    	x=feature_1,
                    	y=feature_2,
                    	color='Target',
                    	title=f"Relation entre {feature_1} et {feature_2}")
    	st.plotly_chart(fig)

elif page == "⚙️ Entraînement":
	st.title("Entraînement du modèle")
    
	if st.session_state.data is not None:
    	# Options d'entraînement
    	model_name = st.selectbox(
        	"Sélectionner le modèle à entraîner",
        	["Logistic Regression", "Decision Tree", "Random Forest", "Gradient Boost"]
    	)
   	 
    	# Paramètres du modèle
    	st.subheader("Paramètres du modèle")
    	if model_name == "Random Forest":
        	n_estimators = st.slider("Nombre d'arbres", 10, 200, 100)
        	max_depth = st.slider("Profondeur maximale", 1, 20, 10)
   	 
    	if st.button("Entraîner le modèle"):
        	with st.spinner("Entraînement en cours..."):
            	X = st.session_state.data.drop("Target", axis=1)
            	y = st.session_state.data["Target"]
            	model = train_model(X, y, model_name)
            	st.success("Modèle entraîné avec succès!")
           	 
            	# Afficher les métriques
            	y_pred = model.predict(X)
            	col1, col2, col3 = st.columns(3)
            	with col1:
                	st.metric("Accuracy", f"{accuracy_score(y, y_pred):.2%}")
            	with col2:
                	st.metric("Precision", f"{precision_score(y, y_pred):.2%}")
            	with col3:
                	st.metric("Recall", f"{recall_score(y, y_pred):.2%}")


if __name__ == '__main__':
    app()