MATLAB: How to get prediction scores from exported Classification Learner SVM model

classification learnermachine learning toolboxpredict scores

Hello, I am using the Classification Learner tool to train a binary classifier, and I am wondering how to obtain scores for the predictions the trained classifier makes on new test data. I have been using the below command to test the classifiers:
yfit = C.predictFcn(T)
But I would like to know how to calculate/access the scores as well after exporting the model to the workspace from Classification Learner. The command
[labels,score] = predict(___)
gives the following error:
Error using predict (line 84)
Systems of struct class cannot be used with the "predict" command. Convert the system to an identified model
first, such as by using the "idss" command.
I am new to this, so if anyone could explain what I am doing incorrectly, and how to get scores for each prediction using an exported classifier, I would greatly appreciate the help.
Thanks!

Best Answer

Hello Ashley,
The predict function expects an input classifier of type such as ClassificationKNN or ClassificationTree etc. In this case you are passing a variable T which is if type struct. That is the reason for the error.
Type the following at the command prompt to see the contents of the struct:
>> T %Variable name of the exported classifier struct
Here one of the fields will be the classification object. For e.g. for my exported model referred by struct variable trainedClassifier, I see the following.
>> trainedClassifier
trainedClassifier =
predictFcn: [function_handle]
RequiredVariables: {1x11 cell}
ClassificationKNN: [1x1 ClassificationKNN]
About: 'This struct is a tra…'
HowToPredict: 'To make predictions …'
Note here that one of the fields is the actual classification object, ClassificationKNN. Note that, based on the type of trained classifier this might differ. To access this we can use the dot operator.
kNNMdl = trainedClassifier.ClassificationKNN;
Now use this as input to the predict function to get scores:
[labels,score] = predict(kNNMdl,ValData);
Related Question