我有一些测试数据和标签:
testZ = [0.25, 0.29, 0.62, 0.27, 0.82, 1.18, 0.93, 0.54, 0.78, 0.31, 1.11, 1.08, 1.02];
testY = [1 1 1 1 1 2 2 2 2 2 2 2 2];
然后我对它们进行排序:
[sZ, ind] = sort(testZ); %%Sorts Z, and gets indexes of Z
sY = testY(ind); %%Sorts Y by index
[N, n] = size(testZ');
然后,这将给出已排序的Y数据。在排序的Y数据的每个元素处,我想将左边的每个点分类为类型1,右边的所有点都是类2;然后对数据的每个点重复这一过程。我该如何做,并找出每个元素的变量:
这样做的目的是,我可以为分类器创建一个ROC曲线,作为一些学校工作的一部分。
答案 0 :(得分:1)
以下是绘制ROC和查找AUC值的代码:
tot_op = testZ;
targets = testY;
th_vals= sort(tot_op);
for i = 1:length(th_vals)
b_pred = (tot_op>=th_vals(i,1));
TP = sum(b_pred == 1 & targets == 2);
FP = sum(b_pred == 1 & targets == 1);
TN = sum(b_pred == 0 & targets == 1);
FN = sum(b_pred == 0 & targets == 2);
sens(i) = TP/(TP+FN);
spec(i) = TN/(TN+FP);
end
figure(2);
cspec = 1-spec;
cspec = cspec(end:-1:1);
sens = sens(end:-1:1);
plot(cspec,sens,'k');
AUC = sum(0.5*(sens(2:end)+sens(1:end-1)).*(cspec(2:end) - cspec(1:end-1)));
fprintf('\nAUC: %g \n',AUC);
以上代码是http://www.dcs.gla.ac.uk/~srogers/firstcourseml/matlab/chapter5/svmroc.html
上给出的修改版本答案 1 :(得分:0)
您可以遍历这些点,然后使用逻辑索引和逐元素布尔运算符来获得您想要的大部分内容。
for i = 1:length(sY)
classification = [ones(1,i-1) 2*ones(1,length(sy)-i+1)];
isTruePositive = ( (sY == classification) & (sY == 1) );
numberOfTruePositive = sum(isTruePositive);
% Similar for other cases.
% Use the result in the loop or store it somewhere - as written here
% variables are over-written each iteration in the loop.
end
我没有运行这个,所以你可能需要一些调整,但这应该可以帮到你。