使用PHP在Google Prediction API中训练数据时出错

时间:2012-02-29 12:34:02

标签: php google-prediction

我在PHP中使用Google Prediction API。

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

  

致命错误:未捕获的异常'apiException',消息'未知   功能: - > - >插入()'   C:\ XAMPP \ htdocs中\谷歌-API-PHP-客户\ SRC \服务\ apiServiceResource.php:81   堆栈跟踪:#0   C:\ XAMPP \ htdocs中\谷歌-API-PHP-客户\ SRC \的contrib \ apiPredictionService.php(60):   apiServiceResource-> __ call('insert',Array)#1   C:\ XAMPP \ htdocs中\ Google处理API-PHP-客户\例子\分析\ new2.php(51):   TrainedmodelsServiceResource->插入(对象(apiPredictionService),   数组)#2 {main}引入   C:\ XAMPP \ htdocs中\谷歌的API的PHP客户端的\ src \服务\ 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());
}

我无法继续下去。我计划训练然后调用预测功能。

1 个答案:

答案 0 :(得分: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 ) ) )