svm.h 2.8 KB

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