if (require("randomForest")) {
# Train a model
rf = randomForest(Species ~ ., data = iris)
# Create a predictor object
predictor = iml::Predictor$new(rf, type = "prob")
# Find counterfactuals for x_interest
wi_classif = WhatIfClassif$new(predictor, n_counterfactuals = 5L)
cfactuals = wi_classif$find_counterfactuals(
x_interest = iris[150L, ], desired_class = "versicolor", desired_prob = c(0.5, 1)
)
# Print the results
cfactuals$data
}
Run the code above in your browser using DataLab