MATLAB Answers

How To Draw The ROC Curve for SVM, KNN, & Naive Bayes Classifiers?

조회 수: 93(최근 30일)
abdelhamid bourouhou
abdelhamid bourouhou 2015년 12월 16일
답변: SOLOMON Beyene 2019년 11월 8일
Hello, I'm not a professional MATLAB user, so I have some problem to find what I want. My problem is how can I draw the roc curve for SVM, KNN, & Naive Bayes Classifiers. For classification I use the "fit" to train my classifiers and "predict" to classify the test samples, and to find a roc curve I tried "plotroc" & "perfcurve", but without being able to draw curve. Can you Help me? I use MATLAB R2014a for information.


Vladimir Calderón
Vladimir Calderón 2019년 3월 2일
Load the sample data.
load ionosphere
X is a 351x34 real-valued matrix of predictors. Y is a character array of class labels: 'b' for bad radar returns and 'g' for good radar returns.
Reformat the response to fit a logistic regression. Use the predictor variables 3 through 34.
resp = strcmp(Y,'b'); % resp = 1, if Y = 'b', or 0 if Y = 'g'pred = X(:,3:34);
Fit a logistic regression model to estimate the posterior probabilities for an iris to be a virginica.
mdl = fitglm(pred,resp,'Distribution','binomial','Link','logit'); score_log = mdl.Fitted.Probability; % Probability estimates
Compute the standard ROC curve using the probabilities for scores.
[Xlog,Ylog,Tlog,AUClog] = perfcurve(resp,score_log,'true');
Train an SVM classifier on the same sample data. Standardize the data.
mdlSVM = fitcsvm(pred,resp,'Standardize',true);
Compute the posterior probabilities (scores).
mdlSVM = fitPosterior(mdlSVM); [~,score_svm] = resubPredict(mdlSVM);
The second column of score_svm contains the posterior probabilities of bad radar returns.
Compute the standard ROC curve using the scores from the SVM model.
[Xsvm,Ysvm,Tsvm,AUCsvm] = perfcurve(resp,score_svm(:,mdlSVM.ClassNames),'true');
Fit a naive Bayes classifier on the same sample data.
mdlNB = fitcnb(pred,resp);
Compute the posterior probabilities (scores).
[~,score_nb] = resubPredict(mdlNB);
Compute the standard ROC curve using the scores from the naive Bayes classification.
[Xnb,Ynb,Tnb,AUCnb] = perfcurve(resp,score_nb(:,mdlNB.ClassNames),'true');
Plot the ROC curves on the same graph.
plot(Xlog,Ylog) hold onplot(Xsvm,Ysvm) plot(Xnb,Ynb) legend('Logistic Regression','Support Vector Machines','Naive Bayes','Location','Best') xlabel('False positive rate'); ylabel('True positive rate'); title('ROC Curves for Logistic Regression, SVM, and Naive Bayes Classification') hold off
  댓글 수: 2
Tianpei Li
Tianpei Li 2019년 6월 28일
mdlknn = fitcknn(X,Y,'NumNeighbors',5,'Standardize',1)
[~,score_knn] = resubPredict(mdlknn);
[Xknn,Yknn,Tknn,AUCknn] = perfcurve(Y,score_knn(:,mdlknn.ClassNames),'true');

댓글을 달려면 로그인하십시오.

SOLOMON Beyene 2019년 11월 8일
I want to draw ROC curve for 6 algorithms(IBK,RF,ANN,KNN,J48,NB)
I have csv file with multclass..coulum contain features and rows contain class.
I am new user of Matlab ,can anyone help me how to load and draw in matlab

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by