1

I extracted the principal components of training and testing data.
'trainingdata.train' has feature values from both +1(face 1) and -1(all other faces) labels. 'testdata.train' has feature values from face 2 and no label since i want the SVM to predict its label. The "predicted_label" given by LIBSVM is +1 even though it should be -1.

[training_label_matrix, training_instance_matrix] = libsvmread('trainingdata.train');
[testing_label_matrix, testing_instance_matrix] = libsvmread('testdata.train');
model = svmtrain(training_label_matrix, training_instance_matrix);
[predicted_label] = svmpredict(testing_label_matrix, testing_instance_matrix, model);

Please point me out to what i am doing wrong.

lennon310
  • 12,503
  • 11
  • 43
  • 61
Sid
  • 249
  • 5
  • 16

2 Answers2

1

Use [predict_label, accuracy, prob_values] = svmpredict(testLabel, testData, model, '-b 1'); to observe the accuracy.

testLabel is the vector that includes the 'correct' labels of your test data. This parameter is given in order to calculate the accuracy. In the real case that labels of test data are unknown, simply use any random values to get the predict_label without calculating the accuracy.

Besides, although not required, you'd better specify the options in svmtrain, check their page for more details.

lennon310
  • 12,503
  • 11
  • 43
  • 61
  • So in the testing data should i give the label or no? – Sid Jan 27 '14 at 15:42
  • testLabel is the vector that includes the 'correct' labels of your test data. This parameter is given in order to calculate the accuracy. In the real case that labels of test data are unknown, simply use any random values to get the predict_label without calculating the accuracy. – lennon310 Jan 27 '14 at 15:49
  • Lennon i obtained the accuracy when i give the 'testLabel' as the 'correct Label', i did not find and problems with that. You said "In the real case that labels of test data are unknown, simply use any random values to get the predict_label without calculating the accuracy" -- This is what i am not able to do. How do i get the predict_label? – Sid Jan 27 '14 at 15:54
  • 1
    you already got the predict_label. [predict_label, accuracy, prob_values] = svmpredict(testLabel, testData, model, '-b 1'); will give you the predicted label and accuracy altogether. – lennon310 Jan 27 '14 at 16:00
  • 1
    With your current test data, you know their labels, that's why you can add them as the input in testLabel. If you are given a brand new test data without telling you their labels, you can just randomly set testLabel as the input, you will still get predict_label. You just cannot calculate the accuracy because you don't even know the 'correct' labels of those new test data – lennon310 Jan 27 '14 at 16:01
  • Yes lennon i got that but i am sorry i don't know how to randomly set testLabel as the input, to get predict_label. – Sid Jan 27 '14 at 16:04
  • [predict_label, accuracy, prob_values] = svmpredict(ones(size(testData,1),1), testData, model, '-b 1'); Note that with this setting, only predict_label makes sense, and it is what you want to get. No need to look into accuracy – lennon310 Jan 27 '14 at 16:10
  • Lennon thank you so much. I will try this out and get back with the results. – Sid Jan 27 '14 at 16:17
0

@Lennon : So should the code go like this?

[training_label_matrix, training_instance_matrix] = libsvmread('trainingdata.train');
[testing_label_matrix, testing_instance_matrix] = libsvmread('testdata.train');
model = svmtrain(training_label_matrix, training_instance_matrix);
[predict_label, accuracy, prob_values] = svmpredict(ones(size(testData,1),1), testing_instance_matrix, model, '-b 1');
Sid
  • 249
  • 5
  • 16
  • 1
    looks good to me. If you want to check the accuracy, use the true testLabel instead. If you can got high accuracy, it indicates your learning model works well – lennon310 Jan 27 '14 at 16:23
  • I had checked the accuracy earlier , it gave me 100% which got me thinking that something is really wrong with the model/ the features i am using. So i looked into it several times and nothing seemed amiss. I am still wondering what exactly is wrong there! – Sid Jan 27 '14 at 17:12
  • 1
    why you felt sth is wrong if you got a 100% accuracy? The +1 or -1 labels are all given by yourself in your predict process. – lennon310 Jan 27 '14 at 17:59
  • Lennon thank you so much, i am getting the right predicted label now. I tweaked how to input the testing data and gave a random label (4) when it was getting converted from .csv to .train. That's okay to do i presume? – Sid Jan 28 '14 at 07:54
  • that's good ! congratulations! and yes what you did looks good to me. – lennon310 Jan 28 '14 at 12:32
  • @lennon310 : Please look through this question http://stackoverflow.com/questions/21426842/how-to-label-the-training-projections-obtained-by-pca-to-use-for-training-svm-fo/21437343?noredirect=1#comment32346217_21437343 – Sid Jan 30 '14 at 01:50