我编写了以下代码来获取我的 KNN 分类器的 ROC 图:
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);
然后我们可以绘制 FPR 与 TPR 的曲线来获得 ROC 曲线。
但是,FPR 和 TPR 与我使用自己的实现得到的不同,上面的代码不会显示所有的点,实际上,上面的代码只显示 ROC 上的三个点。我实现的代码将在 ROC 上显示 151 个点,因为数据大小为 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];
请问如何调' perfcurve
'让它输出ROC的所有分数?非常感谢。
一种。