Ask Your Question
0

How to combine several features of a hand for machine learning

asked 2016-03-26 13:46:14 -0600

bob409 gravatar image

updated 2016-03-26 22:21:11 -0600

berak gravatar image

I have extracted the following features of hand: compactness, aspect ration and orientation and stored them in a .csv file. Now I want to train them using KNN. KNN takes a matrix as input, how should I combines these features into a single Mat and train them.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2016-03-26 22:03:44 -0600

berak gravatar image

updated 2016-03-26 22:16:59 -0600

for machine learning with opencv, you need a continuous MxN (float)Mat, where M(rows) is the number of feature vectors, and N(cols) is the feature count. also you need a Mx1 (int) Mat with the labels, one per feature row.

like this:

  feature1        1
  feature2        1
  feature3        2
  ...

you can construct your training data manually (pseudocode):

Mat data, labels; // initially empty.
for each featurevec:
    // this is the same format we need later for testing, see below !:
    Mat row;
    row.push_back(2.2f); // compactness
    row.push_back(1.2f); // aspect
    row.push_back(22.0f); // orient
    data.push_back(row.reshape(1,1)); // flat row

    labels.push_back(17);

Ptr<ml::TrainData> td = ml::TrainData::create(data, ml::ROW_SAMPLE, labels);

but, if you already have a csv file, and it looks like this:

compactness, aspect_ratio, orientation, label
2.2, 1.4, 27, 1
3.2, 3.4, 7, 1
1.2, 2.4, 17, 2
2.6, 1.2, 2, 2

then it's a piece of cake:

    // train:
    Ptr<ml::TrainData> td = ml::TrainData::loadFromCSV("my.csv",1); // 1 header row
    Ptr<ml::KNearest> knn = ml::KNearest::create();
    knn->train(td);

    // later, test:
    Mat test;
    test.push_back(2.2f); // compactness
    test.push_back(1.2f); // aspect
    test.push_back(22.0f); // orient
    // reshape to row-vec, and predict:
    Mat res;
    knn->findNearest(test.reshape(1,1), 3, res);
    cerr << res << endl;
edit flag offensive delete link more

Comments

Thanks a lot.

bob409 gravatar imagebob409 ( 2016-03-27 02:11:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-03-26 13:46:14 -0600

Seen: 541 times

Last updated: Mar 26 '16