GenSVM
gensvm_cross_validation.c
Go to the documentation of this file.
1 
28 
29 extern FILE *GENSVM_OUTPUT_FILE;
30 
52 double gensvm_cross_validation(struct GenModel *model,
53  struct GenData **train_folds, struct GenData **test_folds,
54  long folds, long n_total)
55 {
56  long f;
57  long *predy = NULL;
58  double performance, total_perf = 0;
59 
60  // make sure that gensvm_optimize() is silent.
61  FILE *fid = GENSVM_OUTPUT_FILE;
62  GENSVM_OUTPUT_FILE = NULL;
63 
64  // run cross-validation
65  for (f=0; f<folds; f++) {
66  // reallocate model in case dimensions differ with data
67  gensvm_reallocate_model(model, train_folds[f]->n,
68  train_folds[f]->r);
69 
70  // initialize object weights
71  gensvm_initialize_weights(train_folds[f], model);
72 
73  // train the model (surpressing output)
74  gensvm_optimize(model, train_folds[f]);
75 
76  // calculate prediction performance on test set
77  predy = Calloc(long, test_folds[f]->n);
78  gensvm_predict_labels(test_folds[f], model, predy);
79  performance = gensvm_prediction_perf(test_folds[f], predy);
80  total_perf += performance * test_folds[f]->n;
81 
82  free(predy);
83  }
84 
85  total_perf /= ((double) n_total);
86 
87  // reset the output stream
88  GENSVM_OUTPUT_FILE = fid;
89 
90  return total_perf;
91 }
#define Calloc(type, size)
Definition: gensvm_memory.h:40
void gensvm_optimize(struct GenModel *model, struct GenData *data)
The main training loop for GenSVM.
double gensvm_prediction_perf(struct GenData *data, long *perdy)
Calculate the predictive performance (percentage correct)
A structure to represent the data.
Definition: gensvm_base.h:57
void gensvm_initialize_weights(struct GenData *data, struct GenModel *model)
Initialize instance weights.
Definition: gensvm_init.c:152
A structure to represent a single GenSVM model.
Definition: gensvm_base.h:92
FILE * GENSVM_OUTPUT_FILE
Definition: gensvm_print.c:33
void gensvm_predict_labels(struct GenData *testdata, struct GenModel *model, long *predy)
Predict class labels of data given and output in predy.
void gensvm_reallocate_model(struct GenModel *model, long n, long m)
Reallocate memory for GenModel.
Definition: gensvm_base.c:172
long n
number of instances
Definition: gensvm_base.h:60
Header file for gensvm_cross_validation.c.
double gensvm_cross_validation(struct GenModel *model, struct GenData **train_folds, struct GenData **test_folds, long folds, long n_total)
Run cross validation with a given set of train/test folds.