%pip install -q validmind
Document multiple results for the same test
Documentation templates facilitate the presentation of multiple unique test results for a single test.
Consider various scenarios where you may intend to showcase results of the same test with diverse inputs:
- Comparing test results with varied parameter values: Illustrate model performance by contrasting test results achieved with different parameter values to identify optimal settings.
- Displaying test results with distinct datasets: Showcase test versatility by presenting results on diverse datasets, such as providing confusion matrices for both training and test data.
- Model comparison: Conduct a comprehensive model evaluation by comparing tests like
ROC curve
andAccuracy
to discern and select the superior-performing model.
This interactive notebook guides you through the process of documenting a model with the ValidMind Library. It uses the Bank Customer Churn Prediction sample dataset from Kaggle to train a simple classification model. As part of the notebook, you will learn how to render more than one unique test result for the same test while exploring how the documentation process works:
- Initializing the ValidMind Library
- Loading a sample dataset provided by the library to train a simple classification model
- Running a ValidMind test suite to quickly generate documentation about the data and model
Contents
About ValidMind
ValidMind is a platform for managing model risk, including risk associated with AI and statistical models.
You use the ValidMind Library to automate documentation and validation tests, and then use the ValidMind AI Risk Platform UI to collaborate on model documentation. Together, these products simplify model risk management, facilitate compliance with regulations and institutional standards, and enhance collaboration between yourself and model validators.
Before you begin
This notebook assumes you have basic familiarity with Python, including an understanding of how functions work. If you are new to Python, you can still run the notebook but we recommend further familiarizing yourself with the language.
If you encounter errors due to missing modules in your Python environment, install the modules with pip install
, and then re-run the notebook. For more help, refer to Installing Python Modules.
New to ValidMind?
If you haven’t already seen our Get started with the ValidMind Library, we recommend you explore the available resources for developers at some point. There, you can learn more about documenting models, find code samples, or read our developer reference.
Signing up is FREE — Register with ValidMind
Key concepts
Model documentation: A structured and detailed record pertaining to a model, encompassing key components such as its underlying assumptions, methodologies, data sources, inputs, performance metrics, evaluations, limitations, and intended uses. It serves to ensure transparency, adherence to regulatory requirements, and a clear understanding of potential risks associated with the model’s application.
Documentation template: Functions as a test suite and lays out the structure of model documentation, segmented into various sections and sub-sections. Documentation templates define the structure of your model documentation, specifying the tests that should be run, and how the results should be displayed.
Tests: A function contained in the ValidMind Library, designed to run a specific quantitative test on the dataset or model. Tests are the building blocks of ValidMind, used to evaluate and document models and datasets, and can be run individually or as part of a suite defined by your model documentation template.
Custom tests: Custom tests are functions that you define to evaluate your model or dataset. These functions can be registered with ValidMind to be used in the platform.
Inputs: Objects to be evaluated and documented in the ValidMind framework. They can be any of the following:
- model: A single model that has been initialized in ValidMind with
vm.init_model()
. - dataset: Single dataset that has been initialized in ValidMind with
vm.init_dataset()
. - models: A list of ValidMind models - usually this is used when you want to compare multiple models in your custom test.
- datasets: A list of ValidMind datasets - usually this is used when you want to compare multiple datasets in your custom test. See this example for more information.
Parameters: Additional arguments that can be passed when running a ValidMind test, used to pass additional information to a test, customize its behavior, or provide additional context.
Outputs: Custom tests can return elements like tables or plots. Tables may be a list of dictionaries (each representing a row) or a pandas DataFrame. Plots may be matplotlib or plotly figures.
Test suites: Collections of tests designed to run together to automate and generate model documentation end-to-end for specific use-cases.
Example: the classifier_full_suite
test suite runs tests from the tabular_dataset
and classifier
test suites to fully document the data and model sections for binary classification model use-cases.
Install the client library
The client library provides Python support for the ValidMind Library. To install it:
Initialize the client library
ValidMind generates a unique code snippet for each registered model to connect with your developer environment. You initialize the client library with this code snippet, which ensures that your documentation and tests are uploaded to the correct model when you run the notebook.
Get your code snippet:
In a browser, log in to ValidMind.
In the left sidebar, navigate to Model Inventory and click + Register new model.
Enter the model details, making sure to select Binary classification as the template and Marketing/Sales - Attrition/Churn Management as the use case, and click Continue. (Need more help?)
Go to Getting Started and click Copy snippet to clipboard.
Next, replace this placeholder with your own code snippet:
# Replace with your code snippet
import validmind as vm
="...", api_key="...", api_secret="...", model="...") vm.init(api_host
Update the customer churn demo template
Before you initialize the client library by running the notebook, edit the Binary classification template to make a copy of a test of interest and update it with different result_id
fields for each entry:
- Go to Settings > Templates and click on the Binary classification template. Let’s say we want to show
Skewness
results fortraining
andtest
datasets.
To do this we replace
- content_type: test
content_id: validmind.data_validation.Skewness
with
- content_type: test
content_id: validmind.data_validation.Skewness:training_data
- content_type: test
content_id: validmind.data_validation.Skewness:test_data
This way, we can show two results of the same test in the model document. Here, the training_data
and test_data
could be any string. However, they should be unique for the same test.
- Click on Prepare new version, provide some version notes and click on Save new version to save a new version of this template.
- Next, we need to swap our model documentation to use this new version of the template. Follow the steps on Swap documentation templates to swap the template of our customer churn model.
In the following sections we provide more context on how these content_id
fields mentioned earlier get mapped to the actual tests.
Initialize the Python environment
Next, let’s import the necessary libraries and set up your Python environment for data analysis:
import pandas as pd
import xgboost as xgb
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
%matplotlib inline
Preview the documentation template
A template predefines sections for your model documentation and provides a general outline to follow, making the documentation process much easier.
You will upload documentation and test results into this template later on. For now, take a look at the structure that the template provides with the vm.preview_template()
function from the ValidMind library and note the empty sections. You will see two blocks with different result IDs for skewness.
vm.preview_template()
Load the sample dataset
The sample dataset used here is provided by the ValidMind library, along with a second, different dataset (taiwan_credit
) you can try as well.
To be able to use either sample dataset, you need to import the dataset and load it into a pandas DataFrame, a two-dimensional tabular data structure that makes use of rows and columns:
# Import the sample dataset from the library
from validmind.datasets.classification import customer_churn as demo_dataset
= demo_dataset.load_data() df
Initialize a ValidMind dataset object
Before you can run a test suite, which are a collection of tests, you must first initialize a ValidMind dataset object using the init_dataset
function from the ValidMind (vm
) module.
This function takes a number of arguments:
dataset
— the raw dataset that you want to analyzetarget_column
— the name of the target column in the datasetclass_labels
— the list of class labels used for classification model training
= vm.init_dataset(
vm_dataset ="raw_dataset",
input_id=df,
dataset=demo_dataset.target_column,
target_column=demo_dataset.class_labels,
class_labels )
Document the model
As part of documenting the model with the ValidMind Library, you need to preprocess the raw dataset, initialize some training and test datasets, initialize a model object you can use for testing, and then run the full suite of tests.
Prepare datasets
DataFrame (df) preprocessing is simplified by employing demo_dataset.preprocess
to partition it into distinct datasets (train_df
, validation_df
, and test_df
)
= demo_dataset.preprocess(df) train_df, validation_df, test_df
Initialize the training and test datasets
With the datasets ready, you can now initialize the training and test datasets (train_df
and test_df
) created earlier into their own dataset objects using vm.init_dataset()
:
= vm.init_dataset(
vm_train_ds ="train_dataset", dataset=train_df, target_column=demo_dataset.target_column
input_id
)
= vm.init_dataset(
vm_test_ds ="test_dataset", dataset=test_df, target_column=demo_dataset.target_column
input_id )
Run documentation tests
Now specify inputs
and params
for individual tests using config
parameter. The results for the both the datasets will be visible in the documentation. The inputs
in the config get priority over global inputs
in the run_documentation_tests
.
= {
config "validmind.data_validation.Skewness:training_data": {
"params": {"max_threshold": 1},
"inputs": {"dataset": vm_train_ds},
},"validmind.data_validation.Skewness:test_data": {
"params": {"max_threshold": 1.5},
"inputs": {"dataset": vm_test_ds},
},
}
= vm.run_documentation_tests(
tests_suite ={
inputs"dataset": vm_dataset,
},=config,
config=["data_preparation"],
section )
Run the individual tests using the run_test
Now run the Skewness
tests for training and test datasets. The results for the both the datasets will be visible in the documentation.
= vm.tests.run_test(
test ="validmind.data_validation.Skewness:training_data",
test_id={"max_threshold": 1},
params={"dataset": vm_train_ds},
inputs
)
test.log()
= vm.tests.run_test(
test ="validmind.data_validation.Skewness:test_data",
test_id={"max_threshold": 1.5},
params={
inputs"dataset": vm_test_ds,
},
) test.log()
Next steps
You can look at the results of this test suite right in the notebook where you ran the code, as you would expect. But there is a better way — use the ValidMind platform to work with your model documentation.
Work with your model documentation
From the Model Inventory in the ValidMind Platform UI, go to the model you registered earlier. (Need more help?)
Click and expand the Model Development section.
Expand the 2. Data Preparation section and take a look around.
You can now see the skewness tests results of training and test datasets in the
Data Preparation
section.
From here, you can also make qualitative edits to model documentation, view guidelines, collaborate with validators, and submit your model documentation for approval when it’s ready. Learn more …
Discover more learning resources
We offer many interactive notebooks to help you document models:
Or, visit our documentation to learn more about ValidMind.
Upgrade ValidMind
Retrieve the information for the currently installed version of ValidMind:
%pip show validmind
If the version returned is lower than the version indicated in our production open-source code, restart your notebook and run:
%pip install --upgrade validmind
You may need to restart your kernel after running the upgrade package for changes to be applied.