求基于模型名与计算资源获取Azure AutoML模型解释的Python代码
Fetch Model Explanations from Azure AutoML by Model Name & Compute Target
First off, make sure you've got the required Azure ML SDK packages installed (if you haven't already):
pip install azureml-sdk azureml-automl-core azureml-explain-model
Below is a reusable Python function that takes your target model name and compute resource as inputs, tracks down the corresponding AutoML run, and pulls both global and summary explanations for the model:
from azureml.core import Workspace, Experiment from azureml.explain.model._internal.explanation_client import ExplanationClient def get_automl_model_explanations(ws_name: str, resource_group: str, subscription_id: str, experiment_name: str, model_name: str, compute_target_name: str): # Connect to your Azure ML workspace ws = Workspace(subscription_id=subscription_id, resource_group=resource_group, workspace_name=ws_name) # Grab the AutoML experiment you ran experiment = Experiment(ws, experiment_name) # Filter for completed AutoML runs (skip failed/canceled ones) completed_runs = list(experiment.get_runs(type='automl', status='Completed')) # Find the run that matches your model and compute target target_run = None for run in completed_runs: run_details = run.get_details() # First check if the run used the specified compute target if run_details['target'] == compute_target_name: # Pull all models generated by this run run_models = run.get_outputs() for model_output in run_models: # Match the model name if model_output[0].name == model_name: target_run = run break if target_run: break if not target_run: raise ValueError(f"Couldn't find a completed AutoML run for model '{model_name}' on compute '{compute_target_name}'") # Initialize the explanation client to fetch interpretability data explanation_client = ExplanationClient.from_run(target_run) # Fetch the full global explanation global_explanation = explanation_client.get_global_explanation() # Get the summary explanation (includes aggregated feature importance) summary_explanation = global_explanation.get_summary_explanation() # Return a structured result with all key info return { "run_id": target_run.id, "model_name": model_name, "global_explanation": global_explanation, "summary_explanation": summary_explanation, "feature_importance": summary_explanation.feature_importance } # Example usage - fill in your own workspace/experiment details if __name__ == "__main__": # Your Azure ML workspace credentials subscription_id = "YOUR_SUBSCRIPTION_ID" resource_group = "YOUR_RESOURCE_GROUP" ws_name = "YOUR_WORKSPACE_NAME" experiment_name = "YOUR_AUTOML_EXPERIMENT_NAME" # Target model and compute details target_model_name = "YOUR_TRAINED_MODEL_NAME" target_compute_name = "YOUR_COMPUTE_TARGET_NAME" try: explanations = get_automl_model_explanations(ws_name, resource_group, subscription_id, experiment_name, target_model_name, target_compute_name) # Quick check: print feature importance scores print(f"Feature Importance for Model '{target_model_name}':") for feature, score in explanations['feature_importance'].items(): print(f"- {feature}: {round(score, 4)}") # Optional: launch an interactive visualization dashboard # explanations['global_explanation'].visualize() except ValueError as e: print(e)
Quick Tips:
- Simpler Authentication: If you have a
config.jsonfile from your Azure ML workspace in your working directory, replace the workspace connection code withws = Workspace.from_config()to skip manual credential entry. - Handle Duplicate Runs: If you have multiple runs with the same model name/compute pair, add a filter for run timestamp (e.g.,
run.created_time) to pick the most recent one. - Visualization: Uncomment the
visualize()line to open an interactive browser dashboard that lets you explore feature impacts, prediction breakdowns, and more.
内容的提问来源于stack exchange,提问作者datacore
相关产品推荐
相关产品推荐

