client.get_explanation

Get explanation for a single observation.

Input ParameterTypeDefaultDescription
project_idstrNoneA unique identifier for the project.
model_idstrNoneA unique identifier for the model.
input_data_sourceUnion[fdl.RowDataSource, fdl.EventIdDataSource]NoneType of data source for the input dataset to compute explanation on (RowDataSource, EventIdDataSource). A single row explanation is currently supported.
ref_data_sourceOptional[Union[fdl.DatasetDataSource, fdl.SqlSliceQueryDataSource] ]NoneType of data source for the reference data to compute explanation on (DatasetDataSource, SqlSliceQueryDataSource).
Only used for non-text models and the following methods:
'SHAP', 'FIDDLER_SHAP', 'PERMUTE', 'MEAN_RESET'
explanation_typeOptional[str]'FIDDLER_SHAP'Explanation method name. Could be your custom
explanation method or one of the following method:
'SHAP', 'FIDDLER_SHAP', 'IG', 'PERMUTE', 'MEAN_RESET', 'ZERO_RESET'
num_permutationsOptional[int]300- For Fiddler SHAP, num_permutations corresponds to the number of coalitions to sample to estimate the Shapley values of each single-reference game.
- For the permutation algorithms, num_permutations corresponds to the number of permutations from the dataset to use for the computation.
ci_levelOptional[float]0.95The confidence level (between 0 and 1).
top_n_classOptional[int]NoneFor multi-class classification models only, specifying if only the n top classes are computed or all classes (when parameter is None).
PROJECT_ID = 'example_project'
MODEL_ID = 'example_model'
DATASET_ID = 'example_dataset

# FIDDLER SHAP - Dataset reference data source
row = df.to_dict(orient='records')[0]
client.get_explanation(
    project_id=PROJECT_ID,
    model_id=MODEL_ID,
    input_data_source=fdl.RowDataSource(row=row),
    ref_data_source=fdl.DatasetDataSource(dataset_id=DATASET_ID, num_samples=300),
    explanation_type='FIDDLER_SHAP',
    num_permutations=200,
    ci_level=0.95,
)

# FIDDLER SHAP - Slice ref data source
row = df.to_dict(orient='records')[0]
query = f'SELECT * from {DATASET_ID}.{MODEL_ID} WHERE sulphates >= 0.8'
client.get_explanation(
    project_id=PROJECT_ID,
    model_id=MODEL_ID,
    input_data_source=fdl.RowDataSource(row=row),
    ref_data_source=fdl.SqlSliceQueryDataSource(query=query, num_samples=100),
    explanation_type='FIDDLER_SHAP',
    num_permutations=200,
    ci_level=0.95,
)

# FIDDLER SHAP - Multi-class classification (top classes)
row = df.to_dict(orient='records')[0]
client.get_explanation(
    project_id=PROJECT_ID,
    model_id=MODEL_ID,
    input_data_source=fdl.RowDataSource(row=row),
    ref_data_source=fdl.DatasetDataSource(dataset_id=DATASET_ID),
    explanation_type='FIDDLER_SHAP',
    top_n_class=2
)

# IG (Not available by default, need to be enabled via package.py)
row = df.to_dict(orient='records')[0]
client.get_explanation(
    project_id=PROJECT_ID,
    model_id=MODEL_ID,
    input_data_source=fdl.RowDataSource(row=row),
    explanation_type='IG',
)
Return TypeDescription
tupleA named tuple with the explanation results.