0

Google Prediction API を使用しようとしています。モデルをトレーニングし、Web ページで予測をテストしましたが、うまくいきました。ただし、Java 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;
}

私が間違っていることは何ですか?

4

1 に答える 1

0

多くの欲求不満と試行錯誤の後、 Collections.singletonList(features)を使用せずにnew ArrayList(Arrays.asList(features))を使用してこの問題を解決しました。これが変更された予測メソッドです。私の元の実装は、Google の Web サイトのサンプルから直接得たものであることを覚えておいてください :(

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;
}
于 2014-10-10T13:25:10.653 に答える