Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Py] Get shaprpy up to speed #436

Merged
merged 9 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 0 additions & 94 deletions python/examples/devel_new_explain.py

This file was deleted.

47 changes: 24 additions & 23 deletions python/examples/pytorch_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,43 @@ def forward(self, x):
phi0 = dfy_train.mean().item(),
)
print(explanation["shapley_values_est"])

"""
explain_id none MedInc HouseAge AveRooms AveBedrms Population \
1 1 2.205951 3.531437 7.746453 6.985043 5.454877 3.287326
2 2 2.205951 6.004403 7.041080 4.254553 4.118677 3.162567
3 3 2.205950 5.497648 1.538680 6.750968 2.806428 3.687014
4 4 2.205951 5.761901 11.378609 2.112351 5.013451 5.754630
5 5 2.205951 5.325281 2.585713 2.224409 6.418153 -2.848570
explain_id none MedInc HouseAge AveRooms AveBedrms Population \
1 1 2.205948 3.516491 4.069221 4.807392 4.162077 3.419979
2 2 2.205948 4.452062 4.868121 3.657762 3.441219 3.445877
3 3 2.205948 4.242527 4.834834 4.847549 2.698246 3.597892
4 4 2.205948 4.196727 5.077788 2.831637 3.938577 4.289322
5 5 2.205948 4.115855 3.217642 2.986512 4.584130 1.320592

AveOccup Latitude Longitude
1 4.774873 2.273699 4.314784
2 7.386783 6.473623 3.318631
3 5.193341 4.875864 4.290797
4 5.866562 4.564957 5.139962
5 6.428984 4.280456 5.509226
1 4.158741 2.698329 3.695627
2 4.724255 4.478227 3.536784
3 3.934142 4.203065 4.175473
4 4.184477 3.914167 4.245993
5 4.629850 3.457693 4.190717
"""

print(explanation["shapley_values_sd"])

"""
explain_id none MedInc HouseAge AveRooms AveBedrms \
1 1 3.523652e-08 0.122568 0.124885 0.163694 0.134910
2 2 3.501778e-08 0.125286 0.113064 0.123057 0.129869
3 3 1.805247e-08 0.098208 0.095959 0.115399 0.102265
4 4 3.227380e-08 0.110442 0.118524 0.124688 0.101476
5 5 3.650380e-08 0.125538 0.130427 0.136797 0.131515
1 1 2.128999e-08 0.095949 0.121400 0.109478 0.089252
2 2 1.230629e-08 0.073107 0.079607 0.069861 0.071715
3 3 1.451430e-08 0.072112 0.081075 0.066201 0.063103
4 4 1.736058e-08 0.086020 0.093031 0.095717 0.082699
5 5 1.903768e-08 0.087028 0.097773 0.087086 0.096578

Population AveOccup Latitude Longitude
1 0.133510 0.149141 0.132394 0.121605
2 0.113429 0.124539 0.122773 0.100871
3 0.092633 0.110790 0.090657 0.090542
4 0.114721 0.122266 0.103081 0.105613
5 0.113853 0.139291 0.135377 0.132476
1 0.097459 0.098798 0.128215 0.102793
2 0.068862 0.074687 0.083260 0.073188
3 0.065732 0.072440 0.073805 0.066479
4 0.079885 0.088326 0.102809 0.084057
5 0.091073 0.099251 0.097776 0.098360
"""

explanation["MSEv"]["MSEv"]
"""
MSEv MSEv_sd
1 33.143896 7.986808
MSEv MSEv_sd
1 5.122576 0.874736
"""
26 changes: 19 additions & 7 deletions python/examples/regression_paradigm.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,28 @@
for i, (method, explanation) in enumerate(explanation_list.items()):
print(method, round(explanation["MSEv"]["MSEv"].iloc[0].iloc[0], 3), round(explanation["timing"]["total_time_secs"][0], 3))


"""
Method MSEv Elapsed time (seconds)
empirical 0.826 3.59
sep_lm 1.635 13.578
sep_pca 1.62 8.898
sep_splines 1.62 8.538
sep_tree_cv 1.436 229.078
sep_xgboost 0.773 18.604
sep_xgboost_cv 0.826 246.444
sur_lm 1.772 0.528
sur_rf 0.906 46.533
sur_rf_cv 0.979 1111.281
"""


explanation_list["sep_xgboost"]["shapley_values_est"]

