Cannot classify correctly.

asked 2016-10-19 07:41:15 -0600

r6colin gravatar image

Dear Sir, I am playing with the model of Logistic Regression. I just want to classify a set of numbers from 0 to 9 for the numbers smaller than 5 as class "0" and others as class "1". However, It has a response of number 0 as "0" others as "1". Would someone tell me what's wrong. thank a lot.

Mat data(30, 1, CV_32S);
Mat labels(30, 1, CV_32S);
Mat test(30, 1, CV_32S);
// make training and testing data
for (int i = 0; i < 30; i++) {
    data.at<int>(i) = i % 10;
    test.at<int>(i) = i % 10;
    if (i % 10 < 5) {
        labels.at<int>(i) = 0;
    }
    else {
        labels.at<int>(i) = 1;
    }
}
cout << data.t() << endl;
cout << labels.t() << endl;

data.convertTo(data, CV_32F);
labels.convertTo(labels, CV_32F);

cout << "Training samples:" << data.rows << ", Testing samples:" << test.rows << endl;
cout << "Training..." << endl;

Ptr<LogisticRegression> lr1 = LogisticRegression::create();
lr1->setLearningRate(0.001);
lr1->setIterations(10);
lr1->setRegularization(LogisticRegression::REG_L2);
lr1->setTrainMethod(LogisticRegression::BATCH);
lr1->setMiniBatchSize(1);
lr1->train(data, ROW_SAMPLE, labels);
cout << "Testing..." << endl;

Mat responses;
test.convertTo(test, CV_32F);
cout << test.t() << endl;
lr1->predict(test, responses);
cout << "Done!" << endl << endl;

cout << "Original vs Predicted:" << endl;
labels.convertTo(labels, CV_32S);
cout << labels.t() << endl;
cout << responses.t() << endl;
getchar();
return 0;

Train data: 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 Labels: 0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1 Test=Train Response: 0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1

edit retag flag offensive close merge delete