libsvm.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module is a low level wrapper for `libsvm`:idx:.
  10. {.deadCodeElim: on.}
  11. const
  12. LIBSVM_VERSION* = 312
  13. when defined(windows):
  14. const svmdll* = "libsvm.dll"
  15. elif defined(macosx):
  16. const svmdll* = "libsvm.dylib"
  17. else:
  18. const svmdll* = "libsvm.so"
  19. type
  20. Node*{.pure, final.} = object
  21. index*: cint
  22. value*: cdouble
  23. Problem*{.pure, final.} = object
  24. L*: cint
  25. y*: ptr cdouble
  26. x*: ptr ptr Node
  27. Type*{.size: sizeof(cint).} = enum
  28. C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR
  29. KernelType*{.size: sizeof(cint).} = enum
  30. LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED
  31. Parameter*{.pure, final.} = object
  32. typ*: Type
  33. kernelType*: KernelType
  34. degree*: cint # for poly
  35. gamma*: cdouble # for poly/rbf/sigmoid
  36. coef0*: cdouble # for poly/sigmoid
  37. # these are for training only
  38. cache_size*: cdouble # in MB
  39. eps*: cdouble # stopping criteria
  40. C*: cdouble # for C_SVC, EPSILON_SVR and NU_SVR
  41. nr_weight*: cint # for C_SVC
  42. weight_label*: ptr cint # for C_SVC
  43. weight*: ptr cdouble # for C_SVC
  44. nu*: cdouble # for NU_SVC, ONE_CLASS, and NU_SVR
  45. p*: cdouble # for EPSILON_SVR
  46. shrinking*: cint # use the shrinking heuristics
  47. probability*: cint # do probability estimates
  48. {.deprecated: [Tnode: Node, Tproblem: Problem, Ttype: Type,
  49. TKernelType: KernelType, Tparameter: Parameter].}
  50. #
  51. # svm_model
  52. #
  53. type
  54. Model*{.pure, final.} = object
  55. param*: Parameter # parameter
  56. nr_class*: cint # number of classes, = 2 in regression/one class svm
  57. L*: cint # total #SV
  58. SV*: ptr ptr Node # SVs (SV[l])
  59. sv_coef*: ptr ptr cdouble # coefficients for SVs in decision functions (sv_coef[k-1][l])
  60. rho*: ptr cdouble # constants in decision functions (rho[k*(k-1)/2])
  61. probA*: ptr cdouble # pariwise probability information
  62. probB*: ptr cdouble # for classification only
  63. label*: ptr cint # label of each class (label[k])
  64. nSV*: ptr cint # number of SVs for each class (nSV[k])
  65. # nSV[0] + nSV[1] + ... + nSV[k-1] = l
  66. # XXX
  67. free_sv*: cint # 1 if svm_model is created by svm_load_model
  68. # 0 if svm_model is created by svm_train
  69. {.deprecated: [TModel: Model].}
  70. proc train*(prob: ptr Problem, param: ptr Parameter): ptr Model{.cdecl,
  71. importc: "svm_train", dynlib: svmdll.}
  72. proc cross_validation*(prob: ptr Problem, param: ptr Parameter, nr_fold: cint,
  73. target: ptr cdouble){.cdecl,
  74. importc: "svm_cross_validation", dynlib: svmdll.}
  75. proc save_model*(model_file_name: cstring, model: ptr Model): cint{.cdecl,
  76. importc: "svm_save_model", dynlib: svmdll.}
  77. proc load_model*(model_file_name: cstring): ptr Model{.cdecl,
  78. importc: "svm_load_model", dynlib: svmdll.}
  79. proc get_svm_type*(model: ptr Model): cint{.cdecl, importc: "svm_get_svm_type",
  80. dynlib: svmdll.}
  81. proc get_nr_class*(model: ptr Model): cint{.cdecl, importc: "svm_get_nr_class",
  82. dynlib: svmdll.}
  83. proc get_labels*(model: ptr Model, label: ptr cint){.cdecl,
  84. importc: "svm_get_labels", dynlib: svmdll.}
  85. proc get_svr_probability*(model: ptr Model): cdouble{.cdecl,
  86. importc: "svm_get_svr_probability", dynlib: svmdll.}
  87. proc predict_values*(model: ptr Model, x: ptr Node, dec_values: ptr cdouble): cdouble{.
  88. cdecl, importc: "svm_predict_values", dynlib: svmdll.}
  89. proc predict*(model: ptr Model, x: ptr Node): cdouble{.cdecl,
  90. importc: "svm_predict", dynlib: svmdll.}
  91. proc predict_probability*(model: ptr Model, x: ptr Node,
  92. prob_estimates: ptr cdouble): cdouble{.cdecl,
  93. importc: "svm_predict_probability", dynlib: svmdll.}
  94. proc free_model_content*(model_ptr: ptr Model){.cdecl,
  95. importc: "svm_free_model_content", dynlib: svmdll.}
  96. proc free_and_destroy_model*(model_ptr_ptr: ptr ptr Model){.cdecl,
  97. importc: "svm_free_and_destroy_model", dynlib: svmdll.}
  98. proc destroy_param*(param: ptr Parameter){.cdecl, importc: "svm_destroy_param",
  99. dynlib: svmdll.}
  100. proc check_parameter*(prob: ptr Problem, param: ptr Parameter): cstring{.
  101. cdecl, importc: "svm_check_parameter", dynlib: svmdll.}
  102. proc check_probability_model*(model: ptr Model): cint{.cdecl,
  103. importc: "svm_check_probability_model", dynlib: svmdll.}
  104. proc set_print_string_function*(print_func: proc (arg: cstring) {.cdecl.}){.
  105. cdecl, importc: "svm_set_print_string_function", dynlib: svmdll.}