"""
explain_id none MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude
1 1 2.205937 -0.498764 0.193443 -0.073068 0.005078 -0.216733 -0.313781 -0.433844 -0.362689
2 2 2.205938 -0.160032 0.014564 -0.417670 -0.117127 0.084102 0.151612 -0.486576 -0.326138
3 3 2.205938 0.585638 0.239399 0.103826 -0.656533 0.349671 0.859701 0.275356 0.958495
4 4 2.205938 0.311038 -0.114403 0.206639 0.041748 -0.178090 -0.061004 0.036681 0.045110
5 5 2.205938 0.079439 -0.156861 -0.118913 0.093746 -0.097861 0.433192 -0.239588 -0.003852
explain_id none MedInc HouseAge AveRooms AveBedrms Population AveOccup Latitude Longitude
1 1 2.205937 -0.508051 0.182084 -0.078603 0.014051 -0.198895 -0.327370 -0.421074 -0.362502
2 2 2.205938 -0.167961 0.012070 -0.421726 -0.118408 0.075081 0.142799 -0.475425 -0.303695
3 3 2.205938 0.602841 0.260374 0.095350 -0.656260 0.334603 0.856464 0.268199 0.953981
4 4 2.205938 0.329671 -0.107425 0.222738 0.022782 -0.194580 -0.057752 0.032740 0.039545
5 5 2.205938 0.055675 -0.160149 -0.119120 0.087631 -0.058983 0.394378 -0.224046 0.013916
"""
45 changes: 22 additions & 23 deletions python/examples/sklearn_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@

"""
explain_id none MedInc HouseAge AveRooms AveBedrms Population \
1 1 2.205938 -0.631399 0.073367 -0.120932 0.110256 -0.032183
2 2 2.205938 -0.533538 0.048795 -0.391921 -0.169092 0.022795
3 3 2.205938 0.308461 0.601246 -0.094463 -0.181351 0.059955
4 4 2.205938 0.311945 -0.077067 0.039246 0.140439 -0.129731
5 5 2.205938 -0.371848 -0.083760 -0.135069 0.143870 0.104284
1 1 2.205938 -0.623764 0.062273 -0.111969 0.106106 -0.027083
2 2 2.205938 -0.538988 0.049095 -0.423751 -0.193274 0.015560
3 3 2.205938 0.314155 0.577410 -0.074360 -0.166675 0.063638
4 4 2.205938 0.318181 -0.069231 0.042482 0.124511 -0.122143
5 5 2.205938 -0.368407 -0.088046 -0.140630 0.154444 0.084880

AveOccup Latitude Longitude
1 -0.239760 -0.450027 -0.196370
2 0.145005 -0.205826 -0.102814
3 0.928566 0.149806 0.288071
4 -0.126607 0.115072 0.199486
5 0.016557 0.082128 0.314332
1 -0.243124 -0.442461 -0.207026
2 0.157790 -0.181538 -0.071491
3 0.921495 0.148821 0.275805
4 -0.122394 0.107674 0.193703
5 -0.010989 0.102267 0.336973
"""

explanation["MSEv"]["MSEv"]
"""
MSEv MSEv_sd
1 0.53958 0.257247
MSEv MSEv_sd
1 0.556654 0.25165
"""

# Now do this for grouping as well
Expand Down Expand Up @@ -81,23 +81,22 @@
approach = 'empirical',
causal_ordering = causal_ordering,
asymmetric = True,
paired_shap_sampling=False,
phi0 = dfy_train.mean().item()
)
print(explanation_c_a["shapley_values_est"])

"""
explain_id none MedInc HouseAge AveRooms AveBedrms Population \
1 1 2.205938 -1.322186 -0.051417 0.035802 0.002919 -0.046796
2 2 2.205938 -0.464073 -0.093125 -0.639687 0.022725 0.009969
3 3 2.205938 -0.166641 1.108175 0.867212 0.056946 -0.121676
4 4 2.205938 0.579213 -0.218696 -0.083284 0.099674 0.094715
5 5 2.205938 0.059085 -0.214197 -0.000353 0.050200 0.123651
1 1 2.205938 -1.245302 -0.013757 -0.036561 -0.000087 -0.032422
2 2 2.205938 -0.547557 -0.110084 -0.457877 0.000986 -0.016211
3 3 2.205938 -0.301502 0.993866 0.650967 0.008163 -0.054721
4 4 2.205938 0.610580 -0.242452 0.001717 0.044329 0.007417
5 5 2.205938 0.127702 -0.207553 0.033916 -0.006458 -0.010412

AveOccup Latitude Longitude
1 -0.105191 -0.199058 -0.014433
2 -0.003866 -0.092886 0.003678
3 0.945082 -0.140175 0.166631
4 -0.208487 -0.056344 0.080926
5 -0.198311 0.104367 0.064859
1 -0.016386 -0.134272 -0.008260
2 0.024030 -0.082117 0.002232
3 0.731722 0.015783 0.016013
4 -0.057977 0.032417 0.076751
5 -0.162505 0.185926 0.109878
"""
24 changes: 14 additions & 10 deletions python/examples/xgboost_booster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@
approach = 'gaussian',
phi0 = dfy_train.mean().item(),
)

