GenSVM
test_gensvm_predict.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_predict.h"
29 
48 {
49  int n = 12;
50  int m = 2;
51  int K = 3;
52 
53  struct GenData *data = gensvm_init_data();
54  struct GenModel *model = gensvm_init_model();
55 
56  model->n = n;
57  model->m = m;
58  model->K = K;
59 
60  data->n = n;
61  data->m = m;
62  data->r = m;
63  data->K = K;
64 
65  data->Z = Calloc(double, n*(m+1));
66  data->y = Calloc(long, n);
67 
68  matrix_set(data->Z, m+1, 0, 0, 1.0000000000000000);
69  matrix_set(data->Z, m+1, 0, 1, -0.3943375672974065);
70  matrix_set(data->Z, m+1, 0, 2, -0.1056624327025935);
71  matrix_set(data->Z, m+1, 1, 0, 1.0000000000000000);
72  matrix_set(data->Z, m+1, 1, 1, -0.2886751345948129);
73  matrix_set(data->Z, m+1, 1, 2, -0.2886751345948128);
74  matrix_set(data->Z, m+1, 2, 0, 1.0000000000000000);
75  matrix_set(data->Z, m+1, 2, 1, -0.1056624327025937);
76  matrix_set(data->Z, m+1, 2, 2, -0.3943375672974063);
77  matrix_set(data->Z, m+1, 3, 0, 1.0000000000000000);
78  matrix_set(data->Z, m+1, 3, 1, 0.1056624327025935);
79  matrix_set(data->Z, m+1, 3, 2, -0.3943375672974064);
80  matrix_set(data->Z, m+1, 4, 0, 1.0000000000000000);
81  matrix_set(data->Z, m+1, 4, 1, 0.2886751345948129);
82  matrix_set(data->Z, m+1, 4, 2, -0.2886751345948129);
83  matrix_set(data->Z, m+1, 5, 0, 1.0000000000000000);
84  matrix_set(data->Z, m+1, 5, 1, 0.3943375672974064);
85  matrix_set(data->Z, m+1, 5, 2, -0.1056624327025937);
86  matrix_set(data->Z, m+1, 6, 0, 1.0000000000000000);
87  matrix_set(data->Z, m+1, 6, 1, 0.3943375672974065);
88  matrix_set(data->Z, m+1, 6, 2, 0.1056624327025935);
89  matrix_set(data->Z, m+1, 7, 0, 1.0000000000000000);
90  matrix_set(data->Z, m+1, 7, 1, 0.2886751345948130);
91  matrix_set(data->Z, m+1, 7, 2, 0.2886751345948128);
92  matrix_set(data->Z, m+1, 8, 0, 1.0000000000000000);
93  matrix_set(data->Z, m+1, 8, 1, 0.1056624327025939);
94  matrix_set(data->Z, m+1, 8, 2, 0.3943375672974063);
95  matrix_set(data->Z, m+1, 9, 0, 1.0000000000000000);
96  matrix_set(data->Z, m+1, 9, 1, -0.1056624327025934);
97  matrix_set(data->Z, m+1, 9, 2, 0.3943375672974064);
98  matrix_set(data->Z, m+1, 10, 0, 1.0000000000000000);
99  matrix_set(data->Z, m+1, 10, 1, -0.2886751345948126);
100  matrix_set(data->Z, m+1, 10, 2, 0.2886751345948132);
101  matrix_set(data->Z, m+1, 11, 0, 1.0000000000000000);
102  matrix_set(data->Z, m+1, 11, 1, -0.3943375672974064);
103  matrix_set(data->Z, m+1, 11, 2, 0.1056624327025939);
104 
105  gensvm_allocate_model(model);
106 
107  matrix_set(model->V, K-1, 0, 0, 0.0000000000000000);
108  matrix_set(model->V, K-1, 0, 1, 0.0000000000000000);
109  matrix_set(model->V, K-1, 1, 0, -2.4494897427831779);
110  matrix_set(model->V, K-1, 1, 1, -0.0000000000000002);
111  matrix_set(model->V, K-1, 2, 0, 0.0000000000000000);
112  matrix_set(model->V, K-1, 2, 1, -2.4494897427831783);
113 
114  // start test code
115  long *predy = Calloc(long, n);
116  gensvm_predict_labels(data, model, predy);
117 
118  mu_assert(predy[0] == 2, "Incorrect label at index 0");
119  mu_assert(predy[1] == 3, "Incorrect label at index 1");
120  mu_assert(predy[2] == 3, "Incorrect label at index 2");
121  mu_assert(predy[3] == 3, "Incorrect label at index 3");
122  mu_assert(predy[4] == 3, "Incorrect label at index 4");
123  mu_assert(predy[5] == 1, "Incorrect label at index 5");
124  mu_assert(predy[6] == 1, "Incorrect label at index 6");
125  mu_assert(predy[7] == 1, "Incorrect label at index 7");
126  mu_assert(predy[8] == 1, "Incorrect label at index 8");
127  mu_assert(predy[9] == 2, "Incorrect label at index 9");
128  mu_assert(predy[10] == 2, "Incorrect label at index 10");
129  mu_assert(predy[11] == 2, "Incorrect label at index 11");
130 
131  // end test code
132  gensvm_free_data(data);
133  gensvm_free_model(model);
134  free(predy);
135 
136  return NULL;
137 }
138 
140 {
141  int n = 12;
142  int m = 2;
143  int K = 3;
144 
145  struct GenData *data = gensvm_init_data();
146  struct GenModel *model = gensvm_init_model();
147 
148  model->n = n;
149  model->m = m;
150  model->K = K;
151 
152  data->n = n;
153  data->m = m;
154  data->r = m;
155  data->K = K;
156 
157  data->Z = Calloc(double, n*(m+1));
158  data->y = Calloc(long, n);
159 
160  matrix_set(data->Z, m+1, 0, 0, 1.0000000000000000);
161  matrix_set(data->Z, m+1, 0, 1, -0.3943375672974065);
162  matrix_set(data->Z, m+1, 0, 2, -0.1056624327025935);
163  matrix_set(data->Z, m+1, 1, 0, 1.0000000000000000);
164  matrix_set(data->Z, m+1, 1, 1, -0.2886751345948129);
165  matrix_set(data->Z, m+1, 1, 2, -0.2886751345948128);
166  matrix_set(data->Z, m+1, 2, 0, 1.0000000000000000);
167  matrix_set(data->Z, m+1, 2, 1, -0.1056624327025937);
168  matrix_set(data->Z, m+1, 2, 2, -0.3943375672974063);
169  matrix_set(data->Z, m+1, 3, 0, 1.0000000000000000);
170  matrix_set(data->Z, m+1, 3, 1, 0.1056624327025935);
171  matrix_set(data->Z, m+1, 3, 2, -0.3943375672974064);
172  matrix_set(data->Z, m+1, 4, 0, 1.0000000000000000);
173  matrix_set(data->Z, m+1, 4, 1, 0.2886751345948129);
174  matrix_set(data->Z, m+1, 4, 2, -0.2886751345948129);
175  matrix_set(data->Z, m+1, 5, 0, 1.0000000000000000);
176  matrix_set(data->Z, m+1, 5, 1, 0.3943375672974064);
177  matrix_set(data->Z, m+1, 5, 2, -0.1056624327025937);
178  matrix_set(data->Z, m+1, 6, 0, 1.0000000000000000);
179  matrix_set(data->Z, m+1, 6, 1, 0.3943375672974065);
180  matrix_set(data->Z, m+1, 6, 2, 0.1056624327025935);
181  matrix_set(data->Z, m+1, 7, 0, 1.0000000000000000);
182  matrix_set(data->Z, m+1, 7, 1, 0.2886751345948130);
183  matrix_set(data->Z, m+1, 7, 2, 0.2886751345948128);
184  matrix_set(data->Z, m+1, 8, 0, 1.0000000000000000);
185  matrix_set(data->Z, m+1, 8, 1, 0.1056624327025939);
186  matrix_set(data->Z, m+1, 8, 2, 0.3943375672974063);
187  matrix_set(data->Z, m+1, 9, 0, 1.0000000000000000);
188  matrix_set(data->Z, m+1, 9, 1, -0.1056624327025934);
189  matrix_set(data->Z, m+1, 9, 2, 0.3943375672974064);
190  matrix_set(data->Z, m+1, 10, 0, 1.0000000000000000);
191  matrix_set(data->Z, m+1, 10, 1, -0.2886751345948126);
192  matrix_set(data->Z, m+1, 10, 2, 0.2886751345948132);
193  matrix_set(data->Z, m+1, 11, 0, 1.0000000000000000);
194  matrix_set(data->Z, m+1, 11, 1, -0.3943375672974064);
195  matrix_set(data->Z, m+1, 11, 2, 0.1056624327025939);
196 
197  // convert Z to a sparse matrix to test the sparse functions
198  data->spZ = gensvm_dense_to_sparse(data->Z, data->n, data->m+1);
199  free(data->Z);
200  data->RAW = NULL;
201  data->Z = NULL;
202 
203  gensvm_allocate_model(model);
204 
205  matrix_set(model->V, K-1, 0, 0, 0.0000000000000000);
206  matrix_set(model->V, K-1, 0, 1, 0.0000000000000000);
207  matrix_set(model->V, K-1, 1, 0, -2.4494897427831779);
208  matrix_set(model->V, K-1, 1, 1, -0.0000000000000002);
209  matrix_set(model->V, K-1, 2, 0, 0.0000000000000000);
210  matrix_set(model->V, K-1, 2, 1, -2.4494897427831783);
211 
212  // start test code
213  long *predy = Calloc(long, n);
214  gensvm_predict_labels(data, model, predy);
215 
216  mu_assert(predy[0] == 2, "Incorrect label at index 0");
217  mu_assert(predy[1] == 3, "Incorrect label at index 1");
218  mu_assert(predy[2] == 3, "Incorrect label at index 2");
219  mu_assert(predy[3] == 3, "Incorrect label at index 3");
220  mu_assert(predy[4] == 3, "Incorrect label at index 4");
221  mu_assert(predy[5] == 1, "Incorrect label at index 5");
222  mu_assert(predy[6] == 1, "Incorrect label at index 6");
223  mu_assert(predy[7] == 1, "Incorrect label at index 7");
224  mu_assert(predy[8] == 1, "Incorrect label at index 8");
225  mu_assert(predy[9] == 2, "Incorrect label at index 9");
226  mu_assert(predy[10] == 2, "Incorrect label at index 10");
227  mu_assert(predy[11] == 2, "Incorrect label at index 11");
228 
229  // end test code
230  gensvm_free_data(data);
231  gensvm_free_model(model);
232  free(predy);
233 
234  return NULL;
235 }
236 
238 {
239  int i, n = 8;
240  struct GenData *data = gensvm_init_data();
241  data->n = n;
242  data->y = Calloc(long, n);
243  data->y[0] = 1;
244  data->y[1] = 1;
245  data->y[2] = 1;
246  data->y[3] = 1;
247  data->y[4] = 2;
248  data->y[5] = 2;
249  data->y[6] = 2;
250  data->y[7] = 3;
251 
252  long *y = Calloc(long, n);
253  for (i=0; i<n; i++)
254  y[i] = 1;
255  mu_assert(gensvm_prediction_perf(data, y) == 50.0,
256  "Incorrect first time.");
257 
258  for (i=0; i<n; i++)
259  y[i] = 2;
260  mu_assert(gensvm_prediction_perf(data, y) == 37.5,
261  "Incorrect second time.");
262 
263  for (i=0; i<n; i++)
264  y[i] = 3;
265  mu_assert(gensvm_prediction_perf(data, y) == 12.5,
266  "Incorrect third time.");
267 
268  free(y);
269  gensvm_free_data(data);
270 
271  return NULL;
272 }
273 
274 char *all_tests()
275 {
276  mu_suite_start();
280 
281  return NULL;
282 }
283 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
Header file for gensvm_predict.c.
#define mu_assert(test, message)
Definition: minunit.h:29
long K
number of classes
Definition: gensvm_base.h:58
char * test_gensvm_predict_labels_dense()
double gensvm_prediction_perf(struct GenData *data, long *perdy)
Calculate the predictive performance (percentage correct)
double * Z
Definition: gensvm_base.h:68
void gensvm_free_model(struct GenModel *model)
Free allocated GenModel struct.
Definition: gensvm_base.c:211
char * test_gensvm_prediction_perf()
double * V
augmented weight matrix
Definition: gensvm_base.h:115
RUN_TESTS(all_tests)
#define mu_run_test(test)
Definition: minunit.h:35
long * y
array of class labels, 1..K
Definition: gensvm_base.h:66
struct GenModel * gensvm_init_model(void)
Initialize a GenModel structure.
Definition: gensvm_base.c:102
A structure to represent the data.
Definition: gensvm_base.h:57
A structure to represent a single GenSVM model.
Definition: gensvm_base.h:92
void gensvm_predict_labels(struct GenData *testdata, struct GenModel *model, long *predy)
Predict class labels of data given and output in predy.
long n
number of instances in the dataset
Definition: gensvm_base.h:97
char * all_tests()
void gensvm_free_data(struct GenData *data)
Free allocated GenData struct.
Definition: gensvm_base.c:73
void gensvm_allocate_model(struct GenModel *model)
Allocate memory for a GenModel.
Definition: gensvm_base.c:144
long r
number of eigenvalues (width of Z)
Definition: gensvm_base.h:64
long K
number of classes in the dataset
Definition: gensvm_base.h:95
char * test_gensvm_predict_labels_sparse()
long m
number of predictors (width of RAW)
Definition: gensvm_base.h:62
struct GenSparse * gensvm_dense_to_sparse(double *A, long rows, long cols)
Convert a dense matrix to a GenSparse structure if advantageous.
#define matrix_set(M, cols, i, j, val)
long n
number of instances
Definition: gensvm_base.h:60
struct GenData * gensvm_init_data(void)
Initialize a GenData structure.
Definition: gensvm_base.c:45
long m
number of predictor variables in the dataset
Definition: gensvm_base.h:99
#define mu_suite_start()
Definition: minunit.h:24
double * RAW
augmented raw data matrix
Definition: gensvm_base.h:73
struct GenSparse * spZ
sparse representation of the augmented data matrix
Definition: gensvm_base.h:71