<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 LABXX
Step1:  Loading the mortgage test dataset
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
Step2:  Preview the Features
Preview the features from our model as a pandas DataFrame
Step3:  Load the test features and labels into numpy arrays
Developing machine learning models in Python often requires the use of NumPy arrays.  Recall that NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays.  NumPy arrays are efficient data structures for working with data in Python, and machine learning models like those in the scikit-learn library, and deep learning models like those in the Keras library, expect input data in the format of NumPy arrays and make predictions in the format of NumPy arrays.  As such, it is common to need to save NumPy arrays to file.  Note that the data info reveals the following datatypes dtypes
Step4:  Let's take a look at the contents of the 'x_test.npy' file.  You can see the "array" structure.
Step5:  Combine the features and labels into one array for the What-if Tool
Note that the numpy.hstack() function is used to stack the sequence of input arrays horizontally (i.e. column wise) to make a single array.  In the following example, the numpy matrix is reshaped into a vector using the reshape function with .reshape((-1, 1) to convert the array into a single column matrix.
Step6:  Using the What-if Tool to interpret our model
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
Step7:  Run this cell to load the WIT config builder.  NOTE
<ASSISTANT_TASK:>
Python Code:
import sys
python_version = sys.version_info[0]
print("Python Version: ", python_version)
!pip3 install witwidget
import numpy as np
import pandas as pd
import witwidget
from witwidget.notebook.visualization import WitConfigBuilder, WitWidget
Explanation: LABXX: What-if Tool: Model Interpretability Using Mortgage Data
Learning Objectives
Create a What-if Tool visualization
What-if Tool exploration using the XGBoost Model
Introduction
This notebook shows how to use the What-if Tool (WIT) on a deployed Cloud AI Platform model. The What-If Tool provides an easy-to-use interface for expanding understanding of black-box classification and regression ML models. With the plugin, you can perform inference on a large set of examples and immediately visualize the results in a variety of ways. Additionally, examples can be edited manually or programmatically and re-run through the model in order to see the results of the changes. It contains tooling for investigating model performance and fairness over subsets of a dataset.  The purpose of the tool is to give people a simple, intuitive, and powerful way to explore and investigate trained ML models through a visual interface with absolutely no code required.
Extreme Gradient Boosting (XGBoost) is a decision-tree-based ensemble Machine Learning algorithm that uses a gradient boosting framework. In prediction problems involving unstructured data (images, text, etc.) artificial neural networks tend to outperform all other algorithms or frameworks. However, when it comes to small-to-medium structured/tabular data, decision tree based algorithms are considered best-in-class right now. Please see the chart below for the evolution of tree-based algorithms over the years.
You don't need your own cloud project to run this notebook. 
 UPDATE LINK BEFORE PRODUCTION :  Each learning objective will correspond to a #TODO in the student lab notebook) -- try to complete that notebook first before reviewing this solution notebook.
Set up environment variables and load necessary libraries
We will start by importing the necessary libraries for this lab.
End of explanation
# 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 .
Explanation: Loading the mortgage test dataset
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:
Download some test data from Cloud Storage and load it into a numpy array + Pandas DataFrame
Preview the features for our model in Pandas
End of explanation
features = pd.read_pickle("data.pkl")
features.head()
features.info()
Explanation: Preview the Features
Preview the features from our model as a pandas DataFrame
End of explanation
x_test = np.load("x_test.npy")
y_test = np.load("y_test.npy")
Explanation: Load the test features and labels into numpy arrays
Developing machine learning models in Python often requires the use of NumPy arrays.  Recall that NumPy, which stands for Numerical Python, is a library consisting of multidimensional array objects and a collection of routines for processing those arrays.  NumPy arrays are efficient data structures for working with data in Python, and machine learning models like those in the scikit-learn library, and deep learning models like those in the Keras library, expect input data in the format of NumPy arrays and make predictions in the format of NumPy arrays.  As such, it is common to need to save NumPy arrays to file.  Note that the data info reveals the following datatypes dtypes: float64(8), int16(1), int8(1), uint8(34) -- and no strings or "objects". So, let's now load the features and labels into numpy arrays.
End of explanation
print(x_test)
Explanation: Let's take a look at the contents of the 'x_test.npy' file.  You can see the "array" structure.
End of explanation
test_examples = np.hstack((x_test, y_test.reshape(-1, 1)))
Explanation: Combine the features and labels into one array for the What-if Tool
Note that the numpy.hstack() function is used to stack the sequence of input arrays horizontally (i.e. column wise) to make a single array.  In the following example, the numpy matrix is reshaped into a vector using the reshape function with .reshape((-1, 1) to convert the array into a single column matrix.
End of explanation
# ******** DO NOT RUN THIS CELL ********
# TODO 1
PROJECT_ID = "YOUR_PROJECT_ID"
MODEL_NAME = "YOUR_MODEL_NAME"
VERSION_NAME = "YOUR_VERSION_NAME"
TARGET_FEATURE = "mortgage_status"
LABEL_VOCAB = ["denied", "approved"]
# TODO 1a
config_builder = (
    WitConfigBuilder(
        test_examples.tolist(), features.columns.tolist() + ["mortgage_status"]
    )
    .set_ai_platform_model(
        PROJECT_ID,
        MODEL_NAME,
        VERSION_NAME,
        adjust_prediction=adjust_prediction,
    )
    .set_target_feature(TARGET_FEATURE)
    .set_label_vocab(LABEL_VOCAB)
)
Explanation: Using the What-if Tool to interpret our model
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:
* A Python list of our test features + ground truth labels
* Optionally, the names of our columns
* Our Cloud project, model, and version name (we've created a public one for you to play around with)
See the next cell for some exploration ideas in the What-if Tool.
Create a What-if Tool visualization
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).  
NOTE: The WIT may take a minute to load.  While it is loading, review the parameters that are defined in the next cell, BUT NOT RUN IT, it is simply for reference.
End of explanation
# TODO 1b
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)
Explanation: Run this cell to load the WIT config builder.  NOTE: The WIT may take a minute to load
End of explanation
<END_TASK>