Search in sources :

Example 1 with TablesAnnotation

use of com.google.cloud.automl.v1beta1.TablesAnnotation in project java-automl by googleapis.

the class TablesPredict method predict.

static void predict(String projectId, String modelId, List<Value> values) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, "us-central1", modelId);
        Row row = Row.newBuilder().addAllValues(values).build();
        ExamplePayload payload = ExamplePayload.newBuilder().setRow(row).build();
        // Feature importance gives you visibility into how the features in a specific prediction
        // request informed the resulting prediction. For more info, see:
        // https://cloud.google.com/automl-tables/docs/features#local
        PredictRequest request = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("feature_importance", "true").build();
        PredictResponse response = client.predict(request);
        System.out.println("Prediction results:");
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            TablesAnnotation tablesAnnotation = annotationPayload.getTables();
            System.out.format("Classification label: %s%n", tablesAnnotation.getValue().getStringValue());
            System.out.format("Classification score: %.3f%n", tablesAnnotation.getScore());
            // Get features of top importance
            tablesAnnotation.getTablesModelColumnInfoList().forEach(info -> System.out.format("\tColumn: %s - Importance: %.2f%n", info.getColumnDisplayName(), info.getFeatureImportance()));
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1beta1.ModelName) TablesAnnotation(com.google.cloud.automl.v1beta1.TablesAnnotation) PredictResponse(com.google.cloud.automl.v1beta1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1beta1.ExamplePayload) Row(com.google.cloud.automl.v1beta1.Row) PredictRequest(com.google.cloud.automl.v1beta1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1beta1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1beta1.AnnotationPayload)

Aggregations

AnnotationPayload (com.google.cloud.automl.v1beta1.AnnotationPayload)1 ExamplePayload (com.google.cloud.automl.v1beta1.ExamplePayload)1 ModelName (com.google.cloud.automl.v1beta1.ModelName)1 PredictRequest (com.google.cloud.automl.v1beta1.PredictRequest)1 PredictResponse (com.google.cloud.automl.v1beta1.PredictResponse)1 PredictionServiceClient (com.google.cloud.automl.v1beta1.PredictionServiceClient)1 Row (com.google.cloud.automl.v1beta1.Row)1 TablesAnnotation (com.google.cloud.automl.v1beta1.TablesAnnotation)1