From 021ceb3175968638d72a989c74dd113737c349da Mon Sep 17 00:00:00 2001 From: Alberto Carlevaro <99175531+AlbiCarle@users.noreply.github.com> Date: Sat, 14 Jan 2023 11:48:29 +0100 Subject: [PATCH] Hello everyone ! I add two new files to my repository: 1) CohenKappa.m for evaluating the classification and 2) CV_MultiSVDD for the cross validation. --- CV_MultiSVDD.m | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ CohenKappa.m | 26 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 CV_MultiSVDD.m create mode 100644 CohenKappa.m diff --git a/CV_MultiSVDD.m b/CV_MultiSVDD.m new file mode 100644 index 0000000..460c544 --- /dev/null +++ b/CV_MultiSVDD.m @@ -0,0 +1,70 @@ +function [param_star, C_star, err_matrix] = ... + CV_MultiSVDD(X, Y, kernel,nrip, KerPar,Cpar) + +% Cross Validation function for MC-SVDD + + Num_class = length(unique(Y)); + minimum_abs = 100; + + for rip = 1:nrip + + disp(['--->', num2str(rip)]) + + cv = cvpartition(Y,'HoldOut',0.3, 'Stratify',true); + idx = cv.test; + + Xtr = X(~idx,:); Ytr = Y(~idx,:); + Xvl = X(idx,:); Yvl = Y(idx,:); + + err_matrix = zeros(size(KerPar,2),size(Cpar,1)); + + i = 0; + + for param = KerPar + + i = i + 1; + + j = 0; + + for C = Cpar' + + C = C'; + + j = j + 1; + + [x_class, Ytr_class, Rsquared_class, a_class, SV_class, YSV_class]=... + NC_SVDD_TRAINING(Xtr, Ytr, Num_class, kernel, param, C); + + y_predict = ... + NC_SVDD_TEST(Xtr, Ytr_class, Num_class, x_class, Xvl, kernel, param, Rsquared_class); + + n = size(Yvl,1); + + err = (n-sum(Yvl == y_predict))/n; + + err_matrix(i,j) = err; + + end + end + + minimum = min(min(err_matrix)); + + if minimum < minimum_abs + + [x,y]=find(err_matrix==minimum); + + param_star = KerPar(x(1)); + + C_star = Cpar(y(1),:); + + minimum_abs = minimum; + + disp(minimum_abs) + + end + + end + +disp('Done') + +end diff --git a/CohenKappa.m b/CohenKappa.m new file mode 100644 index 0000000..45f47c1 --- /dev/null +++ b/CohenKappa.m @@ -0,0 +1,26 @@ +function K = CohenKappa(X, Y, y_pred, Num_class) + +% Function that computes the CohenKappa index for multiclassification +% algorithms + + CM = ConfusionMatrix(Y, y_pred, Num_class); + + c = sum(diag(CM)); + + s = size(X,1); + + p = []; t = []; + + for k =1:Num_class + + p_k = sum(CM(:,k)); + + p = [p,p_k]; + + t_k = sum(CM(k,:)); + + t = [t,t_k]; + + end + + K = ((c*s) - sum(p.*t))/(s^2 - sum(p.*t)); \ No newline at end of file