Skip to content

Instantly share code, notes, and snippets.

@SirmaXX
Created October 31, 2023 01:27
Show Gist options
  • Save SirmaXX/6b077edbd9c784549ad3bed474657e9b to your computer and use it in GitHub Desktop.
Save SirmaXX/6b077edbd9c784549ad3bed474657e9b to your computer and use it in GitHub Desktop.
xai examples codes
library("rms")
library("randomForest")
library("gbm")
library("DALEX")
set.seed(1)
library("DALEXtra")
library("lime")
#https://medium.com/analytics-vidhya/a-beginners-guide-to-learning-r-with-the-titanic-dataset-a630bc5495a8
#https://ema.drwhy.ai/breakDown.html#advanced-use-of-the-predict_parts-function
titanicdataset = read.csv("/home/deniz/Masaüstü/yükseklisans/ayz/code/train.csv", na.strings = "")
View(titanicdataset)
##############################################BREAKDOWN PLOTS FOR ADDİTİVE ATTRİBUTES ####################################################################
##modellerin okunması
titanic_imputed <- archivist::aread("pbiecek/models/27e5c")
titanic_rf <- archivist:: aread("pbiecek/models/4e0fc")
(henry <- archivist::aread("pbiecek/models/a6538"))
##modellerin okunması
explain_rf <- DALEX::explain(model = titanic_rf,
data = titanic_imputed[, -9],
y = titanic_imputed$survived == "yes",
label = "Random Forest")
bd_rf <- predict_parts(explainer = explain_rf,
new_observation = henry,
type = "break_down")
bd_rf
plot(bd_rf)
bd_rf_order <- predict_parts(explainer = explain_rf,
new_observation = henry,
type = "break_down",
order = c("class", "age", "gender", "fare",
"parch", "sibsp", "embarked"))
plot(bd_rf_order, max_features = 3)
bd_rf_distr <- predict_parts(explainer = explain_rf,
new_observation = henry,
type = "break_down",
order = c("age", "class", "fare", "gender",
"embarked", "sibsp", "parch"),
keep_distributions = TRUE)
plot(bd_rf_distr, plot_distributions = TRUE)
##############################################BREAKDOWN PLOTS FOR ADDİTİVE ATTRİBUTES ####################################################################
############################################# #BREAKDOWN PLOTS FOR Intereaction s####################################################################
explain_rf <- DALEX::explain(model = titanic_rf,
data = titanic_imputed[, -9],
y = titanic_imputed$survived == "yes",
label = "Random Forest")
bd_rf <- predict_parts(explainer = explain_rf,
new_observation = henry,
type = "break_down_interactions")
bd_rf
plot(bd_rf)
############################################# #BREAKDOWN PLOTS FOR Intereaction s####################################################################
############################################# SHAPLEY ADDTİVE ####################################################################
shap_henry <- predict_parts(explainer = explain_rf,
new_observation = henry,
type = "shap",
B = 25)
shap_henry
plot(shap_henry)
############################################# SHAPLEY ADDTİVE ####################################################################
############################################# LİME ####################################################################
set.seed(1)
library("DALEXtra")
library("lime")
model_type.dalex_explainer <- DALEXtra::model_type.dalex_explainer
predict_model.dalex_explainer <- DALEXtra::predict_model.dalex_explainer
johnny_d <- archivist:: aread("pbiecek/models/e3596")
titanic_rf_exp <- DALEX::explain(model = titanic_rf,
data = titanic_imputed[, -9],
y = titanic_imputed$survived == "yes",
label = "Random Forest")
lime_johnny <- predict_surrogate(explainer = titanic_rf_exp,
new_observation = johnny_d,
n_features = 3,
n_permutations = 1000,
type = "lime")
(as.data.frame(lime_johnny))
plot(lime_johnny)
############################################# LİME ####################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment