|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START aiplatform_explain_tabular_sample] |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +from google.cloud import aiplatform |
| 19 | +from google.protobuf import json_format |
| 20 | +from google.protobuf.struct_pb2 import Value |
| 21 | + |
| 22 | + |
| 23 | +def explain_tabular_sample( |
| 24 | + project: str, |
| 25 | + endpoint_id: str, |
| 26 | + instance_dict: Dict, |
| 27 | + location: str = "us-central1", |
| 28 | + api_endpoint: str = "us-central1-prediction-aiplatform.googleapis.com", |
| 29 | +): |
| 30 | + client_options = {"api_endpoint": api_endpoint} |
| 31 | + # Initialize client that will be used to create and send requests. |
| 32 | + # This client only needs to be created once, and can be reused for multiple requests. |
| 33 | + client = aiplatform.gapic.PredictionServiceClient(client_options=client_options) |
| 34 | + # The format of each instance should conform to the deployed model's prediction input schema. |
| 35 | + instance = json_format.ParseDict(instance_dict, Value()) |
| 36 | + instances = [instance] |
| 37 | + # tabular models do not have additional parameters |
| 38 | + parameters_dict = {} |
| 39 | + parameters = json_format.ParseDict(parameters_dict, Value()) |
| 40 | + endpoint = client.endpoint_path( |
| 41 | + project=project, location=location, endpoint=endpoint_id |
| 42 | + ) |
| 43 | + response = client.explain( |
| 44 | + endpoint=endpoint, instances=instances, parameters=parameters |
| 45 | + ) |
| 46 | + print("response") |
| 47 | + print(" deployed_model_id:", response.deployed_model_id) |
| 48 | + explanations = response.explanations |
| 49 | + for explanation in explanations: |
| 50 | + print(" explanation") |
| 51 | + # Feature attributions. |
| 52 | + attributions = explanation.attributions |
| 53 | + for attribution in attributions: |
| 54 | + print(" attribution") |
| 55 | + print(" baseline_output_value:", attribution.baseline_output_value) |
| 56 | + print(" instance_output_value:", attribution.instance_output_value) |
| 57 | + print(" output_display_name:", attribution.output_display_name) |
| 58 | + print(" approximation_error:", attribution.approximation_error) |
| 59 | + print(" output_name:", attribution.output_name) |
| 60 | + output_index = attribution.output_index |
| 61 | + for output_index in output_index: |
| 62 | + print(" output_index:", output_index) |
| 63 | + predictions = response.predictions |
| 64 | + for prediction in predictions: |
| 65 | + print(" prediction:", dict(prediction)) |
| 66 | + |
| 67 | + |
| 68 | +# [END aiplatform_explain_tabular_sample] |