Skip to content

Instantly share code, notes, and snippets.

@juliensimon
Last active August 29, 2015 14:19
Show Gist options
  • Save juliensimon/93b58f19031796f39749 to your computer and use it in GitHub Desktop.
Save juliensimon/93b58f19031796f39749 to your computer and use it in GitHub Desktop.
aws ml java sample
package org.julien.datastuff;
import com.amazonaws.AmazonClientException;
import com.amazonaws.services.machinelearning.AmazonMachineLearningClient;
import com.amazonaws.services.machinelearning.model.DescribeMLModelsResult;
import com.amazonaws.services.machinelearning.model.MLModel;
import com.amazonaws.services.machinelearning.model.PredictRequest;
import com.amazonaws.services.machinelearning.model.PredictResult;
import com.amazonaws.services.machinelearning.model.RealtimeEndpointInfo;
public class MLSample {
public static void main(String[] args) {
// Create a machine learning client
AmazonMachineLearningClient client = new AmazonMachineLearningClient();
// Get list of prediction models
DescribeMLModelsResult models = client.describeMLModels();
// Iterate over all models and show basic information about each one
for (MLModel m : models.getResults()) {
System.out.println("Model name: " + m.getName());
System.out.println("Model id: " + m.getMLModelId());
System.out.println("Model status: " + m.getStatus());
RealtimeEndpointInfo endpoint = m.getEndpointInfo();
System.out.println("Endpoint URL: " + endpoint.getEndpointUrl());
System.out.println("Endpoint status: "
+ endpoint.getEndpointStatus());
}
// Select first model
MLModel model = models.getResults().get(0);
// Build a prediction request
PredictRequest request = new PredictRequest();
// Select prediction model
request.setMLModelId(model.getMLModelId());
// Select realtime endpoint
request.setPredictEndpoint(model.getEndpointInfo().getEndpointUrl());
// Build data to be predicted
request.addRecordEntry("lastname", "Simon")
.addRecordEntry("firstname", "Julien")
.addRecordEntry("age", "44").addRecordEntry("gender", "M")
.addRecordEntry("state", "Texas").addRecordEntry("month", "4")
.addRecordEntry("day", "106").addRecordEntry("hour", "10")
.addRecordEntry("minutes", "26").addRecordEntry("items", "5");
System.out.println("Sending prediction request for: "
+ request.getRecord());
// Send prediction request
PredictResult result;
try {
long start = System.currentTimeMillis();
result = client.predict(request);
long end = System.currentTimeMillis();
System.out.println((end - start) + " ms");
} catch (Exception e) {
throw new AmazonClientException("Prediction failed", e);
}
// Display predicted value
System.out.println("Predicted value:"
+ result.getPrediction().getPredictedValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment