2013-01-11 50 views
2

如果我以某種方式使用API​​預覽API接口,我已經正確成功地設置了訓練數據,並且可以運行預期的預期結果。google預測API中多特徵csvinstance數組的正確格式

我也可以從本地主機運行一個單一的功能預測基於在谷歌提供的例子在PHP中。

我的訓練數據有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陣列中更多的價值,像這樣... // $ predictData-> setCsvInst ance(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​​ Explorer並進入一個新的數組元素到它的所有數據預測,即:

"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); 

謝謝。

回答

2

已解決。

培訓要求字符串用引號括起來。即「新月」。 預測不需要引號。 我已經改變了預測字符串,沒有引號圍繞單個字符串的功能,我有和所有工作。