|
import streamlit as st
|
|
import pandas as pd
|
|
import pickle
|
|
import lime
|
|
import lime.lime_tabular
|
|
import streamlit.components.v1 as components
|
|
|
|
|
|
with open('model.pkl', 'rb') as file:
|
|
model = pickle.load(file)
|
|
|
|
obesity_mapping = {
|
|
0: 'Normal',
|
|
1: 'Surpoid/Obése'
|
|
}
|
|
|
|
def user_input_features():
|
|
age = st.number_input('Age:',min_value=8, max_value=100, value=24, step=1, format="%d")
|
|
classe = st.radio('Classe_', ('Primaire','Secondaire'))
|
|
Zone = st.radio('zone', ('Rurale', 'Urbaine'))
|
|
Diversité = st.radio('Consumption of food between meals (CAEC)', ('Mauvaise', 'Bonne'))
|
|
Region = st.selectbox(
|
|
'Region de ',
|
|
('Nord_ouest' ,'Sud_ouest', '1Ouest')
|
|
)
|
|
Sexe = st.radio('Genre', ('F', 'M'))
|
|
|
|
|
|
Zone = 1 if Zone == 'Rurale' else 0
|
|
classe = 1 if classe == 'Primaire' else 0
|
|
Diversité = 1 if Diversité == 'Mauvaise' else 0
|
|
Region = ['Nord_ouest' ,'Sud_ouest', '1Ouest'].index(Region)
|
|
|
|
sex_f = 1 if Sexe == 'F' else 0
|
|
sex_m = 1 if Sexe == 'M' else 0
|
|
|
|
data = {
|
|
'Region': Region,
|
|
'Zone': Zone,
|
|
'Classe': classe,
|
|
'Age': age,
|
|
'Diversité': Diversité,
|
|
'Genre_F': sex_f,
|
|
'Genre_M': sex_m
|
|
}
|
|
features = pd.DataFrame(data, index=[0])
|
|
return features
|
|
|
|
st.title('Obesity App')
|
|
|
|
|
|
input_df = user_input_features()
|
|
|
|
|
|
explainer = lime.lime_tabular.LimeTabularExplainer(
|
|
training_data=input_df.values,
|
|
feature_names=input_df.columns,
|
|
class_names=[obesity_mapping[0], obesity_mapping[1]],
|
|
mode='classification'
|
|
)
|
|
|
|
|
|
if st.button('Predict'):
|
|
|
|
prediction = model.predict(input_df)
|
|
prediction_proba = model.predict_proba(input_df)[0]
|
|
|
|
data = {
|
|
'Obesity Type': [obesity_mapping[i] for i in range(len(prediction_proba))],
|
|
'Probability': prediction_proba
|
|
}
|
|
|
|
|
|
result_df = pd.DataFrame(data)
|
|
|
|
|
|
result_df = result_df.T
|
|
result_df.columns = result_df.iloc[0]
|
|
result_df = result_df.drop(result_df.index[0])
|
|
result_df.index = ['Probability']
|
|
|
|
|
|
st.table(result_df.style.format("{:.4f}"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exp = explainer.explain_instance(input_df.values[0], model.predict_proba, num_features=4)
|
|
|
|
|
|
explanation_html = exp.as_html()
|
|
|
|
|
|
st.subheader('Explication LIME')
|
|
|
|
|
|
components.html(explanation_html, height=800)
|
|
|
|
|