Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
C
covid_analysis
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
COMPARA
covid_analysis
Commits
7b58b74c
Commit
7b58b74c
authored
Jun 06, 2024
by
Joaquin Torres
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Progress made on shap (still pending to see predict_proba and X_train vs test)
parent
aa9797c1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
25 deletions
+21
-25
explicability/shap_vals.py
explicability/shap_vals.py
+21
-25
No files found.
explicability/shap_vals.py
View file @
7b58b74c
...
@@ -110,8 +110,10 @@ def get_chosen_model(group_str, method_str, model_name):
...
@@ -110,8 +110,10 @@ def get_chosen_model(group_str, method_str, model_name):
# Initialize the model with the parameters
# Initialize the model with the parameters
chosen_model
=
model_class
(
**
parameters
)
chosen_model
=
model_class
(
**
parameters
)
# Return if it is a tree model, for SHAP
is_tree
=
model_name
not
in
[
'LR'
,
'SVM'
,
'MLP'
]
return
chosen_model
return
chosen_model
,
is_tree
# --------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
@@ -133,42 +135,36 @@ if __name__ == "__main__":
...
@@ -133,42 +135,36 @@ if __name__ == "__main__":
"OVER"
:
"XGB"
,
"OVER"
:
"XGB"
,
"UNDER"
:
"XGB"
"UNDER"
:
"XGB"
}
}
#
#
Retrieve attribute names in order
# Retrieve attribute names in order
# df = pd.read_csv("..\gen_train_data\data\input\
pre_dataset.csv")
df
=
pd
.
read_csv
(
"../gen_train_data/data/input/
pre_dataset.csv"
)
#
attribute_names = list(df.columns.values)
attribute_names
=
list
(
df
.
columns
.
values
)
# --------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------
# Shap value generation
# Shap value generation
# --------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------
for
i
,
group
in
enumerate
([
'pre'
,
'post'
]):
for
i
,
group
in
enumerate
([
'pre'
,
'post'
]):
# Get test dataset based on group
# Get test dataset based on group
X_test
=
data_dic
[
'X_test_'
+
group
]
X_test
=
pd
.
DataFrame
(
data_dic
[
'X_test_'
+
group
],
columns
=
attribute_names
)
y_test
=
data_dic
[
'y_test_'
+
group
]
y_test
=
data_dic
[
'y_test_'
+
group
]
for
j
,
method
in
enumerate
([
''
,
''
,
'over_'
,
'under_'
]):
for
j
,
method
in
enumerate
([
''
,
''
,
'over_'
,
'under_'
]):
print
(
f
"{group}-{method_names[j]}"
)
print
(
f
"{group}-{method_names[j]}"
)
# Get train dataset based on group and method
# Get train dataset based on group and method
X_train
=
data_dic
[
'X_train_'
+
method
+
group
]
X_train
=
pd
.
DataFrame
(
data_dic
[
'X_train_'
+
method
+
group
],
columns
=
attribute_names
)
y_train
=
data_dic
[
'y_train_'
+
method
+
group
]
y_train
=
data_dic
[
'y_train_'
+
method
+
group
]
method_name
=
method_names
[
j
]
method_name
=
method_names
[
j
]
# Get chosen tuned model for this group and method context
# Get chosen tuned model for this group and method context
model
=
get_chosen_model
(
group_str
=
group
,
method_str
=
method_name
,
model_name
=
model_choices
[
method_name
])
model
,
is_tree
=
get_chosen_model
(
group_str
=
group
,
method_str
=
method_name
,
model_name
=
model_choices
[
method_name
])
print
(
f
'Name: {model_choices[method_name]}'
)
# --------------------------------------------------------------------------------------------------------
print
(
model
.
get_params
())
# Fit model with training data
# # --------------------------------------------------------------------------------------------------------
fitted_model
=
model
.
fit
(
X_train
[:
500
],
y_train
[:
500
])
# # Retrieve best model for this group-method context
# Check if we are dealing with a tree vs nn model
# model_info = models[group + '_' + method_names[j]]
if
is_tree
:
# is_tree = model_info[0]
explainer
=
shap
.
TreeExplainer
(
fitted_model
,
X_test
[:
500
])
# model = model_info[1]
else
:
# # Fit model with training data
explainer
=
shap
.
KernelExplainer
(
fitted_model
.
predict_proba
,
X_test
[:
500
])
# fitted_model = model.fit(X_train[:500], y_train[:500])
# Compute shap values
# # Check if we are dealing with a tree vs nn model
shap_vals
=
explainer
.
shap_values
(
X_test
[:
500
],
check_additivity
=
False
)
# Change to true for final results
# if is_tree:
# ---------------------------------------------------------------------------------------------------------
# explainer = shap.TreeExplainer(fitted_model, X_test[:500])
# else:
# explainer = shap.KernelExplainer(fitted_model.predict, X_test[:500])
# # Compute shap values
# shap_vals = explainer.shap_values(X_test[:500], check_additivity=False) # Change to true for final results
# # ---------------------------------------------------------------------------------------------------------
# Save results
# Save results
# np.save(f"
shap_values/{group}_{method_names[j]}", shap_vals)
np
.
save
(
f
"./output/
shap_values/{group}_{method_names[j]}"
,
shap_vals
)
# --------------------------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------------------------
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment