Last active
May 31, 2018 13:48
-
-
Save yabyzq/f3fb677c7d886a87857ad248863d6adc to your computer and use it in GitHub Desktop.
R - model explainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#xgboost explainer | |
#library(devtools) | |
#install_github("AppliedDataSciencePartners/xgboostExplainer") | |
library(xgboost) | |
library(xgboostExplainer) | |
#getting data | |
set.seed(123) | |
data(agaricus.train, package='xgboost') | |
X = as.matrix(agaricus.train$data) | |
y = agaricus.train$label | |
#split train test | |
train_idx = 1:5000 | |
train.data = X[train_idx,] | |
test.data = X[-train_idx,] | |
#convert to matrix | |
xgb.train.data <- xgb.DMatrix(train.data, label = y[train_idx]) | |
xgb.test.data <- xgb.DMatrix(test.data) | |
#run model | |
param <- list(objective = "binary:logistic") | |
xgb.model <- xgboost(param =param, data = xgb.train.data, nrounds=10, early_stopping_rounds = 2) | |
col_names = colnames(X) | |
pred.train = predict(xgb.model,X) | |
nodes.train = predict(xgb.model,X,predleaf =TRUE) | |
trees = xgb.model.dt.tree(col_names, model = xgb.model) | |
#### The XGBoost Explainer | |
explainer = buildExplainer(xgb.model,xgb.train.data, type="binary", base_score = 0.5, n_first_tree= 1) | |
pred.breakdown = explainPredictions(xgb.model, explainer, xgb.test.data) | |
showWaterfall(xgb.model, explainer, xgb.test.data, test.data, 2, type = "binary") | |
showWaterfall(xgb.model, explainer, xgb.test.data, test.data, 8, type = "binary") | |
#live | |
#https://mi2datalab.github.io/live/ | |
library(e1071) | |
library(live) | |
library(mlr) | |
library(forestmodel) | |
library(xgboost) | |
library(xgboostExplainer) | |
head(wine) | |
wine_svm <- e1071::svm(quality ~., data = wine) | |
similar <- sample_locally2(data = wine, | |
explained_instance = wine[10, ], | |
explained_var = "quality", | |
size = 500) | |
similar1 <- add_predictions2(to_explain = similar, | |
black_box_model = wine_svm) | |
wine_expl <- fit_explanation2(live_object = similar1, | |
white_box = "regr.lm") | |
plot_explanation2(wine_expl, "forest") | |
plot_explanation2(wine_expl, "waterfall") | |
#Lime | |
https://cran.r-project.org/web/packages/lime/vignettes/Understanding_lime.html | |
#XGBOOST | |
X <- as.matrix(wine[,1:11]) | |
xgb.train.data <- xgb.DMatrix(X, label = wine$quality>6 ) | |
param <- list(objective = "binary:logistic") | |
xgb.model <- xgboost(param =param, data = xgb.train.data, nrounds=20, early_stopping_rounds = 2) | |
col_names = colnames(X) | |
pred.train = predict(xgb.model,X) | |
nodes.train = predict(xgb.model,X,predleaf =TRUE) | |
trees = xgb.model.dt.tree(col_names, model = xgb.model) | |
#### The XGBoost Explainer | |
explainer = buildExplainer(xgb.model,xgb.train.data, type="binary", base_score = 0.5, n_first_tree= 1) | |
pred.breakdown = explainPredictions(xgb.model, explainer, xgb.train.data) | |
showWaterfall(xgb.model, explainer, xgb.train.data, X, 100, type = "binary", threshold = 1e-04, limits = c(NA, NA)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment