File size: 9,858 Bytes
b0ff21d |
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 |
import streamlit as st
import pandas as pd
import numpy as np
import copy
from oocsi_source import OOCSI
from uuid import uuid4
from streamlit_extras.switch_page_button import switch_page
import random
# import shap
import dice_ml
from dice_ml.utils import helpers
import xgboost as xgb
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
# st.markdown("""<style>
# .stSlider {
# padding-bottom: 20px;
# }
# </style> """,
# unsafe_allow_html=True)
# st.session_state.Y_train
# st.session_state.X_test
# st.session_state.X_test_names
#Delete this page from the array of pages to visit, this way it cannot be visited twice
if 'profile3' not in st.session_state:
st.session_state.pages.remove("counterfactual")
st.session_state.profile3= 'deleted'
if (len(st.session_state.pages)>0):
st.session_state.nextPage3 = random.randint(0, len(st.session_state.pages)-1)
st.session_state.lastQuestion= 'no'
else:
st.session_state.lastQuestion= 'yes'
if 'index3' not in st.session_state:
st.session_state.index3= 0
if 'profileIndex' not in st.session_state:
st.session_state.profileIndex= st.session_state.profileIndices[st.session_state.index3]
header1, header2, header3 = st.columns([1,2,1])
characteristics1, characteristics2, characteristics3 = st.columns([1,2,1])
prediction1, prediction2, prediction3 =st.columns([1,2,1])
explanation1, explanation2, explanation3 = st.columns([1,10,1])
footer1, footer2, footer3 =st.columns([1,2,1])
evaluation1, evaluation2, evaluation3 = st.columns([1,2,1])
name= st.session_state.X_test_names.loc[st.session_state.profileIndex, "Name"]
@st.cache_resource
def trainModel(X_train,Y_train):
model_1 = RandomForestClassifier().fit(X_train, Y_train) ## Random forest because XGBoost doesn't work with counterfactuals
return model_1
@st.cache_resource
def getcounterfactual_values(_model,X_prediction, X_train):
# compute counterfactual values
train_df = pd.read_csv('assets/train_df.csv')
continous_col=["Age", 'Fare', 'Siblings_spouses', 'Title', 'Parents_children','relatives' ]
# test_df_counter = X_test.copy()
# test_df_counter['Survived'] = X_prediction
dice_data = dice_ml.Data(dataframe=train_df,continuous_features=continous_col, outcome_name='Survived')
dice_model= dice_ml.Model(model=_model, backend="sklearn")
explainer = dice_ml.Dice(dice_data, dice_model, method="random")
return explainer
def Counterfactualsplot(X_test, explainer):
e1 = explainer.generate_counterfactuals(
X_test[1:2],total_CFs=4, desired_class="opposite",
features_to_vary = ['Age','Pclass', 'Sex','Siblings_spouses', 'Parents_children', 'Embarked', 'relatives', 'Title'] ) ## Deck, Fare
e1.cf_examples_list[0].final_cfs_df.to_csv(path_or_buf=rf'assets\counterfactuals_{name}.csv', index=False)
counter_csv = pd.read_csv(f'assets\counterfactuals_{name}.csv')
return st.dataframe(counter_csv, width=10000)
with header2:
st.header("Explanation - Counterfactuals")
st.markdown('''A counterfactual explanation describes a situation where if a specific event had not occurred, the conclusion would have been different
and a specific outcome would not have occurred. In machine learning, counterfactuals are used to explain prediction of individuals instances. The prediction
of the model will be analysed and certain conditions/features that created this prediction will be modified to obtain an different outcome for the model.''')
st.markdown('''As displayed in the graph below, the relation betwwen the inputs andthe prediciton is modified by the feature values that creates a simple causal
relationshhip betwen inputs and predictions.
''')
st.image('assets/counterfactual.jpg', caption = 'Causal relation between inputs and predictions', use_column_width = 'always' )
st.markdown('''A counterfactual explanation of a prediction will then describe the smallest amount of change that is necessary to make to change the output
prediction to a predefine one.''')
st.subheader(name, anchor='top')
# st.write("For debugging:")
# st.write(st.session_state.participantID)
random_forest= trainModel(st.session_state.X_train, st.session_state.Y_train)
with characteristics2:
# initialize list of lists
data = st.session_state.X_test.iloc[st.session_state.profileIndex].values.reshape(1, -1)
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=st.session_state.X_test.columns)
st.dataframe(df)
with prediction2:
# st.header("Prediction")
prediction = random_forest.predict(st.session_state.X_test.iloc[st.session_state.profileIndex].values.reshape(1, -1))
prediction_all = random_forest.predict(st.session_state.X_test.values)
probability = random_forest.predict_proba(st.session_state.X_test.iloc[st.session_state.profileIndex].values.reshape(1, -1))
if prediction == 0:
prob = round((probability[0][0]*100),2)
st.markdown("The model predicts with {}% probability that {} will :red[**not survive**]".format(prob, name) )
else:
prob = round((probability[0][1]*100),2)
st.markdown("The model predicts with {}% probability that {} will :green[**survive**]".format(prob, name) )
with explanation2:
st.subheader("Explanation")
st.markdown("counterfactual, more text here")
# with st.spinner("Please be patient, we are generating a new explanation"):
explainer= getcounterfactual_values(random_forest, prediction_all, st.session_state.X_test)
st.set_option('deprecation.showPyplotGlobalUse', False)
e1=Counterfactualsplot(st.session_state.X_test, explainer)
data_indices = pd.concat([d.reset_index(drop=True) for d in [st.session_state.ports_df, st.session_state.title_df, st.session_state.gender_df]], axis=1)
st.dataframe(data_indices)
with footer2:
if (st.session_state.index3 < len(st.session_state.profileIndices)-1):
if st.button("New profile"):
st.session_state.index3 = st.session_state.index3+1
st.session_state.profileIndex = st.session_state.profileIndices[st.session_state.index3]
st.experimental_rerun()
else:
def is_user_active():
if 'user_active3' in st.session_state.keys() and st.session_state['user_active3']:
return True
else:
return False
if is_user_active():
# if st.button("Continue to evaluation"):
# st.write(" ")
with st.form("my_form3", clear_on_submit=True):
st.subheader("Evaluation")
st.write("These questions only ask for your opinion about this specific explanation")
q1 = st.select_slider(
'**1**- From the explanation, I **understand** how the algorithm works:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q2 = st.select_slider(
'**2**- This explanation of how the algorithm works is **satisfying**:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q3 = st.select_slider(
'**3**- This explanation of how the algorithm works has **sufficient detail**:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q4 = st.select_slider(
'**4**- This explanation of how the algorithm works seems **complete**:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q5 = st.select_slider(
'**5**- This explanation of how the algorithm works **tells me how to use it**:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q6 = st.select_slider(
'**6**- This explanation of how the algorithm works is **useful to my goals**:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q7 = st.select_slider(
'**7**- This explanation of the algorithm shows me how **accurate** the algorithm is:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
q8 = st.select_slider(
'**8**- This explanation lets me judge when I should **trust and not trust** the algorithm:',
options=['totally disagree', 'disagree', 'neutral' , 'agree', 'totally agree'])
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
# st.write("question 1", q1)
st.session_state.oocsi.send('EngD_HAII', {
'participant_ID': st.session_state.participantID,
'type of explanation': 'counterfactual',
'q1': q1,
'q2': q2,
'q3': q3,
'q4': q4,
'q5': q5,
'q6': q6,
'q7': q7,
'q8': q8,
})
if (st.session_state.lastQuestion =='yes'):
switch_page('finalPage')
else:
st.session_state.profileIndex =st.session_state.profileIndices[0]
switch_page(st.session_state.pages[st.session_state.nextPage3])
else:
if st.button('Continue to evaluation'):
st.session_state['user_active3']=True
st.experimental_rerun()
|