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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from streamlit_option_menu import option_menu
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle
|
| 5 |
+
import lime
|
| 6 |
+
import lime.lime_tabular
|
| 7 |
+
import streamlit.components.v1 as components
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import seaborn as sns
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
|
| 12 |
+
# 1=sidebar menu, 2=horizontal menu, 3=horizontal menu w/ custom menu
|
| 13 |
+
EXAMPLE_NO = 3
|
| 14 |
+
st.set_page_config(layout='wide')
|
| 15 |
+
st.markdown("""
|
| 16 |
+
<style>
|
| 17 |
+
.block-container {
|
| 18 |
+
padding-top: 2rem;
|
| 19 |
+
padding-bottom: 0rem;
|
| 20 |
+
padding-left: 1rem;
|
| 21 |
+
padding-right: 1rem;
|
| 22 |
+
}
|
| 23 |
+
</style>
|
| 24 |
+
""", unsafe_allow_html=True)
|
| 25 |
+
|
| 26 |
+
def streamlit_menu(example=1):
|
| 27 |
+
if example == 1:
|
| 28 |
+
# 1. as sidebar menu
|
| 29 |
+
with st.sidebar:
|
| 30 |
+
selected = option_menu(
|
| 31 |
+
menu_title="Main Menu", # required
|
| 32 |
+
options=["Home", "Projects", "Prédiction"], # required
|
| 33 |
+
icons=["house", "book", "envelope"], # optional
|
| 34 |
+
menu_icon="cast", # optional
|
| 35 |
+
default_index=0, # optional
|
| 36 |
+
)
|
| 37 |
+
return selected
|
| 38 |
+
|
| 39 |
+
if example == 2:
|
| 40 |
+
# 2. horizontal menu w/o custom style
|
| 41 |
+
selected = option_menu(
|
| 42 |
+
menu_title=None, # required
|
| 43 |
+
options=[ "Projects","Home", "Prédiction"], # required
|
| 44 |
+
icons=["house", "book", "envelope"], # optional
|
| 45 |
+
menu_icon="cast", # optional
|
| 46 |
+
default_index=0, # optional
|
| 47 |
+
orientation="horizontal",
|
| 48 |
+
)
|
| 49 |
+
return selected
|
| 50 |
+
|
| 51 |
+
if example == 3:
|
| 52 |
+
# 2. horizontal menu with custom style
|
| 53 |
+
selected = option_menu(
|
| 54 |
+
menu_title=None, # required
|
| 55 |
+
options=["Home", "Projects", "Prédiction"], # required
|
| 56 |
+
icons=["house", "book", "envelope"], # optional
|
| 57 |
+
menu_icon="cast", # optional
|
| 58 |
+
default_index=0, # optional
|
| 59 |
+
orientation="horizontal",
|
| 60 |
+
styles={
|
| 61 |
+
"container": {"padding": "0!important", "background-color": "#fafafa"},
|
| 62 |
+
"icon": {"color": "orange", "font-size": "25px"},
|
| 63 |
+
"nav-link": {
|
| 64 |
+
"font-size": "25px",
|
| 65 |
+
"text-align": "left",
|
| 66 |
+
"margin": "0px",
|
| 67 |
+
"--hover-color": "#eee",
|
| 68 |
+
},
|
| 69 |
+
"nav-link-selected": {"background-color": "skyblue"},
|
| 70 |
+
},
|
| 71 |
+
)
|
| 72 |
+
return selected
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
selected = streamlit_menu(example=EXAMPLE_NO)
|
| 76 |
+
|
| 77 |
+
if selected == "Home":
|
| 78 |
+
# st.title(f"You have selected {selected}")
|
| 79 |
+
# Load your trained model
|
| 80 |
+
with open('model.pkl', 'rb') as file:
|
| 81 |
+
model = pickle.load(file)
|
| 82 |
+
|
| 83 |
+
obesity_mapping = {
|
| 84 |
+
0: 'Normal',
|
| 85 |
+
1: 'Surpoid\Obése'
|
| 86 |
+
}
|
| 87 |
+
# Define the input features for the user to input
|
| 88 |
+
def user_input_features():
|
| 89 |
+
age = st.number_input('Age:',min_value=8, max_value=19, value=19, step=1, format="%d")
|
| 90 |
+
classe = st.radio('Classe_', ('Primaire','Secondaire'))
|
| 91 |
+
Zone = st.radio('zone', ('Rurale', 'Urbaine'))
|
| 92 |
+
Voler = st.radio('Voler', ('Oui', 'Non'))
|
| 93 |
+
Diversité = st.radio('Diversité', ('Mauvaise', 'Bonne'))
|
| 94 |
+
Region = st.selectbox(
|
| 95 |
+
'Region de ',
|
| 96 |
+
('Nord_ouest' ,'Sud_ouest', 'Ouest')
|
| 97 |
+
)
|
| 98 |
+
Source_eau=st.selectbox(
|
| 99 |
+
'Provenence ',
|
| 100 |
+
('Camwater','Eau_de_surface','forage','Puits','Eau_minérale')
|
| 101 |
+
)
|
| 102 |
+
Sexe = st.radio('Genre', ('F', 'M'))
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
Zone = 1 if Zone == 'Rurale' else 0
|
| 106 |
+
classe = 1 if classe == 'Primaire' else 0
|
| 107 |
+
Diversité = 1 if Diversité == 'Mauvaise' else 0
|
| 108 |
+
Region = ['Nord_ouest' ,'Sud_ouest', 'Ouest'].index(Region)
|
| 109 |
+
Source_eau=['Camwater','Eau_de_surface','forage','Puits','Eau_minérale'].index(Source_eau)
|
| 110 |
+
sex_f = 1 if Sexe == 'F' else 0
|
| 111 |
+
sex_m = 1 if Sexe == 'M' else 0
|
| 112 |
+
|
| 113 |
+
data = {
|
| 114 |
+
'Region': Region,
|
| 115 |
+
'Zone': Zone,
|
| 116 |
+
'Classe': classe,
|
| 117 |
+
'Age': age,
|
| 118 |
+
'Diversité': Diversité,
|
| 119 |
+
'Voler': Voler,
|
| 120 |
+
'Source_eau':Source_eau,
|
| 121 |
+
'Genre_F': sex_f,
|
| 122 |
+
'Genre_M': sex_m
|
| 123 |
+
}
|
| 124 |
+
features = pd.DataFrame(data, index=[0])
|
| 125 |
+
return features
|
| 126 |
+
|
| 127 |
+
# st.title('Obesity App')
|
| 128 |
+
|
| 129 |
+
# Display the input fields
|
| 130 |
+
input_df = user_input_features()
|
| 131 |
+
# Convertir toutes les colonnes non numériques en numérique si possible
|
| 132 |
+
input_df = input_df.apply(pd.to_numeric, errors='coerce')
|
| 133 |
+
|
| 134 |
+
# Remplacer les NaN par une valeur arbitraire (par exemple 0) si nécessaire
|
| 135 |
+
input_df = input_df.fillna(0)
|
| 136 |
+
|
| 137 |
+
# Initialiser LIME
|
| 138 |
+
explainer = lime.lime_tabular.LimeTabularExplainer(
|
| 139 |
+
training_data=input_df.values, # Entraînement sur la base des données d'entrée
|
| 140 |
+
feature_names=input_df.columns,
|
| 141 |
+
class_names=[obesity_mapping[0], obesity_mapping[1]],
|
| 142 |
+
mode='classification'
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
# Predict button
|
| 146 |
+
if st.button('Predict'):
|
| 147 |
+
# Make a prediction
|
| 148 |
+
prediction = model.predict(input_df)
|
| 149 |
+
prediction_proba = model.predict_proba(input_df)[0]
|
| 150 |
+
|
| 151 |
+
data = {
|
| 152 |
+
'Statut nutritionnel': [obesity_mapping[i] for i in range(len(prediction_proba))],
|
| 153 |
+
'Probabilité': prediction_proba
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
# Create a dataframe to display the results
|
| 157 |
+
result_df = pd.DataFrame(data)
|
| 158 |
+
|
| 159 |
+
# Transpose the dataframe to have obesity types as columns and add a row header
|
| 160 |
+
result_df = result_df.T
|
| 161 |
+
result_df.columns = result_df.iloc[0]
|
| 162 |
+
result_df = result_df.drop(result_df.index[0])
|
| 163 |
+
result_df.index = ['Probability']
|
| 164 |
+
|
| 165 |
+
# Display the results in a table with proper formatting
|
| 166 |
+
st.table(result_df.style.format("{:.4f}"))
|
| 167 |
+
# Générer l'explication LIME pour l'individu
|
| 168 |
+
# exp = explainer.explain_instance(input_df.values[0], model.predict_proba, num_features=5)
|
| 169 |
+
|
| 170 |
+
# # Afficher les explications dans Streamlit
|
| 171 |
+
# st.subheader('Explication LIME')
|
| 172 |
+
# exp.show_in_notebook(show_table=True, show_all=False)
|
| 173 |
+
# st.write(exp.as_list())
|
| 174 |
+
# Générer l'explication LIME pour l'individu
|
| 175 |
+
exp = explainer.explain_instance(input_df.values[0], model.predict_proba, num_features=4)
|
| 176 |
+
|
| 177 |
+
# Récupérer l'explication LIME sous forme HTML
|
| 178 |
+
explanation_html = exp.as_html()
|
| 179 |
+
|
| 180 |
+
# Afficher l'explication LIME dans Streamlit
|
| 181 |
+
st.subheader('Explication LIME')
|
| 182 |
+
|
| 183 |
+
# Utiliser Streamlit pour afficher du HTML
|
| 184 |
+
components.html(explanation_html, height=800) # Ajuster la hauteur selon le contenu
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
if selected == "Projects":
|
| 189 |
+
st.image("az.JPEG", caption="Description de l'image", use_column_width=True)
|
| 190 |
+
if selected == "Prédiction":
|
| 191 |
+
# Ouvrir l'image avec Pillow
|
| 192 |
+
image = Image.open("az.JPEG")
|
| 193 |
+
|
| 194 |
+
# Redimensionner l'image (largeur, hauteur)
|
| 195 |
+
image = image.resize((300, 200)) # Par exemple, 300x200 pixels
|
| 196 |
+
|
| 197 |
+
# Afficher l'image redimensionnée
|
| 198 |
+
st.image(image, caption="Image redimensionnée", use_column_width=False)
|
| 199 |
+
# Titre de l'application
|
| 200 |
+
st.title("Visualisation des données avec Seaborn et Pandas")
|
| 201 |
+
|
| 202 |
+
# Charger le fichier CSV
|
| 203 |
+
uploaded_file = st.file_uploader("Choisissez un fichier", type=["csv", "xlsx", "json"])
|
| 204 |
+
|
| 205 |
+
if uploaded_file is not None:
|
| 206 |
+
# Lecture du fichier CSV
|
| 207 |
+
file_extension = uploaded_file.name.split('.')[-1]
|
| 208 |
+
if file_extension == 'csv':
|
| 209 |
+
# Lecture du fichier CSV
|
| 210 |
+
df = pd.read_csv(uploaded_file)
|
| 211 |
+
elif file_extension == 'xlsx':
|
| 212 |
+
# Lecture du fichier Excel
|
| 213 |
+
df = pd.read_excel(uploaded_file)
|
| 214 |
+
elif file_extension == 'json':
|
| 215 |
+
# Lecture du fichier JSON
|
| 216 |
+
df = pd.read_json(uploaded_file)
|
| 217 |
+
else:
|
| 218 |
+
st.error("Format de fichier non supporté!")
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# Afficher le dataframe
|
| 222 |
+
st.write("Aperçu du dataset :")
|
| 223 |
+
st.write(df.head())
|
| 224 |
+
|
| 225 |
+
# Afficher les statistiques descriptives
|
| 226 |
+
st.write("Statistiques descriptives :")
|
| 227 |
+
st.write(df.describe())
|
| 228 |
+
|
| 229 |
+
# Sélection des variables pour les visualisations
|
| 230 |
+
numerical_columns = df.select_dtypes(include=['float64', 'int64']).columns.tolist()
|
| 231 |
+
categorical_columns = df.select_dtypes(include=['object', 'category']).columns.tolist()
|
| 232 |
+
|
| 233 |
+
# Distribution d'une variable
|
| 234 |
+
st.subheader("Distribution d'une variable numérique")
|
| 235 |
+
selected_column = st.selectbox("Choisissez une variable numérique", numerical_columns)
|
| 236 |
+
if st.button("Afficher la distribution"):
|
| 237 |
+
fig, ax = plt.subplots(figsize=(5, 6))
|
| 238 |
+
sns.histplot(df[selected_column], kde=True, ax=ax)
|
| 239 |
+
st.pyplot(fig)
|
| 240 |
+
|
| 241 |
+
# Scatter plot
|
| 242 |
+
st.subheader("Scatter Plot entre deux variables numériques")
|
| 243 |
+
x_axis = st.selectbox("Choisissez la variable pour l'axe X", numerical_columns)
|
| 244 |
+
y_axis = st.selectbox("Choisissez la variable pour l'axe Y", numerical_columns, key='scatter')
|
| 245 |
+
if st.button("Afficher le scatter plot"):
|
| 246 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 247 |
+
sns.scatterplot(x=df[x_axis], y=df[y_axis], ax=ax)
|
| 248 |
+
st.pyplot(fig)
|
| 249 |
+
|
| 250 |
+
# Boxplot
|
| 251 |
+
st.subheader("Boxplot d'une variable numérique par rapport à une variable catégorielle")
|
| 252 |
+
selected_categorical = st.selectbox("Choisissez une variable catégorielle", categorical_columns)
|
| 253 |
+
selected_numerical = st.selectbox("Choisissez une variable numérique", numerical_columns, key='boxplot')
|
| 254 |
+
if st.button("Afficher le boxplot"):
|
| 255 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 256 |
+
sns.boxplot(x=df[selected_categorical], y=df[selected_numerical], ax=ax)
|
| 257 |
+
st.pyplot(fig)
|
az.jpeg
ADDED
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:28aa471b6ae23de5abe249cfe72e5bfe8240d72e942feeacb56980d1aed7020c
|
| 3 |
+
size 335533
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.25.0
|
| 2 |
+
streamlit-option-menu==0.3.2
|
| 3 |
+
pandas==2.1.1
|
| 4 |
+
pickle5==0.0.12
|
| 5 |
+
lime==0.2.0.1
|
| 6 |
+
Pillow==9.2.0
|
| 7 |
+
seaborn==0.12.2
|
| 8 |
+
matplotlib==3.7.1
|