1 | /*
|
---|
2 | * SVM.NET Library
|
---|
3 | * Copyright (C) 2008 Matthew Johnson
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | using System;
|
---|
21 | using System.Linq;
|
---|
22 | using System.Collections.Generic;
|
---|
23 |
|
---|
24 | namespace SVM
|
---|
25 | {
|
---|
26 | /// <summary>
|
---|
27 | /// Contains all of the types of SVM this library can model.
|
---|
28 | /// </summary>
|
---|
29 | public enum SvmType {
|
---|
30 | /// <summary>
|
---|
31 | /// C-SVC.
|
---|
32 | /// </summary>
|
---|
33 | C_SVC,
|
---|
34 | /// <summary>
|
---|
35 | /// nu-SVC.
|
---|
36 | /// </summary>
|
---|
37 | NU_SVC,
|
---|
38 | /// <summary>
|
---|
39 | /// one-class SVM
|
---|
40 | /// </summary>
|
---|
41 | ONE_CLASS,
|
---|
42 | /// <summary>
|
---|
43 | /// epsilon-SVR
|
---|
44 | /// </summary>
|
---|
45 | EPSILON_SVR,
|
---|
46 | /// <summary>
|
---|
47 | /// nu-SVR
|
---|
48 | /// </summary>
|
---|
49 | NU_SVR
|
---|
50 | };
|
---|
51 | /// <summary>
|
---|
52 | /// Contains the various kernel types this library can use.
|
---|
53 | /// </summary>
|
---|
54 | public enum KernelType {
|
---|
55 | /// <summary>
|
---|
56 | /// Linear: u'*v
|
---|
57 | /// </summary>
|
---|
58 | LINEAR,
|
---|
59 | /// <summary>
|
---|
60 | /// Polynomial: (gamma*u'*v + coef0)^degree
|
---|
61 | /// </summary>
|
---|
62 | POLY,
|
---|
63 | /// <summary>
|
---|
64 | /// Radial basis function: exp(-gamma*|u-v|^2)
|
---|
65 | /// </summary>
|
---|
66 | RBF,
|
---|
67 | /// <summary>
|
---|
68 | /// Sigmoid: tanh(gamma*u'*v + coef0)
|
---|
69 | /// </summary>
|
---|
70 | SIGMOID,
|
---|
71 | /// <summary>
|
---|
72 | /// Precomputed kernel
|
---|
73 | /// </summary>
|
---|
74 | PRECOMPUTED,
|
---|
75 | };
|
---|
76 |
|
---|
77 | /// <summary>
|
---|
78 | /// This class contains the various parameters which can affect the way in which an SVM
|
---|
79 | /// is learned. Unless you know what you are doing, chances are you are best off using
|
---|
80 | /// the default values.
|
---|
81 | /// </summary>
|
---|
82 | [Serializable]
|
---|
83 | public class Parameter : ICloneable
|
---|
84 | {
|
---|
85 | private SvmType _svmType;
|
---|
86 | private KernelType _kernelType;
|
---|
87 | private int _degree;
|
---|
88 | private double _gamma;
|
---|
89 | private double _coef0;
|
---|
90 |
|
---|
91 | private double _cacheSize;
|
---|
92 | private double _C;
|
---|
93 | private double _eps;
|
---|
94 |
|
---|
95 | private Dictionary<int, double> _weights;
|
---|
96 | private double _nu;
|
---|
97 | private double _p;
|
---|
98 | private bool _shrinking;
|
---|
99 | private bool _probability;
|
---|
100 |
|
---|
101 | /// <summary>
|
---|
102 | /// Default Constructor. Gives good default values to all parameters.
|
---|
103 | /// </summary>
|
---|
104 | public Parameter()
|
---|
105 | {
|
---|
106 | _svmType = SvmType.C_SVC;
|
---|
107 | _kernelType = KernelType.RBF;
|
---|
108 | _degree = 3;
|
---|
109 | _gamma = 0; // 1/k
|
---|
110 | _coef0 = 0;
|
---|
111 | _nu = 0.5;
|
---|
112 | _cacheSize = 40;
|
---|
113 | _C = 1;
|
---|
114 | _eps = 1e-3;
|
---|
115 | _p = 0.1;
|
---|
116 | _shrinking = true;
|
---|
117 | _probability = false;
|
---|
118 | _weights = new Dictionary<int, double>();
|
---|
119 | }
|
---|
120 |
|
---|
121 | /// <summary>
|
---|
122 | /// Type of SVM (default C-SVC)
|
---|
123 | /// </summary>
|
---|
124 | public SvmType SvmType
|
---|
125 | {
|
---|
126 | get
|
---|
127 | {
|
---|
128 | return _svmType;
|
---|
129 | }
|
---|
130 | set
|
---|
131 | {
|
---|
132 | _svmType = value;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | /// <summary>
|
---|
136 | /// Type of kernel function (default Polynomial)
|
---|
137 | /// </summary>
|
---|
138 | public KernelType KernelType
|
---|
139 | {
|
---|
140 | get
|
---|
141 | {
|
---|
142 | return _kernelType;
|
---|
143 | }
|
---|
144 | set
|
---|
145 | {
|
---|
146 | _kernelType = value;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | /// <summary>
|
---|
150 | /// Degree in kernel function (default 3).
|
---|
151 | /// </summary>
|
---|
152 | public int Degree
|
---|
153 | {
|
---|
154 | get
|
---|
155 | {
|
---|
156 | return _degree;
|
---|
157 | }
|
---|
158 | set
|
---|
159 | {
|
---|
160 | _degree = value;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | /// <summary>
|
---|
164 | /// Gamma in kernel function (default 1/k)
|
---|
165 | /// </summary>
|
---|
166 | public double Gamma
|
---|
167 | {
|
---|
168 | get
|
---|
169 | {
|
---|
170 | return _gamma;
|
---|
171 | }
|
---|
172 | set
|
---|
173 | {
|
---|
174 | _gamma = value;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | /// <summary>
|
---|
178 | /// Zeroeth coefficient in kernel function (default 0)
|
---|
179 | /// </summary>
|
---|
180 | public double Coefficient0
|
---|
181 | {
|
---|
182 | get
|
---|
183 | {
|
---|
184 | return _coef0;
|
---|
185 | }
|
---|
186 | set
|
---|
187 | {
|
---|
188 | _coef0 = value;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /// <summary>
|
---|
193 | /// Cache memory size in MB (default 100)
|
---|
194 | /// </summary>
|
---|
195 | public double CacheSize
|
---|
196 | {
|
---|
197 | get
|
---|
198 | {
|
---|
199 | return _cacheSize;
|
---|
200 | }
|
---|
201 | set
|
---|
202 | {
|
---|
203 | _cacheSize = value;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | /// <summary>
|
---|
207 | /// Tolerance of termination criterion (default 0.001)
|
---|
208 | /// </summary>
|
---|
209 | public double EPS
|
---|
210 | {
|
---|
211 | get
|
---|
212 | {
|
---|
213 | return _eps;
|
---|
214 | }
|
---|
215 | set
|
---|
216 | {
|
---|
217 | _eps = value;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | /// <summary>
|
---|
221 | /// The parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
|
---|
222 | /// </summary>
|
---|
223 | public double C
|
---|
224 | {
|
---|
225 | get
|
---|
226 | {
|
---|
227 | return _C;
|
---|
228 | }
|
---|
229 | set
|
---|
230 | {
|
---|
231 | _C = value;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | /// <summary>
|
---|
236 | /// Contains custom weights for class labels. Default weight value is 1.
|
---|
237 | /// </summary>
|
---|
238 | public Dictionary<int,double> Weights
|
---|
239 | {
|
---|
240 | get{
|
---|
241 | return _weights;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | /// <summary>
|
---|
246 | /// The parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
|
---|
247 | /// </summary>
|
---|
248 | public double Nu
|
---|
249 | {
|
---|
250 | get
|
---|
251 | {
|
---|
252 | return _nu;
|
---|
253 | }
|
---|
254 | set
|
---|
255 | {
|
---|
256 | _nu = value;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | /// <summary>
|
---|
260 | /// The epsilon in loss function of epsilon-SVR (default 0.1)
|
---|
261 | /// </summary>
|
---|
262 | public double P
|
---|
263 | {
|
---|
264 | get
|
---|
265 | {
|
---|
266 | return _p;
|
---|
267 | }
|
---|
268 | set
|
---|
269 | {
|
---|
270 | _p = value;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | /// <summary>
|
---|
274 | /// Whether to use the shrinking heuristics, (default True)
|
---|
275 | /// </summary>
|
---|
276 | public bool Shrinking
|
---|
277 | {
|
---|
278 | get
|
---|
279 | {
|
---|
280 | return _shrinking;
|
---|
281 | }
|
---|
282 | set
|
---|
283 | {
|
---|
284 | _shrinking = value;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | /// <summary>
|
---|
288 | /// Whether to train an SVC or SVR model for probability estimates, (default False)
|
---|
289 | /// </summary>
|
---|
290 | public bool Probability
|
---|
291 | {
|
---|
292 | get
|
---|
293 | {
|
---|
294 | return _probability;
|
---|
295 | }
|
---|
296 | set
|
---|
297 | {
|
---|
298 | _probability = value;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | #region ICloneable Members
|
---|
304 | /// <summary>
|
---|
305 | /// Creates a memberwise clone of this parameters object.
|
---|
306 | /// </summary>
|
---|
307 | /// <returns>The clone (as type Parameter)</returns>
|
---|
308 | public object Clone()
|
---|
309 | {
|
---|
310 | return base.MemberwiseClone();
|
---|
311 | }
|
---|
312 |
|
---|
313 | #endregion
|
---|
314 | }
|
---|
315 | } |
---|