2

如果我以某种方式将 API Explorer 界面用于 Google 预测 API,我已经正确且成功地设置了训练数据,并且可以运行具有预期结果的预测。

我还可以根据 google 在 php 中给出的示例从 localhost 运行单个特征预测。

我的训练数据有 51 个我想要运行预测的特征。该模型是可靠的,并且返回了 92% 的准确率。我对基于 25000 个实例的训练模型没有任何问题。

在一个有点相关的问题中,Marc Cohen 在 php 中给出了以下示例来运行预测,该预测非常适用于语言演示文件或任何单个特征预测。

//------------------

我刚刚编写了一个测试程序来使用 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 ) ) )

//--------------------

他对多特征所做的说明,即://我的模型采用单个特征,但如果您的模型需要多个 // 特征,只需在 csvInstance 数组中包含更多值,如下所示... // $predictionData-> setCsvInstance(array($data1, $data2, ..., $dataN));

对我来说意味着只需将 $predictionText 变量作为“Feature_1”、“Feature_2”、“Feature_3”、.....“Feature_N”传递就可以了。

我使用的数据主要是数字。例如:69,13,10,9,101,69,94,96,96,96......9 我已经尝试过加引号和不加引号,但始终得到相同的预测。

如果我使用 API 资源管理器并在其中输入一个新的数组元素来预测所有数据,即:

"input": {
"csvInstance": [

"84",

"63",

"30",

"30",

...........

它会预测正确的答案。

如果我使用资源管理器并按照 Marcs 示例输入数据。即:"84","63","30","30","207","83","87","94","94","94","94","94","94","94","38","57","143","144","164","164","164","164","164"......... 相同的数据将给出完全不同的结果,而第二种方法总是返回相同的结果。

显然我在这里做错了什么。我已经尝试了所有 php json 编码选项以及我能想到的任何其他方法来正确格式化它以在我的 php 脚本或 API 资源管理器中工作,但无济于事。

谁能告诉我如何$predictionText正确格式化。

我的代码如下。(我试过带和不带引号和纯数字)

    $predictionText = '84,63,30,30,207,83,87,94,94,94,94,94,94,94,38,57,143,144,164,164,164,164,164,"New Moon",115,221,31,62,-14,-106,-43,-4,43,-174,-224,25,93,142,78,87,29,-65,44,33,34,19,16,14,13,12,11';

$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array($predictionText) ); 
$input = new Google_Input();    
$input->setInput($predictionData);   
$result = $predictionService->trainedmodels->predict($id, $input);
print("</div><br><br><h2>Prediction Result:</h2>");
print_r($result);

谢谢你。

4

1 回答 1

2

解决了。

训练要求字符串用引号引起来。即“新月”。预测不需要引号。我已将预测字符串更改为在我拥有的单个字符串功能周围没有引号,并且一切正常。

于 2013-01-13T15:09:50.223 回答