0

我想使用谷歌预测API。我已经训练了我的模型,并通过网页测试了一个预测,它效果很好。不过,我现在正在试图使用Java API来预测了一系列的记录,但我一直收到错误“无效的值:无法解析”谷歌预测API请求

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request 
{ 
    "code" : 400, 
    "errors" : [ { 
    "domain" : "global", 
    "message" : "Invalid value for: Unable to parse '[feature1, feature2, feature3, feature4, feature5]'.", 
    "reason" : "invalid" 
    } ], 
    "message" : "Invalid value for: Unable to parse '[feature1, feature2, feature3, feature4, feature5]'." 

对我来说,这似乎是JSON的创造者并没有给周围的特征报价,但我下面尽可能接近样品,他们不会更改或修改json工厂。这是证书和预测建筑代码。

private static GoogleCredential authorize() throws Exception { 

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport) 
      .setJsonFactory(JSON_FACTORY) 
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL) 
      .setServiceAccountScopes(Collections.singleton(PredictionScopes.PREDICTION)) 
      .setServiceAccountPrivateKeyFromP12File(new File("p12filefromdevconsole.p12")) 
      .build(); 
    return credential; 

} 

... 
Prediction prediction = new Prediction.Builder(
      httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build(); 

... 
private static Output predict(Prediction prediction, String... features) throws IOException { 
    Input input = new Input(); 
    InputInput inputInput = new InputInput(); 
    inputInput.setCsvInstance(Collections.<Object>singletonList(features)); 
    input.setInput(inputInput); 
    Output output = prediction.trainedmodels().predict(PROJECT_ID, MODEL_ID, input).execute(); 
    return output; 
} 

有什么想法我做错了?

回答

0

太多的无奈和反复试验后,我通过使用新的ArrayList(Arrays.asList(特征)),而不是使用Collections.singletonList(功能)解决了这个问题。这是修改后的预测方法。请记住我的最初的实现直接从谷歌网站上的样品:(

private static Output predict(Prediction prediction, String... features) throws IOException { 
    Input input = new Input(); 
    InputInput inputInput = new InputInput(); 
    inputInput.setCsvInstance(new ArrayList(Arrays.asList(features))); 
    input.setInput(inputInput); 
    Output output = prediction.trainedmodels().predict(PROJECT_ID, MODEL_ID, input).execute(); 
    return output; 
} 
+0

其中预测API的版本,确实存在InputInput类?我无法找到任何版本的类来了。请帮忙..!! – 2015-03-24 07:09:43