#include "stdafx.h"
#include "orsci.h"
#include "orsciDM.h"
using namespace orsci;
using namespace dm;
Demo_KNN()
{
cout << "orsci: kNN demo..." << endl;
cout << endl;
mdouble A = "1.14, 1.78; 1.18, 1.96; 1.20, 1.86; 1.26, 2.00; 1.28, 2.00; 1.30, 1.96";
mdouble B = "1.24, 1.72; 1.36, 1.74; 1.38, 1.64; 1.38, 1.82; 1.38, 1.90; 1.40, 1.70; 1.48, 1.82; 1.54, 1.82; 1.56, 2.08";
rowdouble x0 = "1.24, 1.80"; //book
rowdouble x1 = "1.50, 2.10"; //book
rowdouble x2 = "1.28, 1.86"; //练习题。
cout << "A= " << endl;
cout << A << endl;
cout << "B= " << endl;
cout << B << endl;
dm::TClassifier_kNN mModel;
list_mdouble mDataList;
mDataList.resize(2, false);
mDataList[0] = A;
mDataList[1] = B;
mModel.train(mDataList);
cout << "test ... " << endl;
vint mCatList;
vdouble mDistList;
mModel.compute_dist_Euclid(x0, mCatList, mDistList, true);
for (int k = 0; k < mCatList.size(); ++ k)
{
cout << "[" << k << "]\t" << mCatList[k] << "\t" << mDistList[k] << endl;
}
mdouble testset = A.conn_push_back_row(B); //将B追加到A的末尾形成测试集。
colint pred = mModel.test_dist_Euclid(testset, 3, 5, 0.3);
cout << pred.T() << endl;
}
int main(int argc, _TCHAR* argv[])
{
Demo_KNN();
cout << endl;
cout << "press any key to stop..." << endl;
char pp;
cin >> pp;
return 0;
}
|