|
| 1 | +### Model ### |
| 2 | + |
| 3 | +data(titanic_imputed, package = "DALEX") |
| 4 | + |
| 5 | +model <- ranger::ranger(survived~., data = titanic_imputed, classification = TRUE, probability = TRUE) |
| 6 | + |
| 7 | + |
| 8 | +###DALEXtra### |
| 9 | + |
| 10 | +library(DALEX) |
| 11 | +library(DALEXtra) |
| 12 | + |
| 13 | +explainer <- explain(model = model, |
| 14 | + data = titanic_imputed[,-8], |
| 15 | + y = titanic_imputed$survived) |
| 16 | + |
| 17 | + |
| 18 | +library(mlr) |
| 19 | +titanic_imputed_fct <- titanic_imputed |
| 20 | +titanic_imputed_fct$survived <- as.factor(titanic_imputed_fct$survived) |
| 21 | + |
| 22 | +classif_task <- makeClassifTask(data = titanic_imputed_fct, target = "survived") |
| 23 | +classif_lrn <- makeLearner("classif.svm", predict.type = "prob") |
| 24 | +model_mlr <- train(classif_lrn, classif_task) |
| 25 | + |
| 26 | +explainer_mlr <- explain(model = model_mlr, |
| 27 | + data = titanic_imputed_fct[,-8], |
| 28 | + y = as.numeric(as.character(titanic_imputed_fct$survived))) |
| 29 | + |
| 30 | +### LIME ### |
| 31 | + |
| 32 | +library("lime") |
| 33 | +model_type.dalex_explainer <- DALEXtra::model_type.dalex_explainer |
| 34 | +predict_model.dalex_explainer <- DALEXtra::predict_model.dalex_explainer |
| 35 | + |
| 36 | +lime_johnny_mlr <- predict_surrogate(explainer = explainer_mlr, |
| 37 | + new_observation = titanic_imputed_fct[11, -8], |
| 38 | + n_features = 3, |
| 39 | + n_permutations = 1000, |
| 40 | + type = "lime") |
| 41 | + |
| 42 | +lime_johnny_ranger <- predict_surrogate(explainer = explainer, |
| 43 | + new_observation = titanic_imputed_fct[11, -8], |
| 44 | + n_features = 3, |
| 45 | + n_permutations = 1000, |
| 46 | + type = "lime") |
| 47 | + |
| 48 | +plot(lime_johnny) |
| 49 | +plot(lime_johnny_ranger) |
| 50 | + |
| 51 | +### Break Down ### |
| 52 | + |
| 53 | +pp_ranger_bd_1 <- predict_parts(explainer, new_observation = titanic_imputed[11,]) |
| 54 | +plot(pp_ranger_bd_1) |
| 55 | + |
| 56 | +pp_ranger_bd_2 <- predict_parts(explainer_mlr, new_observation = titanic_imputed[11,]) |
| 57 | +plot(pp_ranger_bd_2) |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +# Zadanko |
| 62 | + |
| 63 | +# Wez dowolny zbior, stworz dowolny model. Wygeneruj oraz porównaj wyjasnienie LIME oraz BreakDown |
| 64 | + |
0 commit comments