C++ 如何在华夫格c+中预测实例的类+;美国石油学会

C++ 如何在华夫格c+中预测实例的类+;美国石油学会,c++,machine-learning,classification,C++,Machine Learning,Classification,我的名字叫提提里,很高兴我找到了华夫格图书馆进行分类。我认为华夫格是一个很好的机器学习算法库。 我有一个关于华夫饼干图书馆的问题 训练模型后,我希望打印预测,例如: 我的代码是: 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

我的名字叫提提里,很高兴我找到了华夫格图书馆进行分类。我认为华夫格是一个很好的机器学习算法库。 我有一个关于华夫饼干图书馆的问题

训练模型后,我希望打印预测,例如:

我的代码是:

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;
GMatrix实例(1,8)//实例有8个实属性和
双重输出;//属性“class”中的值是名词性的
实例[0][0]=6;
实例[0][1]=148;
实例[0][2]=72;
实例[0][3]=35;
实例[0][4]=0;
实例[0][5]=33.6;
实例[0][6]=0.62;
实例[0][7]=50;
modell->predict(实例[0],&out);

我怀疑您的代码没有打印任何内容的原因是您忘记了
endl
。(这是他在评论中提到的。)

如果您使用的是Visual Studio,您可能希望在代码末尾(可能在return语句上)添加一个断点,因为在某些模式下,它可以在您看到输出之前关闭应用程序,这会使它看起来好像什么都没有发生

例子 下面是一个完整的例子,对我来说很好。它包括您的实例。它从
2blobs_knn.json
加载一个K-最近邻学习器,然后在其上计算实例。您可以将该文件名替换为由华夫格工具生成的任何经过培训的受监督模型的名称

对于我使用的模型,程序打印“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;
}
然后,我拿了那个CSV,用

waffles_transform import 2blobs.csv > 2blobs.arff
接下来,我将最后一个属性从
@attr8 real
更改为
@属性类{0,1}
在文本编辑器中,因此它将是标称的

最后,我用

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

语句
coutHow about
cout No,您不想将
system(“Pause”)
添加到代码中,而不管编译器/IDE如何。VisualStudio将把stdout路由到它的输出窗口,该窗口位于应用程序的末尾,因此完全没有必要。这取决于VisualStudio的版本。在2008年(上次我使用VS时),如果在调试模式(F5)而不是发布模式(CTRL-F5)下执行,它会将输出发送到终端并关闭终端。例如,参见。(顺便说一句,MS Q&A看起来有点像StackOverflow)即使您处于附加调试模式下,
main
末尾的断点仍然更简单(以后不必删除),也可以正常工作。好的一点,我将编辑我的答案,建议使用断点,而不是
系统(“暂停”)这不是我可以在评论中完全回答的问题。很快,您需要在经过训练的模型中使用serialize函数将其保存到DOM,然后使用GDOM::saveJson将其写出。如果你需要更多的细节,请问另一个问题。
waffles_learn train 2blobs.arff knn -neighbors 10 > 2blobs_knn.json