parameter.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # This file is part of svm_toolkit.
  2. #
  3. # Author:: Peter Lane
  4. # Copyright:: Copyright 2011-13, Peter Lane.
  5. # License:: MIT License
  6. #
  7. module SvmToolkit
  8. #
  9. # Parameter holds values determining the kernel type
  10. # and training process.
  11. #
  12. class Parameter
  13. # :attr_accessor: svm_type
  14. # The type of SVM problem being solved.
  15. # * C_SVC, the usual classification task.
  16. # * NU_SVC
  17. # * ONE_CLASS
  18. # * EPSILON_SVR
  19. # * NU_SVR
  20. # :attr_accessor: kernel_type
  21. # The type of kernel to use.
  22. # * LINEAR
  23. # * POLY
  24. # * RBF
  25. # * SIGMOID
  26. # * PRECOMPUTED
  27. # :attr_writer: degree
  28. # A parameter in polynomial kernels.
  29. # :attr_accessor: gamma
  30. # A parameter in poly/rbf/sigmoid kernels.
  31. # :attr_accessor: coef0
  32. # A parameter for poly/sigmoid kernels.
  33. # :attr_accessor: cache_size
  34. # For training, in MB.
  35. # :attr_accessor: eps
  36. # For training, stopping criterion.
  37. # :attr_accessor: C
  38. # For training with C_SVC, EPSILON_SVR, NU_SVR: the cost parameter.
  39. # :attr_accessor: nr_weight
  40. # For training with C_SVC.
  41. # :attr_accessor: weight_label
  42. # For training with C_SVC.
  43. # :attr_accessor: weight
  44. # For training with C_SVC.
  45. # :attr_accessor: nu
  46. # For training with NU_SVR, ONE_CLASS, NU_SVC.
  47. # :attr_accessor: p
  48. # For training with EPSILON_SVR.
  49. # :attr_accessor: shrinking
  50. # For training, whether to use shrinking heuristics.
  51. # :attr_accessor: probability
  52. # For training, whether to use probability estimates.
  53. # Constructor sets up values of attributes based on provided map.
  54. # Valid keys with their default values:
  55. # * :svm_type = Parameter::C_SVC, for the type of SVM
  56. # * :kernel_type = Parameter::LINEAR, for the type of kernel
  57. # * :cost = 1.0, for the cost or C parameter
  58. # * :gamma = 0.0, for the gamma parameter in kernel
  59. # * :degree = 1, for polynomial kernel
  60. # * :coef0 = 0.0, for polynomial/sigmoid kernels
  61. # * :eps = 0.001, for stopping criterion
  62. # * :nr_weight = 0, for C_SVC
  63. # * :nu = 0.5, used for NU_SVC, ONE_CLASS and NU_SVR. Nu must be in (0,1]
  64. # * :p = 0.1, used for EPSILON_SVR
  65. # * :shrinking = 1, use the shrinking heuristics
  66. # * :probability = 0, use the probability estimates
  67. def initialize args
  68. super()
  69. self.svm_type = args.fetch(:svm_type, Parameter::C_SVC)
  70. self.kernel_type = args.fetch(:kernel_type, Parameter::LINEAR)
  71. self.C = args.fetch(:cost, 1.0)
  72. self.gamma = args.fetch(:gamma, 0.0)
  73. self.degree = args.fetch(:degree, 1)
  74. self.coef0 = args.fetch(:coef0, 0.0)
  75. self.eps = args.fetch(:eps, 0.001)
  76. self.nr_weight = args.fetch(:nr_weight, 0)
  77. self.nu = args.fetch(:nu, 0.5)
  78. self.p = args.fetch(:p, 0.1)
  79. self.shrinking = args.fetch(:shrinking, 1)
  80. self.probability = args.fetch(:probability, 0)
  81. unless self.nu > 0.0 and self.nu <= 1.0
  82. raise ArgumentError "Invalid value of nu #{self.nu}, should be in (0,1]"
  83. end
  84. end
  85. # A more readable accessor for the C parameter
  86. def cost
  87. self.C
  88. end
  89. # A more readable mutator for the C parameter
  90. def cost= val
  91. self.C = val
  92. end
  93. # Return a list of the available kernels.
  94. def self.kernels
  95. [Parameter::LINEAR, Parameter::POLY, Parameter::RBF, Parameter::SIGMOID]
  96. end
  97. # Return a printable name for the given kernel.
  98. def self.kernel_name kernel
  99. case kernel
  100. when Parameter::LINEAR
  101. "Linear"
  102. when Parameter::POLY
  103. "Polynomial"
  104. when Parameter::RBF
  105. "Radial basis function"
  106. when Parameter::SIGMOID
  107. "Sigmoid"
  108. else
  109. "Unknown"
  110. end
  111. end
  112. end
  113. end