Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: KNN classifier with ROC Analysis
Replies: 1   Last Post: Jun 18, 2013 11:58 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Aaronne

Posts: 110
Registered: 6/2/11
KNN classifier with ROC Analysis
Posted: Mar 19, 2013 8:30 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi Smart guys,

I wrote following codes to get a plot of ROC for my KNN classifier:

load fisheriris;

features = meas;
featureSelcted = features;
numFeatures = size(meas,1);

%% Define ground truth
groundTruthGroup = species;

%% Construct a KNN classifier
KNNClassifierObject = ClassificationKNN.fit(featureSelcted, groundTruthGroup, 'NumNeighbors', 3, 'Distance', 'euclidean');

% Predict resubstitution response of k-nearest neighbor classifier
[KNNLabel, KNNScore] = resubPredict(KNNClassifierObject);

% Fit probabilities for scores
groundTruthNumericalLable = [ones(50,1); zeros(50,1); -1.*ones(50,1)];
[FPR, TPR, Thr, AUC, OPTROCPT] = perfcurve(groundTruthNumericalLable(:,1), KNNScore(:,1), 1);

Then we can plot the FPR vs TPR to get the ROC curve.

However, the FPR and TPR is different from what I got using my own implementation that the one above will not display all the points, actually, the codes above display only three points on the ROC. The codes I implemented will dispaly 151 points on the ROC as the size of the data is 150.

patternsKNN = [KNNScore(:,1), groundTruthNumericalLable(:,1)];
patternsKNN = sortrows(patternsKNN, -1);
groundTruthPattern = patternsKNN(:,2);

POS = cumsum(groundTruthPattern==1);
TPR = POS/sum(groundTruthPattern==1);
NEG = cumsum(groundTruthPattern==0);
FPR = NEG/sum(groundTruthPattern==0);

FPR = [0; FPR];
TPR = [0; TPR];


May I ask how to tune '`perfcurve`' to let it output all the points for the ROC? Thanks a lot.



A.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.