2012-08-15 55 views
0

我的名字是titiri和快樂,我發現胡扯庫分類。我認爲華夫餅是機器學習算法的一個很好的庫。 我有一個關於華夫餅圖書館的問題。如何預測一個類實例的在華夫餅C++ API

訓練模型後,我想打印的預測,對於一個實例:

我的代碼是:

GMatrix Instance(1,8);//instance have 8 real attribute and 
double out;// value in attribute 'class' is nomial 
Instance[0][0]=6; 
Instance[0][1]=148; 
Instance[0][2]=72; 
Instance[0][3]=35; 
Instance[0][4]=0; 
Instance[0][5]=33.6; 
Instance[0][6]=0.62; 
Instance[0][7]=50; 
modell->predict(Instance[0],&out); 
cout<<&out; 

這個代碼不工作真實,不打印任何東西。 請幫幫我! 我需要做什麼來預測一個實例的類,然後打印其類, 都有不錯的表現「預測」歸類爲一個實例方法? 還是有更好的方法來完成這項工作嗎?

感謝, 要快樂,贏得

+0

聲明'COUT << &out;'輸出變量'out'的_address_。 – 2012-08-15 05:51:14

+0

cout <<出也不行! – titiri 2012-08-15 06:36:18

+0

'cout << out << endl;'? – 2012-08-15 06:37:42

回答

2

我懷疑你的代碼不會打印出任何東西是因爲你忘了endl的原因。 (這是在他的評論中提到Joachim Pileborg

如果您正在使用Visual Studio,你可能想在你的代碼(也許在return語句)的末尾添加一個斷點,因爲在某些模式下它可以關閉應用程序才能看到輸出,這可以使其看起來好像什麼也沒有發生。

示例

下面是一個完整的示例,對我來說工作正常。它包含你的實例。它從2blobs_knn.json加載一個K最近的鄰居學習者,然後評估你的實例。您可以使用由華夫餅工具生成的任何經過培訓的受監督模型的名稱來替換該文件名。

對於I中使用的,程序打印「1」和退出模型。

如果您想使用我測試代碼的確切模型(以防您的學習者構建方法出現問題)請參閱示例代碼之後的部分。

#include <GClasses/GMatrix.h> 
#include <GClasses/GHolders.h> 
#include <GClasses/GRand.h> 
#include <GClasses/GLearner.h> 
#include <GClasses/GDom.h> 
#include <iostream> 
#include <cassert> 

using namespace GClasses; 
using std::cout; using std::endl; 

int main(int argc, char *argv[]) 
{ 
    //Load my trained learner from a file named 2blobs_knn.json and put 
    //it in hModel which is a shared-pointer class. 
    GLearnerLoader ll(GRand::global()); 
    GDom dom; 
    dom.loadJson("2blobs_knn.json"); 
    Holder<GSupervisedLearner> hModel(ll.loadSupervisedLearner(dom.root())); 
    assert(hModel.get() != NULL); 


    //Here is your code 
    GMatrix Instance(1,8);// Instance has 8 real attributes and one row 
    double out;   // The value in attribute 'class' is nominal 
    Instance[0][0]=6; 
    Instance[0][1]=148; 
    Instance[0][2]=72; 
    Instance[0][3]=35; 
    Instance[0][4]=0; 
    Instance[0][5]=33.6; 
    Instance[0][6]=0.62; 
    Instance[0][7]=50; 

    hModel.get()->predict(Instance[0],&out); 
    cout << out << endl; 
    return 0; 
} 

如何我在示例中使用的學習者構建

爲了獲得學習者,我使用Matlab的(Octave是自由模仿者)生成CSV文件,其中0級是8維以(0,0,0,0,0,0,0,0)和1級爲中心的球形單元高斯具有相同的分佈,但集中在(2,2,2,2,2,2,2,2)

m=[[randn(200,8);randn(200,8)+2], [repmat(0,200,1);repmat(1,200,1)]]; 
csvwrite('2blobs.csv',m) 

然後,我把那CSV,使用

它轉化爲ARFF
waffles_transform import 2blobs.csv > 2blobs.arff 

接下來,我在文本編輯器中將最後一個屬性從@ATTRIBUTE attr8 real更改爲 @ATTRIBUTE class {0,1},所以它是名義上的。

最後,我訓練的模型

waffles_learn train 2blobs.arff knn -neighbors 10 > 2blobs_knn.json 
+0

不,你不想在編譯器/ IDE中添加'system(「Pause」)'到你的代碼中。 Visual Studio會將stdout路由到它的輸出窗口,該窗口超過了應用程序的末尾,這完全沒有必要。 – ssube 2012-08-15 17:44:58

+0

它取決於Visual Studio的版本。在2008年(我上次使用VS)時,如果在調試模式(F5)而不是釋放模式(CTRL-F5)下執行,它將用於發送輸出到終端並關閉終端。例如,請參閱[Microsoft Q&A](http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/636a5fe5-06e8-4eaa-b88b-3d16921f161d)。 (順便說一下,MS Q&A看起來像StackOverflow一樣可疑) – Eponymous 2012-08-15 17:51:08

+1

即使您處於連接調試模式,「main」末尾的斷點仍然更簡單(不必稍後移除),並且工作正常。 – ssube 2012-08-15 17:53:38