This notebook shows how to use the What-if Tool on a deployed Cloud AI Platform model. You don't need your own cloud project to run this notebook.
For instructions on creating a Cloud project, see the documentation here.
In [ ]:
import sys
python_version = sys.version_info[0]
In [0]:
# If you're running on Colab, you'll need to install the What-if Tool package and authenticate on the TF instance
def pip_install(module):
if python_version == '2':
!pip install {module} --quiet
else:
!pip3 install {module} --quiet
try:
import google.colab
IN_COLAB = True
except:
IN_COLAB = False
if IN_COLAB:
pip_install('witwidget')
from google.colab import auth
auth.authenticate_user()
In [0]:
import pandas as pd
import numpy as np
import witwidget
from witwidget.notebook.visualization import WitWidget, WitConfigBuilder
The model we'll be exploring here is a binary classification model built with XGBoost and trained on a mortgage dataset. It predicts whether or not a mortgage application will be approved. In this section we'll:
In [0]:
# Download our Pandas dataframe and our test features and labels
!gsutil cp gs://mortgage_dataset_files/data.pkl .
!gsutil cp gs://mortgage_dataset_files/x_test.npy .
!gsutil cp gs://mortgage_dataset_files/y_test.npy .
In [0]:
# Preview the features from our model as a pandas DataFrame
features = pd.read_pickle('data.pkl')
features.head()
In [0]:
# Load the test features and labels into numpy ararys
x_test = np.load('x_test.npy')
y_test = np.load('y_test.npy')
In [0]:
# Combine the features and labels into one array for the What-if Tool
test_examples = np.hstack((x_test,y_test.reshape(-1,1)))
With our test examples ready, we can now connect our model to the What-if Tool using the WitWidget
. To use the What-if Tool with Cloud AI Platform, we need to send it:
See the next cell for some exploration ideas in the What-if Tool.
In [0]:
# Create a What-if Tool visualization, it may take a minute to load
# See the cell below this for exploration ideas
# This prediction adjustment function is needed as this xgboost model's
# prediction returns just a score for the positive class of the binary
# classification, whereas the What-If Tool expects a list of scores for each
# class (in this case, both the negative class and the positive class).
def adjust_prediction(pred):
return [1 - pred, pred]
config_builder = (WitConfigBuilder(test_examples.tolist(), features.columns.tolist() + ['mortgage_status'])
.set_ai_platform_model('wit-caip-demos', 'xgb_mortgage', 'v1', adjust_prediction=adjust_prediction)
.set_target_feature('mortgage_status')
.set_label_vocab(['denied', 'approved']))
WitWidget(config_builder, height=800)
Individual data points: the default graph shows all data points from the test set, colored by their ground truth label (approved or denied)
Binning data: create separate graphs for individual features
Exploring overall performance: Click on the "Performance & Fairness" tab to view overall performance statistics on the model's results on the provided dataset, including confusion matrices, PR curves, and ROC curves.