-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from bgreenwell/devel
fix print issues
- Loading branch information
Showing
10 changed files
with
90 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
#' @keywords internal | ||
#' | ||
#' @export | ||
print.explain <- function(x, ...) { | ||
print(data.matrix(as.data.frame(x))) | ||
invisible(x) | ||
} | ||
#' #' @keywords internal | ||
#' #' | ||
#' #' @export | ||
#' print.explain <- function(x, ...) { | ||
#' if (is.matrix(x)) { | ||
#' x <- data.matrix(as.data.frame(x)) | ||
#' } | ||
#' print(x) | ||
#' invisible(x) | ||
#' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Exits | ||
if (!requireNamespace("shapviz", quietly = TRUE)) { | ||
exit_file("Package shapviz missing") | ||
} | ||
if (!requireNamespace("ranger", quietly = TRUE)) { | ||
exit_file("Package ranger missing") | ||
} | ||
|
||
library(shapviz) | ||
|
||
# Read in the data and clean it up a bit | ||
set.seed(2220) # for reproducibility | ||
trn <- gen_friedman(500) | ||
tst <- gen_friedman(10) | ||
|
||
# Features only | ||
X <- subset(trn, select = -y) | ||
newX <- subset(tst, select = -y) | ||
|
||
# Fit a default random forest | ||
set.seed(2222) # for reproducibility | ||
rfo <- ranger::ranger(y ~ ., data = trn) | ||
|
||
# Prediction wrapper | ||
pfun <- function(object, newdata) { | ||
predict(object, data = newdata)$predictions | ||
} | ||
|
||
# Generate explanations for test set | ||
set.seed(2024) # for reproducibility | ||
ex1 <- explain(rfo, X = X, newdata = newX, pred_wrapper = pfun, adjust = TRUE, | ||
nsim = 50) | ||
|
||
# Same, but set `shap_only = FALSE` for convenience with shapviz | ||
set.seed(2024) # for reproducibility | ||
ex2 <- explain(rfo, X = X, newdata = newX, pred_wrapper = pfun, adjust = TRUE, | ||
nsim = 50, shap_only = FALSE) | ||
|
||
# Create "shapviz" objects | ||
shv1 <- shapviz(ex1, X = newX) | ||
shv2 <- shapviz(ex2) | ||
shv3 <- shapviz(ex2$shapley_values, X = newX, baseline = ex2$baseline) | ||
|
||
# Expectations | ||
expect_error(shapviz(ex1)) | ||
expect_identical(ex2$baseline, mean(pfun(rfo, X))) | ||
expect_identical(shv1$X, shv2$X) | ||
expect_identical(shv1$X, shv3$X) | ||
expect_identical(shv1$baseline, shv2$baseline) | ||
expect_identical(shv1$baseline, shv3$baseline) | ||
|
||
# # SHAP waterfall plots | ||
# sv_waterfall(shv1, row_id = 1) | ||
# sv_waterfall(shv2, row_id = 1) | ||
# sv_waterfall(shv3, row_id = 1) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters