Spaces:
Sleeping
Sleeping
File size: 9,451 Bytes
3a61454 b1e8012 7d36604 c56b43d b1e8012 02638a1 b1e8012 3a61454 b1e8012 1ad2cd1 6cd1c2c c56da91 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 2644c4d 3a61454 |
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree, export_text
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier, plot_tree
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, roc_curve
data = pd.read_csv('exported_named_train_good.csv')
data_test = pd.read_csv('exported_named_test_good.csv')
X_train = data.drop("Target", axis=1).values
y_train = data['Target'].values
X_test = data_test.drop('Target', axis=1).values
y_test = data_test['Target'].values
models={
"Logisitic Regression":LogisticRegression(),
"Decision Tree":DecisionTreeClassifier(),
"Random Forest":RandomForestClassifier(),
"Gradient Boost":GradientBoostingClassifier()
}
for name, model in models.items():
model.fit(X_train, y_train)
# Make predictions
y_train_pred = model.predict(X_train)
y_test_pred = model.predict(X_test)
# Training set performance
model_train_accuracy = accuracy_score(y_train, y_train_pred) # Calculate Accuracy
model_train_f1 = f1_score(y_train, y_train_pred, average='weighted') # Calculate F1-score
model_train_precision = precision_score(y_train, y_train_pred) # Calculate Precision
model_train_recall = recall_score(y_train, y_train_pred) # Calculate Recall
model_train_rocauc_score = roc_auc_score(y_train, y_train_pred)
# Test set performance
model_test_accuracy = accuracy_score(y_test, y_test_pred) # Calculate Accuracy
model_test_f1 = f1_score(y_test, y_test_pred, average='weighted') # Calculate F1-score
model_test_precision = precision_score(y_test, y_test_pred) # Calculate Precision
model_test_recall = recall_score(y_test, y_test_pred) # Calculate Recall
model_test_rocauc_score = roc_auc_score(y_test, y_test_pred) #Calculate Roc
print(name)
print('Model performance for Training set')
print("- Accuracy: {:.4f}".format(model_train_accuracy))
print('- F1 score: {:.4f}'.format(model_train_f1))
print('- Precision: {:.4f}'.format(model_train_precision))
print('- Recall: {:.4f}'.format(model_train_recall))
print('- Roc Auc Score: {:.4f}'.format(model_train_rocauc_score))
print('----------------------------------')
print('Model performance for Test set')
print('- Accuracy: {:.4f}'.format(model_test_accuracy))
print('- F1 score: {:.4f}'.format(model_test_f1))
print('- Precision: {:.4f}'.format(model_test_precision))
print('- Recall: {:.4f}'.format(model_test_recall))
print('- Roc Auc Score: {:.4f}'.format(model_test_rocauc_score))
print('='*35)
print('\n')
def load_model_and_data():
# Ici vous chargeriez votre modèle et données
# Pour l'exemple, on suppose qu'ils sont disponibles comme:
# model = loaded_model
# X = loaded_X
# y = loaded_y
# feature_names = X.columns
model = models['Decision Tree']
data = pd.read_csv('exported_named_train_good.csv')
X = data.drop("Target", axis=1)
y = data['Target']
return model, X, y
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree, export_text
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from dtreeviz.trees import dtreeviz
def app():
st.title("Interpréteur d'Arbre de Décision")
# Chargement du modèle et des données
model, X, y, feature_names = load_model_and_data()
if model is None:
st.warning("Veuillez charger un modèle pour commencer.")
return
# Sidebar avec les sections
st.sidebar.title("Navigation")
page = st.sidebar.radio(
"Sélectionnez une section",
["Vue globale du modèle",
"Explorateur de règles",
"Analyse de cohortes",
"Simulateur de prédictions"]
)
# Vue globale du modèle
if page == "Vue globale du modèle":
st.header("Vue globale du modèle")
col1, col2 = st.columns(2)
with col1:
st.subheader("Importance des caractéristiques")
importance_plot = plt.figure(figsize=(10, 6))
feature_importance = pd.DataFrame({
'feature': feature_names,
'importance': model.feature_importances_
}).sort_values('importance', ascending=True)
plt.barh(feature_importance['feature'], feature_importance['importance'])
st.pyplot(importance_plot)
with col2:
st.subheader("Statistiques du modèle")
st.write(f"Profondeur de l'arbre: {model.get_depth()}")
st.write(f"Nombre de feuilles: {model.get_n_leaves()}")
# Explorateur de règles
elif page == "Explorateur de règles":
st.header("Explorateur de règles de décision")
viz_type = st.radio(
"Type de visualisation",
["Texte", "Graphique interactif"]
)
max_depth = st.slider("Profondeur maximale à afficher", 1, model.get_depth(), 3)
if viz_type == "Texte":
tree_text = export_text(model, feature_names=list(feature_names), max_depth=max_depth)
st.text(tree_text)
else:
# Création de la visualisation dtreeviz
viz = dtreeviz(
model,
X,
y,
target_name="target",
feature_names=list(feature_names),
class_names=list(map(str, model.classes_)),
max_depth=max_depth
)
# Conversion en PNG pour Streamlit
png_data = viz.save_png()
st.image(png_data, use_column_width=True)
# Analyse de cohortes
elif page == "Analyse de cohortes":
st.header("Analyse de cohortes")
selected_features = st.multiselect(
"Sélectionnez les caractéristiques pour définir les cohortes",
feature_names,
max_selections=2
)
if len(selected_features) > 0:
def create_cohorts(X, features):
cohort_def = X[features].copy()
for feat in features:
if X[feat].dtype == 'object' or len(X[feat].unique()) < 10:
cohort_def[feat] = X[feat]
else:
cohort_def[feat] = pd.qcut(X[feat], q=4, labels=['Q1', 'Q2', 'Q3', 'Q4'])
return cohort_def.apply(lambda x: ' & '.join(x.astype(str)), axis=1)
cohorts = create_cohorts(X, selected_features)
cohort_analysis = pd.DataFrame({
'Cohorte': cohorts,
'Prédiction': model.predict(X)
})
cohort_stats = cohort_analysis.groupby('Cohorte')['Prédiction'].agg(['count', 'mean'])
cohort_stats.columns = ['Nombre d\'observations', 'Taux de prédiction positive']
st.write("Statistiques par cohorte:")
st.dataframe(cohort_stats)
cohort_viz = plt.figure(figsize=(10, 6))
sns.barplot(data=cohort_analysis, x='Cohorte', y='Prédiction')
plt.xticks(rotation=45)
st.pyplot(cohort_viz)
# Simulateur de prédictions
else:
st.header("Simulateur de prédictions")
input_values = {}
for feature in feature_names:
if X[feature].dtype == 'object':
input_values[feature] = st.selectbox(
f"Sélectionnez {feature}",
options=X[feature].unique()
)
else:
input_values[feature] = st.slider(
f"Valeur pour {feature}",
float(X[feature].min()),
float(X[feature].max()),
float(X[feature].mean())
)
if st.button("Prédire"):
input_df = pd.DataFrame([input_values])
prediction = model.predict_proba(input_df)
st.write("Probabilités prédites:")
st.write({f"Classe {i}": f"{prob:.2%}" for i, prob in enumerate(prediction[0])})
st.subheader("Chemin de décision")
node_indicator = model.decision_path(input_df)
leaf_id = model.apply(input_df)
node_index = node_indicator.indices[node_indicator.indptr[0]:node_indicator.indptr[1]]
rules = []
for node_id in node_index:
if node_id != leaf_id[0]:
threshold = model.tree_.threshold[node_id]
feature = feature_names[model.tree_.feature[node_id]]
if input_df.iloc[0][feature] <= threshold:
rules.append(f"{feature} ≤ {threshold:.2f}")
else:
rules.append(f"{feature} > {threshold:.2f}")
for rule in rules:
st.write(rule)
if __name__ == "__main__":
app() |