The modelStudio
package automates the Explanatory Analysis of Machine Learning predictive models. Generate advanced interactive and animated model explanations in the form of a serverless HTML site with only one line of code. This tool is model agnostic, therefore compatible with most of the black box predictive models and frameworks (e.g. mlr/mlr3
, xgboost
, caret
, h2o
, scikit-learn
, lightGBM
, keras/tensorflow
).
The main modelStudio()
function computes various (instance and dataset level) model explanations and produces an interactive, customisable dashboard made with D3.js. It consists of multiple panels for plots with their short descriptions. Easily save and share the dashboard with others. Tools for model exploration unite with tools for EDA (Exploratory Data Analysis) to give a broad overview of the model behavior.
β β β β β β explain FIFA20 β R & Python examples β More Resources β FAQ & Troubleshooting
The modelStudio
package is a part of the DrWhy.AI universe.
# Install from CRAN:
install.packages("modelStudio")
# Install the development version from GitHub:
devtools::install_github("ModelOriented/modelStudio")
library("DALEX")
library("modelStudio")
# fit a model
model <- glm(survived ~., data = titanic_imputed, family = "binomial")
# create an explainer for the model
explainer <- explain(model,
data = titanic_imputed,
y = titanic_imputed$survived,
label = "Titanic GLM")
# make a studio for the model
modelStudio(explainer)
Save the output in the form of a HTML file - Demo Dashboard.
R & Python Examples more
The modelStudio()
function uses DALEX
explainers created with DALEX::explain()
or DALEXtra::explain_*()
.
# packages for explainer objects
install.packages("DALEX")
install.packages("DALEXtra")
# update main dependencies
install.packages("ingredients")
install.packages("iBreakDown")
mlr dashboard
In this example we will fit a ranger
model on titanic
data.
# load packages and data
library(mlr)
library(DALEXtra)
library(modelStudio)
data <- DALEX::titanic_imputed
# split the data
index <- sample(1:nrow(data), 0.7*nrow(data))
train <- data[index,]
test <- data[-index,]
# mlr ClassifTask takes target as factor
train$survived <- as.factor(train$survived)
# fit a model
task <- makeClassifTask(id = "titanic", data = train, target = "survived")
learner <- makeLearner("classif.ranger", predict.type = "prob")
model <- train(learner, task)
# create an explainer for the model
explainer <- explain_mlr(model,
data = test,
y = test$survived,
label = "mlr")
# pick observations
new_observation <- test[1:2,]
rownames(new_observation) <- c("id1", "id2")
# make a studio for the model
modelStudio(explainer,
new_observation)
xgboost dashboard
In this example we will fit a xgboost
model on titanic
data.
# load packages and data
library(xgboost)
library(DALEX)
library(modelStudio)
data <- DALEX::titanic_imputed
# split the data
index <- sample(1:nrow(data), 0.7*nrow(data))
train <- data[index,]
test <- data[-index,]
train_matrix <- model.matrix(survived ~.-1, train)
test_matrix <- model.matrix(survived ~.-1, test)
# fit a model
xgb_matrix <- xgb.DMatrix(train_matrix, label = train$survived)
params <- list(max_depth = 7, objective = "binary:logistic", eval_metric = "auc")
model <- xgb.train(params, xgb_matrix, nrounds = 500)
# create an explainer for the model
explainer <- explain(model,
data = test_matrix,
y = test$survived,
label = "xgboost")
# pick observations
new_observation <- test_matrix[1:2, , drop=FALSE]
rownames(new_observation) <- c("id1", "id2")
# make a studio for the model
modelStudio(explainer,
new_observation,
options = modelStudioOptions(margin_left = 140))
scikit-learn dashboard
The modelStudio()
function uses dalex
explainers created with dalex.Explainer()
.
pip3 install dalex --force
Use pickle
Python module and reticulate
R package to easily make a studio for a model.
# package for pickle load
install.packages("reticulate")
In this example we will fit a Pipeline MLPClassifier
model on titanic
data.
First, use dalex
in Python:
# load packages and data
import dalex as dx
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.neural_network import MLPClassifier
data = dx.datasets.load_titanic()
X = data.drop(columns='survived')
y = data.survived
# split the data
X_train, X_test, y_train, y_test = train_test_split(X, y)
# fit a pipeline model
numerical_features = ['age', 'fare', 'sibsp', 'parch']
numerical_transformer = Pipeline(
steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
]
)
categorical_features = ['gender', 'class', 'embarked']
categorical_transformer = Pipeline(
steps=[
('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
]
)
preprocessor = ColumnTransformer(
transformers=[
('num', numerical_transformer, numerical_features),
('cat', categorical_transformer, categorical_features)
]
)
classifier = MLPClassifier(hidden_layer_sizes=(150,100,50), max_iter=500)
model = Pipeline(
steps=[
('preprocessor', preprocessor),
('classifier', classifier)
]
)
model.fit(X_train, y_train)
# create an explainer for the model
explainer = dx.Explainer(model, data=X_test, y=y_test, label='scikit-learn')
#! remove residual_function before dump !
explainer.residual_function = None
# pack the explainer into a pickle file
import pickle
pickle_out = open('explainer_scikitlearn.pickle', 'wb')
pickle.dump(explainer, pickle_out)
pickle_out.close()
Then, use modelStudio
in R:
# load the explainer from the pickle file
library(reticulate)
explainer <- py_load_object("explainer_scikitlearn.pickle", pickle = "pickle")
# make a studio for the model
library(modelStudio)
modelStudio(explainer)
Save modelStudio
as a HTML file using buttons on the top of the RStudio Viewer
or with r2d3::save_d3_html()
.
-
Theoretical introduction to the plots: Explanatory Model Analysis. Explore, Explain and Examine Predictive Models.
-
Vignette: modelStudio - R & Python examples
-
Vignette: modelStudio - perks and features
-
Conference poster: MLinPL2019
-
Changelog: News
Work on this package was financially supported by the NCN Opus grant 2016/21/B/ST6/02176
.