LCOV - code coverage report
Current view: top level - src - gensvm_predict.c (source / functions) Hit Total Coverage
Test: coverage.all Lines: 31 31 100.0 %
Date: 2017-02-21 18:44:20 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file gensvm_predict.c
       3             :  * @author G.J.J. van den Burg
       4             :  * @date 2013-08-09
       5             :  * @brief Main functions for predicting class labels..
       6             :  *
       7             :  * @details
       8             :  * This file contains functions for predicting the class labels of instances
       9             :  * and a function for calculating the predictive performance (hitrate) of
      10             :  * a prediction given true class labels.
      11             :  *
      12             :  * @copyright
      13             :  Copyright 2016, G.J.J. van den Burg.
      14             : 
      15             :  This file is part of GenSVM.
      16             : 
      17             :  GenSVM is free software: you can redistribute it and/or modify
      18             :  it under the terms of the GNU General Public License as published by
      19             :  the Free Software Foundation, either version 3 of the License, or
      20             :  (at your option) any later version.
      21             : 
      22             :  GenSVM is distributed in the hope that it will be useful,
      23             :  but WITHOUT ANY WARRANTY; without even the implied warranty of
      24             :  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      25             :  GNU General Public License for more details.
      26             : 
      27             :  You should have received a copy of the GNU General Public License
      28             :  along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
      29             : 
      30             :  */
      31             : 
      32             : #include "gensvm_predict.h"
      33             : 
      34             : /**
      35             :  * @brief Predict class labels of data given and output in predy
      36             :  *
      37             :  * @details
      38             :  * The labels are predicted by mapping each instance in data to the
      39             :  * simplex space using the matrix V in the given model. Next, for each
      40             :  * instance the nearest simplex vertex is determined using an Euclidean
      41             :  * norm. The nearest simplex vertex determines the predicted class label,
      42             :  * which is recorded in predy.
      43             :  *
      44             :  * @param[in]   testdata        GenData to predict labels for
      45             :  * @param[in]   model           GenModel with optimized V
      46             :  * @param[out]  predy           pre-allocated vector to record predictions in
      47             :  */
      48           2 : void gensvm_predict_labels(struct GenData *testdata, struct GenModel *model,
      49             :                 long *predy)
      50             : {
      51             :         long i, j, k, n, K, label;
      52             :         double norm, min_dist,
      53           2 :                *S = NULL,
      54           2 :                *ZV = NULL;
      55             : 
      56           2 :         n = testdata->n;
      57           2 :         K = model->K;
      58             : 
      59             :         // allocate necessary memory
      60           2 :         S = Calloc(double, K-1);
      61           2 :         ZV = Calloc(double, n*(K-1));
      62             : 
      63             :         // Generate the simplex matrix
      64           2 :         gensvm_simplex(model);
      65             : 
      66             :         // Generate the simplex space vectors
      67           2 :         gensvm_calculate_ZV(model, testdata, ZV);
      68             : 
      69             :         // Calculate the distance to each of the vertices of the simplex.
      70             :         // The closest vertex defines the class label
      71          26 :         for (i=0; i<n; i++) {
      72          24 :                 label = 0;
      73          24 :                 min_dist = INFINITY;
      74          96 :                 for (j=0; j<K; j++) {
      75         216 :                         for (k=0; k<K-1; k++) {
      76         288 :                                 S[k] = matrix_get(ZV, K-1, i, k) -
      77         144 :                                         matrix_get(model->U, K-1, j, k);
      78             :                         }
      79          72 :                         norm = cblas_dnrm2(K-1, S, 1);
      80          72 :                         if (norm < min_dist) {
      81          44 :                                 label = j+1;
      82          44 :                                 min_dist = norm;
      83             :                         }
      84             :                 }
      85          24 :                 predy[i] = label;
      86             :         }
      87             : 
      88           2 :         free(ZV);
      89           2 :         free(S);
      90           2 : }
      91             : 
      92             : /**
      93             :  * @brief Calculate the predictive performance (percentage correct)
      94             :  *
      95             :  * @details
      96             :  * The predictive performance is calculated by simply counting the number
      97             :  * of correctly classified samples and dividing by the total number of
      98             :  * samples, multiplying by 100.
      99             :  *
     100             :  * @param[in]   data    the GenData dataset with known labels
     101             :  * @param[in]   predy   the predicted class labels
     102             :  *
     103             :  * @returns percentage correctly classified.
     104             :  */
     105           3 : double gensvm_prediction_perf(struct GenData *data, long *predy)
     106             : {
     107           3 :         long i, correct = 0;
     108             :         double performance;
     109             : 
     110          27 :         for (i=0; i<data->n; i++)
     111          24 :                 if (data->y[i] == predy[i])
     112           8 :                         correct++;
     113             : 
     114           3 :         performance = ((double) correct)/((double) data->n)* 100.0;
     115             : 
     116           3 :         return performance;
     117             : }

Generated by: LCOV version 1.12