print(explanation["shapley_values_est"])

"""
explain_id none MedInc HouseAge AveRooms AveBedrms Population \
1 1 2.205938 -0.832465 -0.053984 -0.311050 0.054333 -0.078014
2 2 2.205938 -0.813781 -0.053652 -0.356067 -0.081677 -0.076472
3 3 2.205937 0.965412 0.407928 -0.209353 -0.051755 -0.006602
4 4 2.205938 0.297996 -0.040097 0.092800 0.132102 0.046493
...
2 -0.025957 0.074273 0.041371
3 1.077735 0.058107 0.447890
4 -0.358188 0.094037 0.071108
5 0.078736 0.139113 0.182091
1 1 2.205938 -0.833311 -0.057978 -0.306664 0.029773 -0.069634
2 2 2.205938 -0.813821 -0.054397 -0.342272 -0.081619 -0.077637
3 3 2.205937 0.940286 0.441631 -0.201057 -0.027957 0.008278
4 4 2.205938 0.322364 -0.014792 0.081928 0.129271 0.003449
5 5 2.205938 -0.405174 -0.012654 -0.015332 0.037333 0.075758

AveOccup Latitude Longitude
1 -0.224789 -0.100868 -0.078253
2 -0.019725 0.069358 0.028153
3 1.086106 0.018124 0.423951
4 -0.346083 0.106678 0.053437
5 0.081340 0.120895 0.134539
"""

explanation["MSEv"]["MSEv"]

"""
MSEv MSEv_sd
1 1.25449 0.831646
1 1.252105 0.820416
"""
22 changes: 11 additions & 11 deletions python/examples/xgboost_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@

"""
explain_id none MedInc HouseAge AveRooms AveBedrms Population \
1 1 2.205937 -0.644869 0.092354 -0.105347 0.122196 -0.054501
2 2 2.205938 -0.509659 0.087719 -0.503114 -0.160219 0.008278
3 3 2.205938 0.511284 0.744960 0.036120 -0.235612 0.055126
4 4 2.205938 0.389474 -0.104019 -0.026614 0.160248 -0.143182
5 5 2.205938 -0.425561 -0.049056 -0.151081 0.153844 0.088019
1 1 2.205937 -0.656673 0.094587 -0.075444 0.130272 -0.050592
2 2 2.205938 -0.517043 0.072571 -0.511003 -0.182379 0.035779
3 3 2.205938 0.542279 0.722088 0.042727 -0.234849 0.042726
4 4 2.205938 0.354106 -0.094916 -0.013684 0.133045 -0.130711
5 5 2.205938 -0.416277 -0.027874 -0.152649 0.153961 0.098047

AveOccup Latitude Longitude
1 -0.289824 -0.584639 -0.235730
2 0.119529 -0.234297 -0.065501
3 1.095101 0.106010 0.402564
4 -0.244692 0.053750 0.202752
5 0.009034 0.057433 0.306669
1 -0.283411 -0.610243 -0.248855
2 0.107369 -0.200838 -0.061720
3 1.125337 0.095749 0.379497
4 -0.228465 0.068373 0.199969
5 -0.050446 0.060515 0.324025
"""

explanation["MSEv"]["MSEv"]

"""
MSEv MSEv_sd
1 0.838435 0.483059
1 0.835836 0.474904
"""
4 changes: 1 addition & 3 deletions python/install_r_packages.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# Installs the required R-packages
install.packages("remotes", repos = "https://cloud.r-project.org")
remotes::install_github("NorskRegnesentral/shapr")
# Installs the development version of shapr from the master branch on CRAN
install.packages("shapr")
Loading
Loading