main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //#include "svm/svm.h"
  2. //#include "svm/data.h"
  3. //#include <stdio.h>
  4. //#include <stdlib.h>
  5. //#include <math.h>
  6. //
  7. //void svm_train_and_predict() {
  8. // struct svm_problem prob{};
  9. // struct svm_parameter param{};
  10. // struct svm_model *model;
  11. // struct svm_node *x_space;
  12. //
  13. // // 设置SVM参数
  14. // param.svm_type = ONE_CLASS;
  15. // param.kernel_type = RBF; // 使用径向基函数核
  16. // param.nu = 0.1; // nu参数,用于控制支持向量的数量
  17. // param.gamma = 0.1; // nu参数,用于控制支持向量的数量
  18. //
  19. // // 初始化问题
  20. // prob.l = HaveCount;
  21. // prob.y = (double *) malloc(prob.l * sizeof(double));
  22. // prob.x = (struct svm_node **) malloc(prob.l * sizeof(struct svm_node *));
  23. // x_space = (struct svm_node *) malloc((Features + 1) * prob.l * sizeof(struct svm_node));
  24. //
  25. // // 设置训练数据
  26. // for (int i = 0; i < prob.l; ++i) {
  27. // prob.x[i] = &x_space[(Features + 1) * i];
  28. // for (int j = 0; j < Features; ++j) {
  29. // prob.x[i][j].index = j + 1;
  30. // prob.x[i][j].value = Have[i][j];
  31. // }
  32. // prob.x[i][Features].index = -1;
  33. // prob.y[i] = 1; // 所有点标记为1,因为这是一个单分类问题
  34. // }
  35. //
  36. // // 训练模型
  37. // model = svm_train(&prob, &param);
  38. // int res = svm_save_model("test.model", model);
  39. // printf("save res: %d\n", res);
  40. //
  41. //
  42. // // 预测
  43. // for (int i = 0; i < TestCount; ++i) {
  44. // struct svm_node test[Features + 1];
  45. // for (int j = 0; j < Features; ++j) {
  46. // test[j].index = j + 1;
  47. // test[j].value = Test[i][j];
  48. // }
  49. // test[Features].index = -1;
  50. //
  51. // double predicted_label = svm_predict(model, test);
  52. // printf("Predicted Label = %.0lf\n", predicted_label);
  53. // }
  54. //
  55. // // 释放资源
  56. // free(prob.y);
  57. // free(prob.x);
  58. // free(x_space);
  59. // svm_free_and_destroy_model(&model);
  60. //}
  61. //
  62. //void load_and_test() {
  63. // svm_model *model = svm_load_model("test.model");
  64. //
  65. // // 预测 have
  66. // int success = 0;
  67. // for (auto &line : Have) {
  68. // struct svm_node node[Features + 1];
  69. // for (int j = 0; j < Features; ++j) {
  70. // node[j].index = j + 1;
  71. // node[j].value = line[j];
  72. // }
  73. // node[Features].index = -1;
  74. //
  75. // if (svm_predict(model, node) > 0) success++;
  76. // }
  77. // printf("Accuracy Have: [%d/%d]\n", success, HaveCount);
  78. //
  79. // // 预测 test
  80. // success = 0;
  81. // for (auto &line : Test) {
  82. // struct svm_node node[Features + 1];
  83. // for (int j = 0; j < Features; ++j) {
  84. // node[j].index = j + 1;
  85. // node[j].value = line[j];
  86. // }
  87. // node[Features].index = -1;
  88. //
  89. // if (svm_predict(model, node) > 0) success++;
  90. // }
  91. // printf("Accuracy Test: [%d/%d]\n", success, TestCount);
  92. //
  93. // // 预测 none
  94. // success = 0;
  95. // for (auto &line : None) {
  96. // struct svm_node node[Features + 1];
  97. // for (int j = 0; j < Features; ++j) {
  98. // node[j].index = j + 1;
  99. // node[j].value = line[j];
  100. // }
  101. // node[Features].index = -1;
  102. //
  103. // if (svm_predict(model, node) < 0) success++;
  104. // }
  105. // printf("Accuracy None: [%d/%d]\n", success, NoneCount);
  106. //}
  107. #include "my-svm/svm.h"
  108. #include "stdio.h"
  109. int main() {
  110. svm_init();
  111. Node *x = input();
  112. int8_t res = svm_predict(x);
  113. printf("result: %d\n", res);
  114. return 0;
  115. }