"Invalid value for: Unable to parse" 谷歌预测 API 请求



我正在尝试使用Google Prediction API。我已经训练了我的模型,并通过网页测试了一个预测,它非常有效。然而,我现在正试图使用javaapi来预测一堆记录,但我一直收到一个错误

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;
}

有没有想过我做错了什么?

在经历了多次挫折和尝试之后,我通过使用新的ArrayList(Arrays.asList(features))而不使用Collections.singletonList(feature)

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;
}

相关内容

  • 没有找到相关文章

最新更新