我试图了解Octave函数中返回值的方式。我写了一个函数,它返回多个变量,[error, accuracy, precision, npv, recall, F1_score]
。
我尝试实现的目标是将每个 结果变量返回到控制台而不是标准ans
(其中似乎总是第一个变量。)
所以这很好用:
>> [error, accuracy, precision, npv, recall, F1_score] = describeConfMatrix([546, 0;314, 0])
error = 0.36512
accuracy = 0.63488
precision = 0.63488
npv = NA
recall = 1
F1_score = 0.77667
但这会产生' ans':
>> z = describeConfMatrix([546, 0;314, 0])
z = 0.36512
>> describeConfMatrix([546, 0;314, 0])
ans = 0.36512
虽然我希望看到整个返回变量列表。
我也想让这个工作(测试函数的断言):
m = [546, 0;314, 0];
ans = [0.36512, 0.63488, 0.63488, NA, 1, 0.77667];
assert(isequal(describeConfMatrix(m),ans))
非常感谢Matlab新手!
这是我的代码:
function [error, accuracy, precision, npv, recall, F1_score] = describeConfMatrix(m)
TP = m(1,1);
FN = m(1,2);
FP = m(2,1);
TN = m(2,2);
K = TP + FN + FP + TN;
error = (FP + FN) / K;
accuracy = (TP + TN) / K;
precision = TP / (TP + FP); # sometimes called 'positive predictive value'
recall = TP / (TP + FN);
if (FN + TN) != 0
npv = TN / (FN + TN);
else
npv = NA;
end;
F1_score = (2*precision*recall)/(precision + recall);
end
答案 0 :(得分:6)
describeConfMatrix([546, 0;314, 0])
),它将仅将第一个输出变量作为ans
返回。 z = describeConfMatrix([546, 0;314, 0])
)。 [error, accuracy, precision, npv, recall, F1_score] = describeConfMatrix([546, 0;314, 0])
这是MATLAB / Octave中的正常和预期行为。由于同样的原因,您的assert
行无法正常工作。