Search in sources :

Example 1 with TabularClassificationPredictionResult

use of com.google.cloud.aiplatform.v1.schema.predict.prediction.TabularClassificationPredictionResult in project java-aiplatform by googleapis.

the class PredictTabularClassificationSample method predictTabularClassification.

static void predictTabularClassification(String instance, String project, String endpointId) throws IOException {
    PredictionServiceSettings predictionServiceSettings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(predictionServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        ListValue.Builder listValue = ListValue.newBuilder();
        JsonFormat.parser().merge(instance, listValue);
        List<Value> instanceList = listValue.getValuesList();
        Value parameters = Value.newBuilder().setListValue(listValue).build();
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instanceList, parameters);
        System.out.println("Predict Tabular Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            TabularClassificationPredictionResult.Builder resultBuilder = TabularClassificationPredictionResult.newBuilder();
            TabularClassificationPredictionResult result = (TabularClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getClassesCount(); i++) {
                System.out.printf("\tClass: %s", result.getClasses(i));
                System.out.printf("\tScore: %f", result.getScores(i));
            }
        }
    }
}
Also used : PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ListValue(com.google.protobuf.ListValue) ListValue(com.google.protobuf.ListValue) Value(com.google.protobuf.Value) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) TabularClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.TabularClassificationPredictionResult) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient)

Aggregations

EndpointName (com.google.cloud.aiplatform.v1.EndpointName)1 PredictResponse (com.google.cloud.aiplatform.v1.PredictResponse)1 PredictionServiceClient (com.google.cloud.aiplatform.v1.PredictionServiceClient)1 PredictionServiceSettings (com.google.cloud.aiplatform.v1.PredictionServiceSettings)1 TabularClassificationPredictionResult (com.google.cloud.aiplatform.v1.schema.predict.prediction.TabularClassificationPredictionResult)1 ListValue (com.google.protobuf.ListValue)1 Value (com.google.protobuf.Value)1