Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. #include "libsvm.h"
  7. #include "main.h"
  8. #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) // struct svm_node *x = (struct svm_node *) malloc((n+1)*sizeof(struct svm_node));
  9.  
  10. struct svm_parameter param; // set by parse_command_line
  11. struct svm_problem prob; // set by read_problem
  12. struct svm_model *model;
  13. struct svm_node *x_space;
  14. struct svm_node ** x;
  15. struct svm_node *testnode;
  16.  
  17. void makeModel2(void) // int main(int argc, char **argv)
  18. {
  19. //char input_file_name[1024];
  20. //char model_file_name[1024];
  21. //const char *error_msg;
  22.  
  23. param.svm_type = C_SVC;
  24. param.kernel_type = RBF;
  25. param.degree = 3;
  26. param.gamma = 0.5;
  27. param.coef0 = 0;
  28. param.nu = 0.5;
  29. param.cache_size = 100;
  30. param.C = 1;
  31. param.eps = 1e-3;
  32. param.p = 0.1;
  33. param.shrinking = 1;
  34. param.probability = 0;
  35. param.nr_weight = 0;
  36. param.weight_label = NULL;
  37. param.weight = NULL;
  38.  
  39.  
  40. //Problem definition-------------------------------------------------------------
  41. prob.l = 4;
  42.  
  43. //x values matrix of xor values
  44. double matrix[prob.l][2];
  45. matrix[0][0] = 1;
  46. matrix[0][1] = 1;
  47.  
  48. matrix[1][0] = 1;
  49. matrix[1][1] = 0;
  50.  
  51. matrix[2][0] = 0;
  52. matrix[2][1] = 1;
  53.  
  54. matrix[3][0] = 0;
  55. matrix[3][1] = 0;
  56.  
  57. //This part i have trouble understanding
  58. struct svm_node** x = (struct svm_node * *)malloc((prob.l)*sizeof(struct svm_node *)); // svm_node** x = Malloc(svm_node*,prob.l); // (x *)malloc((prob.l)*sizeof(prob.l)) // x = Malloc(x,prob.l);
  59.  
  60. //Trying to assign from matrix to svm_node training examples
  61. for (int row = 0;row <prob.l; row++){
  62. x_space = Malloc(x_space,3); // svm_node* x_space = Malloc(svm_node,3); // x_space = Malloc(x_space,3); // struct svm_node *x_space = (struct svm_node *) malloc((3)*sizeof(struct svm_node));
  63. for (int col = 0;col < 2;col++){
  64. x_space[col].index = col;
  65. x_space[col].value = matrix[row][col];
  66. }
  67. x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
  68. x[row] = x_space;
  69. }
  70.  
  71. prob.x = x;
  72.  
  73. //yvalues
  74. prob.y = (double *)malloc((prob.l)*sizeof(double)); // prob.y = Malloc(double,prob.l); // prob.y = Malloc(prob.y,prob.l);
  75. prob.y[0] = -1;
  76. prob.y[1] = 1;
  77. prob.y[2] = 1;
  78. prob.y[3] = -1;
  79.  
  80. //Train model---------------------------------------------------------------------
  81. model = svm_train(&prob,&param); // svm_model *model = svm_train(&prob,&param);
  82.  
  83.  
  84. //Test model----------------------------------------------------------------------
  85. struct svm_node *testnode = (struct svm_node *) malloc((3)*sizeof(struct svm_node)); // svm_node* testnode = Malloc(svm_node,3); // testnode = Malloc(testnode,3); // testnode = Malloc(testnode,3);
  86. testnode[0].index = 0;
  87. testnode[0].value = 1;
  88. testnode[1].index = 1;
  89. testnode[1].value = 0;
  90. testnode[2].index = -1;
  91.  
  92. //This works correctly:
  93. retval[0] = svm_predict(model,testnode); // double retval = svm_predict(model,testnode);
  94. //printf("retval: %fn",retval);
  95.  
  96.  
  97. svm_destroy_param(&param);
  98. free(prob.y);
  99. free(prob.x);
  100. free(x_space);
  101.  
  102. //return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement