Borrar filtros
Borrar filtros

Code Optimization & simplification: BSXFUN & Confusion Matrix Calculation

1 visualización (últimos 30 días)
Dear all
The main objective of this thread is toward codes optimization & simplification.
There are two objectives 1) To compare an array A to array B. If array A is larger than array S then return logical 1, or otherwise. Array A is compromise element by element division between each row at EasyComp(:,1) and EasyComp(:,2). Array S is a vector of similar threshold value. In this example, the array A will be compared with different threshold values. The possible threshold value are -3:0.1:3.
2) The second part is to evaluate the density estimation by calculating element in the confusion matrix.
The ultimate question, is there any ways I can improve this code, specifically avoid the use of to many FOR Loops. The drafted code is as below. The MAT file is attached together in this thread.
load ('EasyComp')
ThrshldResult=EvalThershld (EasyComp);
ConfResult=ConfMatrix_Ev (EasyComp,ThrshldResult);
function ResThRat=EvalThershld (EasyComp)
txt =1;
x=-3:0.1:3; % Threshold value
for k=1:numel(x)
threshold=x(k);
S = repmat(threshold, (length (EasyComp(:,1))),1 );
ResThRat {txt} = bsxfun(@ge, S, (EasyComp(:,1)./EasyComp(:,2)));
txt =txt+1;
end
end
function ConfResult=ConfMatrix_Ev (EasyComp,ThrshldResult)
txc=1;
ActClss=EasyComp(:,3); % Actual class
for kok =1:length(ThrshldResult) % Evaluate every logical in each the cell
PredClss=ThrshldResult{kok}; % Predicted class by varying diff threshold
% In the following lines a comparison between elements in the column THREE
% of the EASYCOMP and Predicted Class.
% 0 0 = True Negetive
% 0 1 = False Negetive
% 1 0 = False Positive
% 1 1 = True Positive
% The all this value store as a CATEGORICAL array name CONFUSION MATRIX: CONFMAT
ConfMat = categorical (repmat({'TN'}, [length(EasyComp) 1])); % True Negative (TN) | Predicted NS, actual NS
ConfMat (logical (((ActClss==0) .*(PredClss==1))))='FN'; % False Negative (FN) | Predicted NS, actual S
ConfMat (logical (((ActClss==1) .*(PredClss==0))))='FP'; % False Positive (FP) | Predicted S, actual NS
ConfMat (logical (((ActClss==1) .*(PredClss==1))))='TP'; % True Positive (TP) | Predicted S, actual S
% % Calculate the True Positive Rate % False Positive Rate which can be used to Plot the Receiver operating characteristic
perf.TPR = (sum(ConfMat(:) == 'TP'))/((sum(ConfMat(:) == 'TP'))+ (sum(ConfMat(:) == 'FN'))); % TP/ (TP+FN) || Sensetivity, SENT is very high if FN is low
perf.FPR = (sum(ConfMat(:) == 'FP'))/((sum(ConfMat(:) == 'FP'))+ (sum(ConfMat(:) == 'TN'))); % FP/ (FP+TN) || Specificity, SPEC is very high is TN is low
result.TPR {txc} = perf.TPR;
result.FPR {txc} = perf.FPR;
%
txc=txc+1;
end
end

Respuestas (0)

Categorías

Más información sobre Multiobjective Optimization en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by