简体   繁体   English

测试性能分类器Matlab

[英]testing performance classifier matlab

Hello I have trained a classifier in matlab and I would like to test its accuracy. 您好,我已经在matlab中训练了一个分类器,我想测试其准确性。 I have found a lot of functions to do that and I am puzzling on what to use... 我发现有很多功能可以做到这一点,我很困惑该使用什么...

at the moment: 在这一刻:

% train my classifier
svmStruct = fitcsvm(Xtrain,Ytrain,'KernelFunction','linear','Standardize',true);

% predict the output of an unknown input <- this part takes a lot of time
IDX_svm = zeros(size(Xtest,1),1);
for j = 1 : size(Xtest,1)    
    IDX_svm(j) = predict(svmStruct,Xtest(j,:));    
end

%compute performaces
TABLE = confusionmat(Ytest,IDX_svm);
sum_diag = 0;
for j = 1 : size(TABLE,2)
    sum_diag = sum_diag + TABLE(j,j);
end
error_rate = 1-(sum_diag/sum(sum(TABLE,2)));

The accuracy is simply defined as the ratio between the correctly predicted labels and the total number of labels in the testing/validation set. 准确度简单地定义为正确预测的标签与测试/验证集中的标签总数之间的比率。 So instead of using the confusion matrix, if you have the Testing Labels vector ( Ytest I suppose) and the Predicted Labels vector ( IDX_svm I suppose), you can simply run 因此,如果没有“测试标签”向量(我假设是Ytest )和“预测标签”向量(我假设是IDX_svm ),则可以不使用混淆矩阵,而只需运行

Accuracy=sum(IDX_svm==Ytest)/length(Ytest)

Accuracy will be in range [0;1], which you can scale in percentage by simply multiplying it by 100. Accuracy将在[0; 1]范围内,您可以通过将其乘以100来按百分比缩放。

The Error Rate, of course, is defined according to one of the following: 当然,错误率是根据以下其中一项定义的:

  • If you have previously evaluated the accuracy 如果您之前已经评估过准确性
    • 1-Accuracy if Accuracy is in range [0;1] 如果Accuracy在[0; 1]范围内,则为1-Accuracy
    • 100-Accuracy if Accuracy is given in percentage 100-Accuracy如果Accuracy以百分比的形式给出
  • If you want to evaluate just the error rate 如果您只想评估错误率
    • the ratio between the in correctly predicted labels and the total number of labels in the testing/validation set: ErrorRate=sum(IDX_svm~=Ytest)/length(Ytest) . 正确地预测的标签和在测试/验证标签集的总数量之间的比率ErrorRate=sum(IDX_svm~=Ytest)/length(Ytest) Such value will be in range [0;1] 该值将在[0; 1]范围内
    • multiplying the above result by 100 will give the error rate in percentage 将以上结果乘以100将得出百分比的错误率

These are standard definitions and they work for every classifier, not just the SVM. 这些是标准定义,它们适用于每个分类器,而不仅仅是SVM。

Also, I recommend to avoid the predict() function in a loop. 另外,我建议避免循环使用predict()函数。 In this case you call predict() several times and each call classifies a single point. 在这种情况下,您多次调用predict() ,并且每次调用都对一个点进行分类。 This slows down your code. 这会使您的代码变慢。 As you might now, predict() can take the whole testing matrix as input and returns the entire label vector so you don't have to preallocate and write every single element of such vector inside a for-loop. 如您现在predict()predict()可以将整个测试矩阵作为输入并返回整个标签向量,因此您不必在for循环内预先分配和写入该向量的每个元素。 And you call predict() only once. 而且您只调用一次predict() You might want to try something like 您可能想尝试类似

IDX_svm=predict(svmStruct,Xtest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM