Mostrar la evaluación de un modelo

Demuestra cómo mostrar una evaluación de modelo.

Muestra de código

Python

# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# model_display_name = 'MODEL_DISPLAY_NAME_HERE'
# filter = 'filter expression here'

from google.cloud import automl_v1beta1 as automl

client = automl.TablesClient(project=project_id, region=compute_region)

# List all the model evaluations in the model by applying filter.
response = client.list_model_evaluations(
    model_display_name=model_display_name, filter=filter
)

# Iterate through the results.
for evaluation in response:
    # There is evaluation for each class in a model and for overall model.
    # Get only the evaluation of overall model.
    if not evaluation.annotation_spec_id:
        model_evaluation_name = evaluation.name
        break

# Get a model evaluation.
model_evaluation = client.get_model_evaluation(
    model_evaluation_name=model_evaluation_name
)

classification_metrics = model_evaluation.classification_evaluation_metrics
if str(classification_metrics):
    confidence_metrics = classification_metrics.confidence_metrics_entry

    # Showing model score based on threshold of 0.5
    print("Model classification metrics (threshold at 0.5):")
    for confidence_metrics_entry in confidence_metrics:
        if confidence_metrics_entry.confidence_threshold == 0.5:
            print(
                "Model Precision: {}%".format(
                    round(confidence_metrics_entry.precision * 100, 2)
                )
            )
            print(
                "Model Recall: {}%".format(
                    round(confidence_metrics_entry.recall * 100, 2)
                )
            )
            print(
                "Model F1 score: {}%".format(
                    round(confidence_metrics_entry.f1_score * 100, 2)
                )
            )
    print("Model AUPRC: {}".format(classification_metrics.au_prc))
    print("Model AUROC: {}".format(classification_metrics.au_roc))
    print("Model log loss: {}".format(classification_metrics.log_loss))

regression_metrics = model_evaluation.regression_evaluation_metrics
if str(regression_metrics):
    print("Model regression metrics:")
    print(
        "Model RMSE: {}".format(regression_metrics.root_mean_squared_error)
    )
    print("Model MAE: {}".format(regression_metrics.mean_absolute_error))
    print(
        "Model MAPE: {}".format(
            regression_metrics.mean_absolute_percentage_error
        )
    )
    print("Model R^2: {}".format(regression_metrics.r_squared))

¿Qué sigue?

Para buscar y filtrar muestras de código para otros productos de Google Cloud, consulta el navegador de muestra de Google Cloud.