Feature importance (APIs)

To help with model monitoring and explainability and aid in root cause analysis, add feature importance generated during training to your models. Then, create feature drift policies that use the feature importance results to get better insight into your models. View the SHAP chart in the Model Dashboard page to visualize results of feature importance analysis.

You can add feature importance values when. The V1ModelsModel tags property supports an array of model tags associated with the model. In addition to model tags for creating the model (such as model_class, model_name, uuid, target column, allcolumns, etc.), V1ModelsModel provides the feature_importance model tag for adding feature importance data to the model.

Configuring feature_importance model tag

For each feature that contributes to feature importance for your model, specify the following information: feature name (string, required), absolute importance (float, optional), percent contribution (float, optional).

"tags":[
   {
      "model_uuid":null,
      "name":"feature_importance",
      "value":[
         { "Feature": "feature1", "absolute_importance": 12.0 },
         { "Feature": "feature4", "absolute_importance": 27.0 },
         { "Feature": "feature7", "absolute_importance": 18.0 },
         { "Feature": "feature10", "absolute_importance": 34.0 }
      ],
      "uuid":null,
      "status":"active",
      "created_ts":null,
      "modified_ts":null,
      "created_by":null,
      "modified_by":null
   },
]

You do not need to include information for features that have no bearing on feature importance. If you do not supply values for absolute importance and/or percent contribution, the endpoint calculates and applies the missing values. Generally, you specify absolute importance and let the endpoint calculate the percent contribution, as illustrated in the code snippet.

Note: If you call the model create endpoint with feature importance data defined (`feature_importance`model tag) but no columns (`all columns`model tag), the model is created but the`feature_importance` model tag is treated as a custom tag and not related to feature importance weighting.

Getting feature importance values from file

Often, feature importance data exists in offline files. You can upload feature importance data to your models from csv, parquet, or json format files.

Using REST API to get feature importance from files

Use v1/model-tags/file/upload for the initial file upload. Specify the path to the file, the name of the model tag ( i.e., feature_importance), the UUID for your model, and actual name of the file containing the feature importance values.

Example file:

{
  "images": [
    {
      "image": [
        "https://files.readme.io/0227a93-Screenshot_2023-07-24_at_9.20.25_PM.png",
        null,
        ""
      ],
      "align": "center"
    }
  ]
}

Another example file:

[
    {"Feature":"feature1", "absolute_importance":12}, 
    {"Feature":"feature4", "absolute_importance":27},
    {"Feature":"feature7", "absolute_importance":18},
    {"Feature":"feature12", "absolute_importance":34}
]

If you need to update the feature importance data for a model after uploading, use v1/model-tags/update/file/upload to add changes. Specify the model tag uuid and model uuid.

Using Python SDK to get feature importance from files

(Note: These snippets assume the associated model is already created.)

Import model tags SDK support.

from vianops_client import (
    ModelsTagsV1Api,
    V1BaseModelTagsModel,
    V1BaseModelTagsModelList,
)

Specify variables, such as:

model_tags_api = ModelsTagsV1Api()
load_model_api = ModelsV1Api()

Identify the file name/path containing the feature importance data.

file_path = r"feat_imp_data.csv"

Upload the feature importance data for a specified model, for example:


create_res = model_tags_api.upload(model_uuid, "feature_importance_modelxyz", file_path)
print(create_res)
assert create_res[0]["name"] == "feature_importance_modelxyz"

Error checking for non-empty uuid is suggested:
assert create_res[0]["uuid"] is not None

TABLE OF CONTENTS