You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svm.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #define LIBSVM_VERSION 324
  3. extern "C" {
  4. extern int libsvm_version;
  5. struct svm_node
  6. {
  7. int index;
  8. double value;
  9. };
  10. struct svm_problem
  11. {
  12. int l;
  13. double* y;
  14. struct svm_node** x;
  15. };
  16. enum { C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR }; // svm_type
  17. enum { LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED }; // kernel_type
  18. /* Table help you to transforms Enums to strings */
  19. //extern static const char *svm_type_table[5];
  20. //extern static const char *kernel_type_table[5];
  21. inline const char* get_svm_type(const unsigned int code)
  22. {
  23. static const char* svm_type_table[] = { "c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr", nullptr };
  24. return svm_type_table[code];
  25. }
  26. inline const char* get_kernel_type(const unsigned int code)
  27. {
  28. static const char* types[] = { "linear", "polynomial", "rbf", "sigmoid", "precomputed", nullptr };
  29. return types[code];
  30. }
  31. struct svm_parameter
  32. {
  33. int svm_type;
  34. int kernel_type;
  35. int degree; // for poly
  36. double gamma; // for poly/rbf/sigmoid
  37. double coef0; // for poly/sigmoid
  38. // these are for training only
  39. double cache_size; // in MB
  40. double eps; // stopping criteria
  41. double C; // for C_SVC, EPSILON_SVR and NU_SVR
  42. int nr_weight; // for C_SVC
  43. int* weight_label; // for C_SVC
  44. double* weight; // for C_SVC
  45. double nu; // for NU_SVC, ONE_CLASS, and NU_SVR
  46. double p; // for EPSILON_SVR
  47. int shrinking; // use the shrinking heuristics
  48. int probability; // do probability estimates
  49. };
  50. //
  51. // svm_model
  52. //
  53. struct svm_model
  54. {
  55. struct svm_parameter param; // parameter
  56. int nr_class; // number of classes, = 2 in regression/one class svm
  57. int l; // total #SV
  58. struct svm_node** SV; // SVs (SV[l])
  59. double** sv_coef; // coefficients for SVs in decision functions (sv_coef[k-1][l])
  60. double* rho; // constants in decision functions (rho[k*(k-1)/2])
  61. double* probA; // pariwise probability information
  62. double* probB;
  63. int* sv_indices; // sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set
  64. // for classification only
  65. int* label; // label of each class (label[k])
  66. int* nSV; // number of SVs for each class (nSV[k])
  67. // nSV[0] + nSV[1] + ... + nSV[k-1] = l
  68. // XXX
  69. int free_sv; // 1 if svm_model is created by svm_load_model
  70. // 0 if svm_model is created by svm_train
  71. };
  72. struct svm_model* svm_train(const struct svm_problem* prob, const struct svm_parameter* param);
  73. void svm_cross_validation(const struct svm_problem* prob, const struct svm_parameter* param, int nr_fold, double* target);
  74. int svm_save_model(const char* model_file_name, const struct svm_model* model);
  75. struct svm_model* svm_load_model(const char* model_file_name);
  76. int svm_get_svm_type(const struct svm_model* model);
  77. int svm_get_nr_class(const struct svm_model* model);
  78. void svm_get_labels(const struct svm_model* model, int* label);
  79. void svm_get_sv_indices(const struct svm_model* model, int* indices);
  80. int svm_get_nr_sv(const struct svm_model* model);
  81. double svm_get_svr_probability(const struct svm_model* model);
  82. double svm_predict_values(const struct svm_model* model, const struct svm_node* x, double* dec_values);
  83. double svm_predict(const struct svm_model* model, const struct svm_node* x);
  84. double svm_predict_probability(const struct svm_model* model, const struct svm_node* x, double* prob_estimates);
  85. void svm_free_model_content(struct svm_model* model_ptr);
  86. void svm_free_and_destroy_model(struct svm_model** model_ptr_ptr);
  87. void svm_destroy_param(struct svm_parameter* param);
  88. const char* svm_check_parameter(const struct svm_problem* prob, const struct svm_parameter* param);
  89. int svm_check_probability_model(const struct svm_model* model);
  90. void svm_set_print_string_function(void (*print_func)(const char*));
  91. }