Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

can't help you with ann, but here's KNearest:

Ptr<ml::KNearest>  knn(ml::KNearest::create());
Mat_<float> trainFeatures(6,4);
trainFeatures << 2,2,2,2,
                 3,3,3,3,
                 4,4,4,4,
                 5,5,5,5,
                 6,6,6,6,
                 7,7,7,7;

Mat_<int> trainLabels(1,6);
trainLabels << 0,1,2,3,4,5,6;

knn->train(trainFeatures, ml::ROW_SAMPLE, trainLabels);

Mat_<float> testFeature(1,4);
testFeature<< 3,3,3,3;


int K=1;
Mat resp,results,dist;
float r = knn->findNearest(testFeature, K, results, resp, dist);
cerr << r << endl;          
cerr << results << endl;
cerr << resp << endl;
cerr << dist<< endl;

// r and results have only a meaningful content in the case of K=1 
1
[1]
[1]
[0]

// for K=4 we get:
0
[0]
[1, 0, 2, 3]
[0, 4, 4, 16]

can't help you with ann, but here's KNearest:

Ptr<ml::KNearest>  knn(ml::KNearest::create());
Mat_<float> trainFeatures(6,4);
trainFeatures << 2,2,2,2,
                 3,3,3,3,
                 4,4,4,4,
                 5,5,5,5,
                 6,6,6,6,
                 7,7,7,7;

Mat_<int> trainLabels(1,6);
trainLabels << 0,1,2,3,4,5,6;
2,3,4,5,6,7;

knn->train(trainFeatures, ml::ROW_SAMPLE, trainLabels);

Mat_<float> testFeature(1,4);
testFeature<< 3,3,3,3;


int K=1;
Mat resp,results,dist;
float r = knn->findNearest(testFeature, K, results, resp, dist);
cerr << r << endl;          
cerr << results << endl;
cerr << resp << endl;
cerr << dist<< endl;

// r and results have only a meaningful content in the case of K=1 
1
[1]
[1]
3
[3]
[3]
[0]

// for K=4 we get:
0
[0]
[1, 0, 2
[2]
[3, 2, 3]
4, 5]
[0, 4, 4, 16]

can't help you with ann, but here's KNearest:

Ptr<ml::KNearest>  knn(ml::KNearest::create());
Mat_<float> trainFeatures(6,4);
trainFeatures << 2,2,2,2,
                 3,3,3,3,
                 4,4,4,4,
                 5,5,5,5,
                 6,6,6,6,
                 7,7,7,7;

Mat_<int> trainLabels(1,6);
trainLabels << 2,3,4,5,6,7;

knn->train(trainFeatures, ml::ROW_SAMPLE, trainLabels);

Mat_<float> testFeature(1,4);
testFeature<< 3,3,3,3;


int K=1;
Mat resp,results,dist;
float r = response,dist;
knn->findNearest(testFeature, K, results, resp, noArray(), response, dist);
cerr << r << endl;          
cerr << results << endl;
cerr << resp response << endl;
cerr << dist<< endl;

// r and results have only a meaningful content in the case of K=1 
3
[3]
[3]
[0]

// for K=4 we get:
2
[2]
K=4:
[3, 2, 4, 5]
[0, 4, 4, 16]