From f4459a2ac748fd4b8aeca7c6eb2c1f09df9d33fe Mon Sep 17 00:00:00 2001 From: susanne-207 Date: Tue, 31 Oct 2023 15:26:58 +0100 Subject: [PATCH] update solution --- book/chapters/appendices/solutions.qmd | 40 +++++++++----------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/book/chapters/appendices/solutions.qmd b/book/chapters/appendices/solutions.qmd index 4cf41abb2..f8b0f38ad 100644 --- a/book/chapters/appendices/solutions.qmd +++ b/book/chapters/appendices/solutions.qmd @@ -1168,7 +1168,9 @@ library(mlr3) library(mlr3learners) set.seed(1) -fifa20 = fifa[,5:42] +feat_of_interest = c("age", "skill_ball_control", "skill_curve", "skill_dribbling", "skill_fk_accuracy", "skill_long_passing", "value_eur") +fifa20 = fifa[,feat_of_interest] + task_fifa = as_task_regr(fifa20, target = "value_eur", id = "fifa20") learner = lrn("regr.ranger") @@ -1176,7 +1178,7 @@ learner$train(task_fifa) learner$model ``` -2. Use the permutation importance method to calculate variable importance ranking. Which variable is the most important? Is it surprising? +2. Use the permutation importance method to calculate feature importance ranking. Which feature is the most important? Do you find the results surprising? **With `iml`** @@ -1184,7 +1186,7 @@ learner$model library(iml) model = Predictor$new(learner, data = fifa20, - y = fifa$value_eur) + y = "value_eur") effect = FeatureImp$new(model, loss = "rmse") @@ -1196,7 +1198,7 @@ effect$plot() ```{r solutions-046, warning=FALSE, message=FALSE} library(DALEX) ranger_exp = DALEX::explain(learner, - data = fifa20, + data = fifa20[, setdiff(names(fifa20), "value_eur")], y = fifa$value_eur, label = "Fifa 2020", verbose = FALSE) @@ -1206,34 +1208,32 @@ head(ranger_effect) plot(ranger_effect) ``` -3. Use the Partial Dependence profile to draw the global behavior of the model for this variable. Is it aligned with your expectations? +3. Use the partial dependence plot/profile to draw the global behavior of the model for this feature. Is it aligned with your expectations? **With `iml`** ```{r solutions-047, warning=FALSE, message=FALSE} -num_features = c("movement_reactions", "skill_ball_control", "age") +impfeat = c("skill_ball_control") -effect = FeatureEffects$new(model) -plot(effect, features = num_features) +effect = FeatureEffects$new(model, features = impfeat) +plot(effect) ``` **With `DALEX`** ```{r solutions-048, warning=FALSE, message=FALSE} -num_features = c("movement_reactions", "skill_ball_control", "age") +impfeat = c("skill_ball_control") -ranger_profiles = model_profile(ranger_exp, variables = num_features) +ranger_profiles = model_profile(ranger_exp, variables = impfeat) plot(ranger_profiles) ``` -4 Choose one of the football players. You can choose some well-known striker (e.g. Robert Lewandowski) or a well-known goalkeeper (e.g. Manuel Neuer). The following tasks are worth repeating for several different choices. +4. Choose Robert Lewandowski as a specific example and calculate and plot the Shapley values. Which feature is locally the most important and has the strongest influence on his valuation as a soccer player? ```{r solutions-049, warning=FALSE, message=FALSE} -player_1 = fifa["R. Lewandowski", 5:42] +player_1 = fifa20["R. Lewandowski",] ``` -5. For the selected footballer, calculate and plot the Shapley values. Which variable is locally the most important and has the strongest influence on the valuation of the footballer? - **With `iml`** ```{r solutions-050, warning=FALSE, message=FALSE} @@ -1250,18 +1250,6 @@ ranger_shap = predict_parts(ranger_exp, plot(ranger_shap, show_boxplots = FALSE) ``` -6. For the selected footballer, calculate the Ceteris Paribus / Individual Conditional Expectation profiles to draw the local behavior of the model for this variable. Is it different from the global behavior? - -**With `DALEX`** - -```{r solutions-052, warning=FALSE, message=FALSE} -num_features = c("movement_reactions", "skill_ball_control", "age") - -ranger_ceteris = predict_profile(ranger_exp, player_1) -plot(ranger_ceteris, variables = num_features) + - ggtitle("Ceteris paribus for R. Lewandowski", " ") -``` - ## Solutions to @sec-fairness 1. Load the `adult_train` task and try to build a first model.