我懷疑你的代碼不會打印出任何東西是因爲你忘了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
聲明'COUT << &out;'輸出變量'out'的_address_。 – 2012-08-15 05:51:14
cout <<出也不行! – titiri 2012-08-15 06:36:18
'cout << out << endl;'? – 2012-08-15 06:37:42