svm.h 3.5 KB

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