diff options
| author | Gertjan van den Burg <burg@ese.eur.nl> | 2013-10-18 15:48:59 +0200 |
|---|---|---|
| committer | Gertjan van den Burg <burg@ese.eur.nl> | 2013-10-18 15:48:59 +0200 |
| commit | 6d064658f8ae7ca0f42fef6dcc7f896144e9637b (patch) | |
| tree | a41e8793f71f637b68f862220ae5566f4537073d /src/msvmmaj_pred.c | |
| parent | allow seeding of V and added documentation (diff) | |
| download | gensvm-6d064658f8ae7ca0f42fef6dcc7f896144e9637b.tar.gz gensvm-6d064658f8ae7ca0f42fef6dcc7f896144e9637b.zip | |
restart using git
Diffstat (limited to 'src/msvmmaj_pred.c')
| -rw-r--r-- | src/msvmmaj_pred.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/msvmmaj_pred.c b/src/msvmmaj_pred.c new file mode 100644 index 0000000..5f1b1ae --- /dev/null +++ b/src/msvmmaj_pred.c @@ -0,0 +1,111 @@ +/** + * @file msvmmaj_pred.c + * @author Gertjan van den Burg (burg@ese.eur.nl) + * @date August 9, 2013 + * @brief Main functions for predicting class labels.. + * + */ + +#include <cblas.h> + +#include "libMSVMMaj.h" +#include "MSVMMaj.h" +#include "matrix.h" +#include "msvmmaj_pred.h" + +/** + * @name predict_labels + * @brief Predict class labels of data given and output in predy + * + * The labels are predicted by mapping each instance in data to the + * simplex space using the matrix V in the given model. Next, for each + * instance the nearest simplex vertex is determined using an Euclidean + * norm. The nearest simplex vertex determines the predicted class label, + * which is recorded in predy + * + * @param [in] data data to predict labels for + * @param [in] model model with optimized V + * @param [out] predy pre-allocated vector to record predictions in + */ +void msvmmaj_predict_labels(struct MajData *data, struct MajModel *model, long *predy) +{ + long i, j, k, label; + double norm, min_dist; + + long n = data->n; // note that model->n is the size of the training sample. + long m = data->m; + long K = model->K; //data->K does not necessarily equal the original K. + + double *S = Calloc(double, K-1); + double *ZV = Calloc(double, n*(K-1)); + double *U = Calloc(double, K*(K-1)); + + // Get the simplex matrix + msvmmaj_simplex_gen(K, U); + + // Generate the simplex-space vectors + cblas_dgemm( + CblasRowMajor, + CblasNoTrans, + CblasNoTrans, + n, + K-1, + m+1, + 1.0, + data->Z, + m+1, + model->V, + K-1, + 0.0, + ZV, + K-1); + + // Calculate the distance to each of the vertices of the simplex. + // The closest vertex defines the class label. + for (i=0; i<n; i++) { + label = 0; + min_dist = 1000000000.0; + for (j=0; j<K; j++) { + for (k=0; k<K-1; k++) { + S[k] = matrix_get(ZV, K-1, i, k) - matrix_get(U, K-1, j, k); + } + norm = cblas_dnrm2(K, S, 1); + if (norm < min_dist) { + label = j+1; // labels start counting from 1 + min_dist = norm; + } + } + predy[i] = label; + } + + free(ZV); + free(U); + free(S); +} + +/** + * @name msvmmaj_prediction_perf + * @brief Calculate the predictive performance (percentage correct) + * + * The predictive performance is calculated by simply counting the number + * of correctly classified samples and dividing by the total number of + * samples, multiplying by 100. + * + * @param [in] data the dataset with known labels + * @param [in] predy the predicted class labels + * + * @returns percentage correctly classified. + */ +double msvmmaj_prediction_perf(struct MajData *data, long *predy) +{ + long i, correct = 0; + double performance; + + for (i=0; i<data->n; i++) + if (data->y[i] == predy[i]) + correct++; + + performance = ((double) correct)/((double) data->n)* 100.0; + + return performance; +} |
