0

我在 PHP 中使用 Google 预测 API。

使用 OAuth 2.0 成功完成授权。我将数据保存在云端的 csv 文件中。我在 Training 类中使用 setDataLocation 方法给出了它的位置。但是在训练/插入数据时出现以下错误:

致命错误:在 C:\xampp\htdocs\google-api-php-client\src\service\apiServiceResource.php:81 中带有消息“未知函数:->->insert()”的未捕获异常 'apiException' 堆栈跟踪: #0 C:\xampp\htdocs\google-api-php-client\src\contrib\apiPredictionService.php(60): apiServiceResource->__call('insert', Array) #1 C:\xampp\htdocs\google- api-php-client\examples\analytics\new2.php(51): TrainedmodelsServiceResource->insert(Object(apiPredictionService), Array) #2 {main} 在 C:\xampp\htdocs\google-api-php-client 中抛出\src\service\apiServiceResource.php 在第 81 行

这是我的代码片段:

if ($client->getAccessToken()) {
    $data = array();
    $buzzy = new Training();
    $predictionService = new apiPredictionService($client);
    $trainedmodels = $predictionService->trainedmodels;
    $buzzzy = new TrainedmodelsServiceResource();
    $me = $buzzy->setStorageDataLocation('my_data.csv');
    $mee = $buzzy->getStorageDataLocation();
    // $ma = $buzzy->getTrainingStatus();
    $setid_in = $buzzy->setId($buzzy->getStorageDataLocation());
    $setid_out = $buzzy->getId();
    echo $setid_out;
    //print_r($predictionService);
    //$insert_1 = $buzzzy->insert($buzzy,array());

    // This is line 81 in my code:
    $insert2=$trainedmodels->insert($predictionService,array());
}

我无法继续进行。我计划训练然后调用预测函数。

4

1 回答 1

1

我刚刚编写了一个测试程序来使用 PHP 进行预测,并且能够使其正常工作。这是魔术序列:

   $id = "your-model-id-goes-here";
   $predictionText = "This is a test";
   $predictionData = new InputInput();
   $predictionData->setCsvInstance(array($predictionText));
   // My model takes a single feature but if your model needs more than one 
   // feature, simply include more values in the csvInstance array, like this...
   // $predictionData->setCsvInstance(array($data1, $data2, ..., $dataN));
   $input = new Input();
   $input->setInput($predictionData);
   print_r($predictionService->trainedmodels->predict($id, $input));

这将显示来自预测请求的未格式化 JSON 响应,如下所示:

Array ( [kind] => prediction#output [id] => languages [selfLink] =>    
https://www.googleapis.com/prediction/v1.4/trainedmodels/languages/predict 
[outputLabel] => French [outputMulti] => Array ( [0] => Array ( [label] => 
English [score] => 0.333297 ) [1] => Array ( [label] => French [score] => 
0.339412 ) [2] => Array ( [label] => Spanish [score] => 0.327291 ) ) )
于 2012-03-01T04:43:52.720 回答