Skip to content

Commit

Permalink
clean docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hbaniecki committed May 8, 2020
1 parent 18367a3 commit ac82198
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 61 deletions.
14 changes: 6 additions & 8 deletions R/modelStudio.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#' library("DALEX")
#' library("modelStudio")
#'
#' #:# ex1 classification on 'titanic_imputed' dataset
#' #:# ex1 classification on 'titanic' data
#'
#' # fit a model
#' model_titanic <- glm(survived ~., data = titanic_imputed, family = "binomial")
Expand All @@ -82,7 +82,7 @@
#'
#' \donttest{
#'
#' #:# ex2 regression on 'apartments' dataset
#' #:# ex2 regression on 'apartments' data
#' library("ranger")
#'
#' model_apartments <- ranger(m2.price ~. ,data = apartments)
Expand Down Expand Up @@ -113,13 +113,11 @@
#' #:# ex3 xgboost model on 'HR' dataset
#' library("xgboost")
#'
#' # fit a model
#' HR_matrix <- model.matrix(status == "fired" ~ . -1, HR)
#'
#' # fit a model
#' xgb_matrix <- xgb.DMatrix(HR_matrix, label = HR$status == "fired")
#'
#' params <- list(max_depth = 7, objective = "binary:logistic", eval_metric = "auc")
#'
#' params <- list(max_depth = 3, objective = "binary:logistic", eval_metric = "auc")
#' model_HR <- xgb.train(params, xgb_matrix, nrounds = 300)
#'
#' # create an explainer for the model
Expand Down Expand Up @@ -206,7 +204,7 @@ modelStudio.explainer <- function(explainer,
if (show_info) {
pb <- progress_bar$new(
format = " Calculating :what \n Elapsed time: :elapsedfull ETA::eta", # :percent [:bar]
total = (3*B + 2 + 1)*obs_count + (B + 3*B + B) + 1,
total = (3*B + 2 + 1)*obs_count + (2*B + 3*B + B) + 1,
show_after = 0
)
pb$tick(0, tokens = list(what = "..."))
Expand All @@ -216,7 +214,7 @@ modelStudio.explainer <- function(explainer,
fi <- calculate(
ingredients::feature_importance(
model, data, y, predict_function, variables = variable_names, B = B, N = 10*N),
"ingredients::feature_importance", show_info, pb, B)
"ingredients::feature_importance", show_info, pb, 2*B)

which_numerical <- which_variables_are_numeric(data)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ test_matrix <- model.matrix(survived ~.-1, test)

# fit a model
xgb_matrix <- xgb.DMatrix(train_matrix, label = train$survived)
params <- list(max_depth = 7, objective = "binary:logistic", eval_metric = "auc")
params <- list(max_depth = 3, objective = "binary:logistic", eval_metric = "auc")
model <- xgb.train(params, xgb_matrix, nrounds = 500)

# create an explainer for the model
Expand Down
6 changes: 3 additions & 3 deletions pkgdown/favicon/caret.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/demo.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/h2o.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/keras.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/lightgbm.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/mlr.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/mlr3.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/parsnip.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/scikitlearn.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pkgdown/favicon/xgboost.html

Large diffs are not rendered by default.

51 changes: 42 additions & 9 deletions vignettes/ms-perks-features.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Let's use `HR` dataset to explore `modelStudio` parameters:

```{r results="hide"}
train <- DALEX::HR
train$fired <- ifelse(train$status == "fired", 1, 0)
train$fired <- as.factor(ifelse(train$status == "fired", 1, 0))
train$status <- NULL
head(train)
Expand All @@ -42,7 +42,7 @@ Prepare `HR_test` data and a `ranger` model for the explainer:
```{r results="hide", eval = FALSE}
# fit a ranger model
library("ranger")
model <- ranger(fired ~., data = train)
model <- ranger(fired ~., data = train, probability = TRUE)
# prepare validation dataset
test <- DALEX::HR_test[1:1000,]
Expand Down Expand Up @@ -189,7 +189,7 @@ modelStudio(explainer,

--------------------------------------------------------------------

## plot options
## additional options

Customize some of `modelStudio` looks by overwriting default options returned
by the `ms_options()` function. Full list of options:
Expand All @@ -212,8 +212,44 @@ modelStudio(explainer,
options = new_options)
```

All visual options can be changed after the calculations using `ms_update_options()`.

```{r eval = FALSE}
old_ms <- modelStudio(explainer)
old_ms
# update the options
new_ms <- ms_update_options(old_ms,
time = 0,
facet_dim = c(1,2),
margin_left = 150)
new_ms
```

-------------------------------------------------------------------

## update observations

Use `ms_update_observations()` to add more observations with their local explanations to the `modelStudio`.

```{r eval = FALSE}
old_ms <- modelStudio(explainer)
old_ms
# add new observations
plus_ms <- ms_update_observations(old_ms,
explainer,
new_observation = test[101:102,])
plus_ms
# overwrite old observations
new_ms <- ms_update_observations(old_ms,
explainer,
new_observation = test[103:104,],
overwrite = TRUE)
new_ms
```

## DALEXtra

Use `explain_*()` functions from the [DALEXtra](https://github.com/ModelOriented/DALEXtra)
Expand All @@ -225,10 +261,8 @@ library(DALEXtra)
library(mlr)
# fit a model
task <- makeRegrTask(id = "task", data = train, target = "fired")
learner <- makeLearner("regr.ranger", predict.type = "response")
task <- makeClassifTask(id = "task", data = train, target = "fired")
learner <- makeLearner("classif.ranger", predict.type = "prob")
model <- train(learner, task)
# create an explainer for the model
Expand All @@ -238,8 +272,7 @@ explainer_mlr <- explain_mlr(model,
label = "mlr")
# make a studio for the model
modelStudio(explainer_mlr,
B = 10)
modelStudio(explainer_mlr)
```

## References
Expand Down
18 changes: 5 additions & 13 deletions vignettes/ms-r-python-examples.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ train$survived <- as.factor(train$survived)
# fit a model
task <- TaskClassif$new(id = "titanic", backend = train, target = "survived")
learner <- lrn("classif.ranger", predict_type = "prob")
learner$train(task)
# create an explainer for the model
Expand Down Expand Up @@ -137,9 +135,7 @@ test_matrix <- model.matrix(survived ~.-1, test)
# fit a model
xgb_matrix <- xgb.DMatrix(train_matrix, label = train$survived)
params <- list(max_depth = 7, objective = "binary:logistic", eval_metric = "auc")
params <- list(max_depth = 3, objective = "binary:logistic", eval_metric = "auc")
model <- xgb.train(params, xgb_matrix, nrounds = 500)
# create an explainer for the model
Expand Down Expand Up @@ -179,9 +175,8 @@ test <- data[-index,]
train$survived <- as.factor(train$survived)
# fit a model
cv <- trainControl(method = "repeatedcv", number = 3, repeats = 10)
model <- train(survived ~ ., data = train, method = "gbm", trControl = cv)
cv <- trainControl(method = "repeatedcv", number = 3, repeats = 3)
model <- train(survived ~ ., data = train, method = "gbm", trControl = cv, verbose = FALSE)
# create an explainer for the model
explainer <- explain(model,
Expand Down Expand Up @@ -212,6 +207,7 @@ data <- DALEX::titanic_imputed
# init h2o
h2o.init()
h2o.no_progress()
# split the data
h2o_split <- h2o.splitFrame(as.h2o(data))
Expand All @@ -223,12 +219,8 @@ train$survived <- as.factor(train$survived)
# fit a model
automl <- h2o.automl(y = "survived", training_frame = train, max_runtime_secs = 30)
model <- automl@leader
# stop h2o progress printing
h2o.no_progress()
# create an explainer for the model
explainer <- explain_h2o(model,
data = test,
Expand Down Expand Up @@ -266,7 +258,7 @@ train <- data[index,]
test <- data[-index,]
# fit a model
rand_forest() %>%
model <- rand_forest() %>%
set_engine("ranger", importance = "impurity") %>%
set_mode("regression") %>%
fit(m2.price ~ ., data = train)
Expand Down

0 comments on commit ac82198

Please sign in to comment.