필터 지우기
필터 지우기

What does this line of code do?

조회 수: 1 (최근 30일)
Asim Shahzad
Asim Shahzad 2021년 5월 29일
댓글: Cris LaPierre 2021년 5월 29일
I've been given a classifier performance evaluation code I'm trying to understand and run. The first line of code gives an error on my data.
ACTUAL = 9957x1 Categorical (My class labels)
When the code reaches the first line
idx = (ACTUAL()==1);
it says Invalid types for comparison. What exactly is this line of code trying to do? Understanding this will help me fix it.
Here's the function code:
function EVAL = Evaluate(ACTUAL,PREDICTED)
% This fucntion evaluates the performance of a classification model by
% calculating the common performance measures: Accuracy, Sensitivity,
% Specificity, Precision, Recall, F-Measure, G-mean.
% Input: ACTUAL = Column matrix with actual class labels of the training
% examples
% PREDICTED = Column matrix with predicted class labels by the
% classification model
% Output: EVAL = Row matrix with all the performance measures
idx = (ACTUAL()==1);
p = length(ACTUAL(idx));
n = length(ACTUAL(~idx));
N = p+n;
tp = sum(ACTUAL(idx)==PREDICTED(idx));
tn = sum(ACTUAL(~idx)==PREDICTED(~idx));
fp = n-tn;
fn = p-tp;
tp_rate = tp/p;
tn_rate = tn/n;
accuracy = (tp+tn)/N;
sensitivity = tp_rate;
specificity = tn_rate;
precision = tp/(tp+fp);
recall = sensitivity;
f_measure = 2*((precision*recall)/(precision + recall));
gmean = sqrt(tp_rate*tn_rate);
EVAL = [accuracy sensitivity specificity precision recall f_measure gmean];

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 5월 29일
You have a data type mismatch. You are passing in a variable (Actual) that is categorical, while the function you are using expects it to be either numeric or logical.
A=1:5
A = 1×5
1 2 3 4 5
idx1 = (A==1)
idx1 = 1×5 logical array
1 0 0 0 0
% Now as categorical
B=categorical(1:5)
B = 1×5 categorical array
1 2 3 4 5
idx2 = B==1
Error using == (line 30)
Invalid types for comparison.
  댓글 수: 2
Asim Shahzad
Asim Shahzad 2021년 5월 29일
So this function will only work with class labels that are numerical?
Cris LaPierre
Cris LaPierre 2021년 5월 29일
I haven't tested your function, but the line of code giving the error will only work for numeric and logical data.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by