Delete app.py
Browse files
app.py
DELETED
@@ -1,102 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import pickle
|
4 |
-
import lime
|
5 |
-
import lime.lime_tabular
|
6 |
-
import streamlit.components.v1 as components
|
7 |
-
|
8 |
-
# Load your trained model
|
9 |
-
with open('model.pkl', 'rb') as file:
|
10 |
-
model = pickle.load(file)
|
11 |
-
|
12 |
-
obesity_mapping = {
|
13 |
-
0: 'Normal',
|
14 |
-
1: 'Surpoid/Obése'
|
15 |
-
}
|
16 |
-
# Define the input features for the user to input
|
17 |
-
def user_input_features():
|
18 |
-
age = st.number_input('Age:',min_value=8, max_value=100, value=24, step=1, format="%d")
|
19 |
-
classe = st.radio('Classe_', ('Primaire','Secondaire'))
|
20 |
-
Zone = st.radio('zone', ('Rurale', 'Urbaine'))
|
21 |
-
Diversité = st.radio('Consumption of food between meals (CAEC)', ('Mauvaise', 'Bonne'))
|
22 |
-
Region = st.selectbox(
|
23 |
-
'Region de ',
|
24 |
-
('Nord_ouest' ,'Sud_ouest', '1Ouest')
|
25 |
-
)
|
26 |
-
Sexe = st.radio('Genre', ('F', 'M'))
|
27 |
-
|
28 |
-
|
29 |
-
Zone = 1 if Zone == 'Rurale' else 0
|
30 |
-
classe = 1 if classe == 'Primaire' else 0
|
31 |
-
Diversité = 1 if Diversité == 'Mauvaise' else 0
|
32 |
-
Region = ['Nord_ouest' ,'Sud_ouest', '1Ouest'].index(Region)
|
33 |
-
|
34 |
-
sex_f = 1 if Sexe == 'F' else 0
|
35 |
-
sex_m = 1 if Sexe == 'M' else 0
|
36 |
-
|
37 |
-
data = {
|
38 |
-
'Region': Region,
|
39 |
-
'Zone': Zone,
|
40 |
-
'Classe': classe,
|
41 |
-
'Age': age,
|
42 |
-
'Diversité': Diversité,
|
43 |
-
'Genre_F': sex_f,
|
44 |
-
'Genre_M': sex_m
|
45 |
-
}
|
46 |
-
features = pd.DataFrame(data, index=[0])
|
47 |
-
return features
|
48 |
-
|
49 |
-
st.title('Obesity App')
|
50 |
-
|
51 |
-
# Display the input fields
|
52 |
-
input_df = user_input_features()
|
53 |
-
|
54 |
-
# Initialiser LIME
|
55 |
-
explainer = lime.lime_tabular.LimeTabularExplainer(
|
56 |
-
training_data=input_df.values, # Entraînement sur la base des données d'entrée
|
57 |
-
feature_names=input_df.columns,
|
58 |
-
class_names=[obesity_mapping[0], obesity_mapping[1]],
|
59 |
-
mode='classification'
|
60 |
-
)
|
61 |
-
|
62 |
-
# Predict button
|
63 |
-
if st.button('Predict'):
|
64 |
-
# Make a prediction
|
65 |
-
prediction = model.predict(input_df)
|
66 |
-
prediction_proba = model.predict_proba(input_df)[0]
|
67 |
-
|
68 |
-
data = {
|
69 |
-
'Obesity Type': [obesity_mapping[i] for i in range(len(prediction_proba))],
|
70 |
-
'Probability': prediction_proba
|
71 |
-
}
|
72 |
-
|
73 |
-
# Create a dataframe to display the results
|
74 |
-
result_df = pd.DataFrame(data)
|
75 |
-
|
76 |
-
# Transpose the dataframe to have obesity types as columns and add a row header
|
77 |
-
result_df = result_df.T
|
78 |
-
result_df.columns = result_df.iloc[0]
|
79 |
-
result_df = result_df.drop(result_df.index[0])
|
80 |
-
result_df.index = ['Probability']
|
81 |
-
|
82 |
-
# Display the results in a table with proper formatting
|
83 |
-
st.table(result_df.style.format("{:.4f}"))
|
84 |
-
# Générer l'explication LIME pour l'individu
|
85 |
-
# exp = explainer.explain_instance(input_df.values[0], model.predict_proba, num_features=5)
|
86 |
-
|
87 |
-
# # Afficher les explications dans Streamlit
|
88 |
-
# st.subheader('Explication LIME')
|
89 |
-
# exp.show_in_notebook(show_table=True, show_all=False)
|
90 |
-
# st.write(exp.as_list())
|
91 |
-
# Générer l'explication LIME pour l'individu
|
92 |
-
exp = explainer.explain_instance(input_df.values[0], model.predict_proba, num_features=4)
|
93 |
-
|
94 |
-
# Récupérer l'explication LIME sous forme HTML
|
95 |
-
explanation_html = exp.as_html()
|
96 |
-
|
97 |
-
# Afficher l'explication LIME dans Streamlit
|
98 |
-
st.subheader('Explication LIME')
|
99 |
-
|
100 |
-
# Utiliser Streamlit pour afficher du HTML
|
101 |
-
components.html(explanation_html, height=800) # Ajuster la hauteur selon le contenu
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|