You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README 29 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. Libsvm is a simple, easy-to-use, and efficient software for SVM
  2. classification and regression. It solves C-SVM classification, nu-SVM
  3. classification, one-class-SVM, epsilon-SVM regression, and nu-SVM
  4. regression. It also provides an automatic model selection tool for
  5. C-SVM classification. This document explains the use of libsvm.
  6. Libsvm is available at
  7. http://www.csie.ntu.edu.tw/~cjlin/libsvm
  8. Please read the COPYRIGHT file before using libsvm.
  9. Table of Contents
  10. =================
  11. - Quick Start
  12. - Installation and Data Format
  13. - `svm-train' Usage
  14. - `svm-predict' Usage
  15. - `svm-scale' Usage
  16. - Tips on Practical Use
  17. - Examples
  18. - Precomputed Kernels
  19. - Library Usage
  20. - Java Version
  21. - Building Windows Binaries
  22. - Additional Tools: Sub-sampling, Parameter Selection, Format checking, etc.
  23. - MATLAB/OCTAVE Interface
  24. - Python Interface
  25. - Additional Information
  26. Quick Start
  27. ===========
  28. If you are new to SVM and if the data is not large, please go to
  29. `tools' directory and use easy.py after installation. It does
  30. everything automatic -- from data scaling to parameter selection.
  31. Usage: easy.py training_file [testing_file]
  32. More information about parameter selection can be found in
  33. `tools/README.'
  34. Installation and Data Format
  35. ============================
  36. On Unix systems, type `make' to build the `svm-train' and `svm-predict'
  37. programs. Run them without arguments to show the usages of them.
  38. On other systems, consult `Makefile' to build them (e.g., see
  39. 'Building Windows binaries' in this file) or use the pre-built
  40. binaries (Windows binaries are in the directory `windows').
  41. The format of training and testing data file is:
  42. <label> <index1>:<value1> <index2>:<value2> ...
  43. .
  44. .
  45. .
  46. Each line contains an instance and is ended by a '\n' character. For
  47. classification, <label> is an integer indicating the class label
  48. (multi-class is supported). For regression, <label> is the target
  49. value which can be any real number. For one-class SVM, it's not used
  50. so can be any number. The pair <index>:<value> gives a feature
  51. (attribute) value: <index> is an integer starting from 1 and <value>
  52. is a real number. The only exception is the precomputed kernel, where
  53. <index> starts from 0; see the section of precomputed kernels. Indices
  54. must be in ASCENDING order. Labels in the testing file are only used
  55. to calculate accuracy or errors. If they are unknown, just fill the
  56. first column with any numbers.
  57. A sample classification data included in this package is
  58. `heart_scale'. To check if your data is in a correct form, use
  59. `tools/checkdata.py' (details in `tools/README').
  60. Type `svm-train heart_scale', and the program will read the training
  61. data and output the model file `heart_scale.model'. If you have a test
  62. set called heart_scale.t, then type `svm-predict heart_scale.t
  63. heart_scale.model output' to see the prediction accuracy. The `output'
  64. file contains the predicted class labels.
  65. For classification, if training data are in only one class (i.e., all
  66. labels are the same), then `svm-train' issues a warning message:
  67. `Warning: training data in only one class. See README for details,'
  68. which means the training data is very unbalanced. The label in the
  69. training data is directly returned when testing.
  70. There are some other useful programs in this package.
  71. svm-scale:
  72. This is a tool for scaling input data file.
  73. svm-toy:
  74. This is a simple graphical interface which shows how SVM
  75. separate data in a plane. You can click in the window to
  76. draw data points. Use "change" button to choose class
  77. 1, 2 or 3 (i.e., up to three classes are supported), "load"
  78. button to load data from a file, "save" button to save data to
  79. a file, "run" button to obtain an SVM model, and "clear"
  80. button to clear the window.
  81. You can enter options in the bottom of the window, the syntax of
  82. options is the same as `svm-train'.
  83. Note that "load" and "save" consider dense data format both in
  84. classification and the regression cases. For classification,
  85. each data point has one label (the color) that must be 1, 2,
  86. or 3 and two attributes (x-axis and y-axis values) in
  87. [0,1). For regression, each data point has one target value
  88. (y-axis) and one attribute (x-axis values) in [0, 1).
  89. Type `make' in respective directories to build them.
  90. You need Qt library to build the Qt version.
  91. (available from http://www.trolltech.com)
  92. You need GTK+ library to build the GTK version.
  93. (available from http://www.gtk.org)
  94. The pre-built Windows binaries are in the `windows'
  95. directory. We use Visual C++ on a 64-bit machine.
  96. `svm-train' Usage
  97. =================
  98. Usage: svm-train [options] training_set_file [model_file]
  99. options:
  100. -s svm_type : set type of SVM (default 0)
  101. 0 -- C-SVC (multi-class classification)
  102. 1 -- nu-SVC (multi-class classification)
  103. 2 -- one-class SVM
  104. 3 -- epsilon-SVR (regression)
  105. 4 -- nu-SVR (regression)
  106. -t kernel_type : set type of kernel function (default 2)
  107. 0 -- linear: u'*v
  108. 1 -- polynomial: (gamma*u'*v + coef0)^degree
  109. 2 -- radial basis function: exp(-gamma*|u-v|^2)
  110. 3 -- sigmoid: tanh(gamma*u'*v + coef0)
  111. 4 -- precomputed kernel (kernel values in training_set_file)
  112. -d degree : set degree in kernel function (default 3)
  113. -g gamma : set gamma in kernel function (default 1/num_features)
  114. -r coef0 : set coef0 in kernel function (default 0)
  115. -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
  116. -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
  117. -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
  118. -m cachesize : set cache memory size in MB (default 100)
  119. -e epsilon : set tolerance of termination criterion (default 0.001)
  120. -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
  121. -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
  122. -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
  123. -v n: n-fold cross validation mode
  124. -q : quiet mode (no outputs)
  125. The k in the -g option means the number of attributes in the input data.
  126. option -v randomly splits the data into n parts and calculates cross
  127. validation accuracy/mean squared error on them.
  128. See libsvm FAQ for the meaning of outputs.
  129. `svm-predict' Usage
  130. ===================
  131. Usage: svm-predict [options] test_file model_file output_file
  132. options:
  133. -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); for one-class SVM only 0 is supported
  134. model_file is the model file generated by svm-train.
  135. test_file is the test data you want to predict.
  136. svm-predict will produce output in the output_file.
  137. `svm-scale' Usage
  138. =================
  139. Usage: svm-scale [options] data_filename
  140. options:
  141. -l lower : x scaling lower limit (default -1)
  142. -u upper : x scaling upper limit (default +1)
  143. -y y_lower y_upper : y scaling limits (default: no y scaling)
  144. -s save_filename : save scaling parameters to save_filename
  145. -r restore_filename : restore scaling parameters from restore_filename
  146. See 'Examples' in this file for examples.
  147. Tips on Practical Use
  148. =====================
  149. * Scale your data. For example, scale each attribute to [0,1] or [-1,+1].
  150. * For C-SVC, consider using the model selection tool in the tools directory.
  151. * nu in nu-SVC/one-class-SVM/nu-SVR approximates the fraction of training
  152. errors and support vectors.
  153. * If data for classification are unbalanced (e.g. many positive and
  154. few negative), try different penalty parameters C by -wi (see
  155. examples below).
  156. * Specify larger cache size (i.e., larger -m) for huge problems.
  157. Examples
  158. ========
  159. > svm-scale -l -1 -u 1 -s range train > train.scale
  160. > svm-scale -r range test > test.scale
  161. Scale each feature of the training data to be in [-1,1]. Scaling
  162. factors are stored in the file range and then used for scaling the
  163. test data.
  164. > svm-train -s 0 -c 5 -t 2 -g 0.5 -e 0.1 data_file
  165. Train a classifier with RBF kernel exp(-0.5|u-v|^2), C=10, and
  166. stopping tolerance 0.1.
  167. > svm-train -s 3 -p 0.1 -t 0 data_file
  168. Solve SVM regression with linear kernel u'v and epsilon=0.1
  169. in the loss function.
  170. > svm-train -c 10 -w1 1 -w-2 5 -w4 2 data_file
  171. Train a classifier with penalty 10 = 1 * 10 for class 1, penalty 50 =
  172. 5 * 10 for class -2, and penalty 20 = 2 * 10 for class 4.
  173. > svm-train -s 0 -c 100 -g 0.1 -v 5 data_file
  174. Do five-fold cross validation for the classifier using
  175. the parameters C = 100 and gamma = 0.1
  176. > svm-train -s 0 -b 1 data_file
  177. > svm-predict -b 1 test_file data_file.model output_file
  178. Obtain a model with probability information and predict test data with
  179. probability estimates
  180. Precomputed Kernels
  181. ===================
  182. Users may precompute kernel values and input them as training and
  183. testing files. Then libsvm does not need the original
  184. training/testing sets.
  185. Assume there are L training instances x1, ..., xL and.
  186. Let K(x, y) be the kernel
  187. value of two instances x and y. The input formats
  188. are:
  189. New training instance for xi:
  190. <label> 0:i 1:K(xi,x1) ... L:K(xi,xL)
  191. New testing instance for any x:
  192. <label> 0:? 1:K(x,x1) ... L:K(x,xL)
  193. That is, in the training file the first column must be the "ID" of
  194. xi. In testing, ? can be any value.
  195. All kernel values including ZEROs must be explicitly provided. Any
  196. permutation or random subsets of the training/testing files are also
  197. valid (see examples below).
  198. Note: the format is slightly different from the precomputed kernel
  199. package released in libsvmtools earlier.
  200. Examples:
  201. Assume the original training data has three four-feature
  202. instances and testing data has one instance:
  203. 15 1:1 2:1 3:1 4:1
  204. 45 2:3 4:3
  205. 25 3:1
  206. 15 1:1 3:1
  207. If the linear kernel is used, we have the following new
  208. training/testing sets:
  209. 15 0:1 1:4 2:6 3:1
  210. 45 0:2 1:6 2:18 3:0
  211. 25 0:3 1:1 2:0 3:1
  212. 15 0:? 1:2 2:0 3:1
  213. ? can be any value.
  214. Any subset of the above training file is also valid. For example,
  215. 25 0:3 1:1 2:0 3:1
  216. 45 0:2 1:6 2:18 3:0
  217. implies that the kernel matrix is
  218. [K(2,2) K(2,3)] = [18 0]
  219. [K(3,2) K(3,3)] = [0 1]
  220. Library Usage
  221. =============
  222. These functions and structures are declared in the header file
  223. `svm.h'. You need to #include "svm.h" in your C/C++ source files and
  224. link your program with `svm.cpp'. You can see `svm-train.c' and
  225. `svm-predict.c' for examples showing how to use them. We define
  226. LIBSVM_VERSION and declare `extern int libsvm_version; ' in svm.h, so
  227. you can check the version number.
  228. Before you classify test data, you need to construct an SVM model
  229. (`svm_model') using training data. A model can also be saved in
  230. a file for later use. Once an SVM model is available, you can use it
  231. to classify new data.
  232. - Function: struct svm_model *svm_train(const struct svm_problem *prob,
  233. const struct svm_parameter *param);
  234. This function constructs and returns an SVM model according to
  235. the given training data and parameters.
  236. struct svm_problem describes the problem:
  237. struct svm_problem
  238. {
  239. int l;
  240. double *y;
  241. struct svm_node **x;
  242. };
  243. where `l' is the number of training data, and `y' is an array containing
  244. their target values. (integers in classification, real numbers in
  245. regression) `x' is an array of pointers, each of which points to a sparse
  246. representation (array of svm_node) of one training vector.
  247. For example, if we have the following training data:
  248. LABEL ATTR1 ATTR2 ATTR3 ATTR4 ATTR5
  249. ----- ----- ----- ----- ----- -----
  250. 1 0 0.1 0.2 0 0
  251. 2 0 0.1 0.3 -1.2 0
  252. 1 0.4 0 0 0 0
  253. 2 0 0.1 0 1.4 0.5
  254. 3 -0.1 -0.2 0.1 1.1 0.1
  255. then the components of svm_problem are:
  256. l = 5
  257. y -> 1 2 1 2 3
  258. x -> [ ] -> (2,0.1) (3,0.2) (-1,?)
  259. [ ] -> (2,0.1) (3,0.3) (4,-1.2) (-1,?)
  260. [ ] -> (1,0.4) (-1,?)
  261. [ ] -> (2,0.1) (4,1.4) (5,0.5) (-1,?)
  262. [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (-1,?)
  263. where (index,value) is stored in the structure `svm_node':
  264. struct svm_node
  265. {
  266. int index;
  267. double value;
  268. };
  269. index = -1 indicates the end of one vector. Note that indices must
  270. be in ASCENDING order.
  271. struct svm_parameter describes the parameters of an SVM model:
  272. struct svm_parameter
  273. {
  274. int svm_type;
  275. int kernel_type;
  276. int degree; /* for poly */
  277. double gamma; /* for poly/rbf/sigmoid */
  278. double coef0; /* for poly/sigmoid */
  279. /* these are for training only */
  280. double cache_size; /* in MB */
  281. double eps; /* stopping criteria */
  282. double C; /* for C_SVC, EPSILON_SVR, and NU_SVR */
  283. int nr_weight; /* for C_SVC */
  284. int *weight_label; /* for C_SVC */
  285. double* weight; /* for C_SVC */
  286. double nu; /* for NU_SVC, ONE_CLASS, and NU_SVR */
  287. double p; /* for EPSILON_SVR */
  288. int shrinking; /* use the shrinking heuristics */
  289. int probability; /* do probability estimates */
  290. };
  291. svm_type can be one of C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR.
  292. C_SVC: C-SVM classification
  293. NU_SVC: nu-SVM classification
  294. ONE_CLASS: one-class-SVM
  295. EPSILON_SVR: epsilon-SVM regression
  296. NU_SVR: nu-SVM regression
  297. kernel_type can be one of LINEAR, POLY, RBF, SIGMOID.
  298. LINEAR: u'*v
  299. POLY: (gamma*u'*v + coef0)^degree
  300. RBF: exp(-gamma*|u-v|^2)
  301. SIGMOID: tanh(gamma*u'*v + coef0)
  302. PRECOMPUTED: kernel values in training_set_file
  303. cache_size is the size of the kernel cache, specified in megabytes.
  304. C is the cost of constraints violation.
  305. eps is the stopping criterion. (we usually use 0.00001 in nu-SVC,
  306. 0.001 in others). nu is the parameter in nu-SVM, nu-SVR, and
  307. one-class-SVM. p is the epsilon in epsilon-insensitive loss function
  308. of epsilon-SVM regression. shrinking = 1 means shrinking is conducted;
  309. = 0 otherwise. probability = 1 means model with probability
  310. information is obtained; = 0 otherwise.
  311. nr_weight, weight_label, and weight are used to change the penalty
  312. for some classes (If the weight for a class is not changed, it is
  313. set to 1). This is useful for training classifier using unbalanced
  314. input data or with asymmetric misclassification cost.
  315. nr_weight is the number of elements in the array weight_label and
  316. weight. Each weight[i] corresponds to weight_label[i], meaning that
  317. the penalty of class weight_label[i] is scaled by a factor of weight[i].
  318. If you do not want to change penalty for any of the classes,
  319. just set nr_weight to 0.
  320. *NOTE* Because svm_model contains pointers to svm_problem, you can
  321. not free the memory used by svm_problem if you are still using the
  322. svm_model produced by svm_train().
  323. *NOTE* To avoid wrong parameters, svm_check_parameter() should be
  324. called before svm_train().
  325. struct svm_model stores the model obtained from the training procedure.
  326. It is not recommended to directly access entries in this structure.
  327. Programmers should use the interface functions to get the values.
  328. struct svm_model
  329. {
  330. struct svm_parameter param; /* parameter */
  331. int nr_class; /* number of classes, = 2 in regression/one class svm */
  332. int l; /* total #SV */
  333. struct svm_node **SV; /* SVs (SV[l]) */
  334. double **sv_coef; /* coefficients for SVs in decision functions (sv_coef[k-1][l]) */
  335. double *rho; /* constants in decision functions (rho[k*(k-1)/2]) */
  336. double *probA; /* pairwise probability information */
  337. double *probB;
  338. int *sv_indices; /* sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to indicate SVs in the training set */
  339. /* for classification only */
  340. int *label; /* label of each class (label[k]) */
  341. int *nSV; /* number of SVs for each class (nSV[k]) */
  342. /* nSV[0] + nSV[1] + ... + nSV[k-1] = l */
  343. /* XXX */
  344. int free_sv; /* 1 if svm_model is created by svm_load_model*/
  345. /* 0 if svm_model is created by svm_train */
  346. };
  347. param describes the parameters used to obtain the model.
  348. nr_class is the number of classes. It is 2 for regression and one-class SVM.
  349. l is the number of support vectors. SV and sv_coef are support
  350. vectors and the corresponding coefficients, respectively. Assume there are
  351. k classes. For data in class j, the corresponding sv_coef includes (k-1) y*alpha vectors,
  352. where alpha's are solutions of the following two class problems:
  353. 1 vs j, 2 vs j, ..., j-1 vs j, j vs j+1, j vs j+2, ..., j vs k
  354. and y=1 for the first j-1 vectors, while y=-1 for the remaining k-j
  355. vectors. For example, if there are 4 classes, sv_coef and SV are like:
  356. +-+-+-+--------------------+
  357. |1|1|1| |
  358. |v|v|v| SVs from class 1 |
  359. |2|3|4| |
  360. +-+-+-+--------------------+
  361. |1|2|2| |
  362. |v|v|v| SVs from class 2 |
  363. |2|3|4| |
  364. +-+-+-+--------------------+
  365. |1|2|3| |
  366. |v|v|v| SVs from class 3 |
  367. |3|3|4| |
  368. +-+-+-+--------------------+
  369. |1|2|3| |
  370. |v|v|v| SVs from class 4 |
  371. |4|4|4| |
  372. +-+-+-+--------------------+
  373. See svm_train() for an example of assigning values to sv_coef.
  374. rho is the bias term (-b). probA and probB are parameters used in
  375. probability outputs. If there are k classes, there are k*(k-1)/2
  376. binary problems as well as rho, probA, and probB values. They are
  377. aligned in the order of binary problems:
  378. 1 vs 2, 1 vs 3, ..., 1 vs k, 2 vs 3, ..., 2 vs k, ..., k-1 vs k.
  379. sv_indices[0,...,nSV-1] are values in [1,...,num_traning_data] to
  380. indicate support vectors in the training set.
  381. label contains labels in the training data.
  382. nSV is the number of support vectors in each class.
  383. free_sv is a flag used to determine whether the space of SV should
  384. be released in free_model_content(struct svm_model*) and
  385. free_and_destroy_model(struct svm_model**). If the model is
  386. generated by svm_train(), then SV points to data in svm_problem
  387. and should not be removed. For example, free_sv is 0 if svm_model
  388. is created by svm_train, but is 1 if created by svm_load_model.
  389. - Function: double svm_predict(const struct svm_model *model,
  390. const struct svm_node *x);
  391. This function does classification or regression on a test vector x
  392. given a model.
  393. For a classification model, the predicted class for x is returned.
  394. For a regression model, the function value of x calculated using
  395. the model is returned. For an one-class model, +1 or -1 is
  396. returned.
  397. - Function: void svm_cross_validation(const struct svm_problem *prob,
  398. const struct svm_parameter *param, int nr_fold, double *target);
  399. This function conducts cross validation. Data are separated to
  400. nr_fold folds. Under given parameters, sequentially each fold is
  401. validated using the model from training the remaining. Predicted
  402. labels (of all prob's instances) in the validation process are
  403. stored in the array called target.
  404. The format of svm_prob is same as that for svm_train().
  405. - Function: int svm_get_svm_type(const struct svm_model *model);
  406. This function gives svm_type of the model. Possible values of
  407. svm_type are defined in svm.h.
  408. - Function: int svm_get_nr_class(const svm_model *model);
  409. For a classification model, this function gives the number of
  410. classes. For a regression or an one-class model, 2 is returned.
  411. - Function: void svm_get_labels(const svm_model *model, int* label)
  412. For a classification model, this function outputs the name of
  413. labels into an array called label. For regression and one-class
  414. models, label is unchanged.
  415. - Function: void svm_get_sv_indices(const struct svm_model *model, int *sv_indices)
  416. This function outputs indices of support vectors into an array called sv_indices.
  417. The size of sv_indices is the number of support vectors and can be obtained by calling svm_get_nr_sv.
  418. Each sv_indices[i] is in the range of [1, ..., num_traning_data].
  419. - Function: int svm_get_nr_sv(const struct svm_model *model)
  420. This function gives the number of total support vector.
  421. - Function: double svm_get_svr_probability(const struct svm_model *model);
  422. For a regression model with probability information, this function
  423. outputs a value sigma > 0. For test data, we consider the
  424. probability model: target value = predicted value + z, z: Laplace
  425. distribution e^(-|z|/sigma)/(2sigma)
  426. If the model is not for svr or does not contain required
  427. information, 0 is returned.
  428. - Function: double svm_predict_values(const svm_model *model,
  429. const svm_node *x, double* dec_values)
  430. This function gives decision values on a test vector x given a
  431. model, and return the predicted label (classification) or
  432. the function value (regression).
  433. For a classification model with nr_class classes, this function
  434. gives nr_class*(nr_class-1)/2 decision values in the array
  435. dec_values, where nr_class can be obtained from the function
  436. svm_get_nr_class. The order is label[0] vs. label[1], ...,
  437. label[0] vs. label[nr_class-1], label[1] vs. label[2], ...,
  438. label[nr_class-2] vs. label[nr_class-1], where label can be
  439. obtained from the function svm_get_labels. The returned value is
  440. the predicted class for x. Note that when nr_class = 1, this
  441. function does not give any decision value.
  442. For a regression model, dec_values[0] and the returned value are
  443. both the function value of x calculated using the model. For a
  444. one-class model, dec_values[0] is the decision value of x, while
  445. the returned value is +1/-1.
  446. - Function: double svm_predict_probability(const struct svm_model *model,
  447. const struct svm_node *x, double* prob_estimates);
  448. This function does classification or regression on a test vector x
  449. given a model with probability information.
  450. For a classification model with probability information, this
  451. function gives nr_class probability estimates in the array
  452. prob_estimates. nr_class can be obtained from the function
  453. svm_get_nr_class. The class with the highest probability is
  454. returned. For regression/one-class SVM, the array prob_estimates
  455. is unchanged and the returned value is the same as that of
  456. svm_predict.
  457. - Function: const char *svm_check_parameter(const struct svm_problem *prob,
  458. const struct svm_parameter *param);
  459. This function checks whether the parameters are within the feasible
  460. range of the problem. This function should be called before calling
  461. svm_train() and svm_cross_validation(). It returns NULL if the
  462. parameters are feasible, otherwise an error message is returned.
  463. - Function: int svm_check_probability_model(const struct svm_model *model);
  464. This function checks whether the model contains required
  465. information to do probability estimates. If so, it returns
  466. +1. Otherwise, 0 is returned. This function should be called
  467. before calling svm_get_svr_probability and
  468. svm_predict_probability.
  469. - Function: int svm_save_model(const char *model_file_name,
  470. const struct svm_model *model);
  471. This function saves a model to a file; returns 0 on success, or -1
  472. if an error occurs.
  473. - Function: struct svm_model *svm_load_model(const char *model_file_name);
  474. This function returns a pointer to the model read from the file,
  475. or a null pointer if the model could not be loaded.
  476. - Function: void svm_free_model_content(struct svm_model *model_ptr);
  477. This function frees the memory used by the entries in a model structure.
  478. - Function: void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr);
  479. This function frees the memory used by a model and destroys the model
  480. structure. It is equivalent to svm_destroy_model, which
  481. is deprecated after version 3.0.
  482. - Function: void svm_destroy_param(struct svm_parameter *param);
  483. This function frees the memory used by a parameter set.
  484. - Function: void svm_set_print_string_function(void (*print_func)(const char *));
  485. Users can specify their output format by a function. Use
  486. svm_set_print_string_function(NULL);
  487. for default printing to stdout.
  488. Java Version
  489. ============
  490. The pre-compiled java class archive `libsvm.jar' and its source files are
  491. in the java directory. To run the programs, use
  492. java -classpath libsvm.jar svm_train <arguments>
  493. java -classpath libsvm.jar svm_predict <arguments>
  494. java -classpath libsvm.jar svm_toy
  495. java -classpath libsvm.jar svm_scale <arguments>
  496. Note that you need Java 1.5 (5.0) or above to run it.
  497. You may need to add Java runtime library (like classes.zip) to the classpath.
  498. You may need to increase maximum Java heap size.
  499. Library usages are similar to the C version. These functions are available:
  500. public class svm {
  501. public static final int LIBSVM_VERSION=322;
  502. public static svm_model svm_train(svm_problem prob, svm_parameter param);
  503. public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target);
  504. public static int svm_get_svm_type(svm_model model);
  505. public static int svm_get_nr_class(svm_model model);
  506. public static void svm_get_labels(svm_model model, int[] label);
  507. public static void svm_get_sv_indices(svm_model model, int[] indices);
  508. public static int svm_get_nr_sv(svm_model model);
  509. public static double svm_get_svr_probability(svm_model model);
  510. public static double svm_predict_values(svm_model model, svm_node[] x, double[] dec_values);
  511. public static double svm_predict(svm_model model, svm_node[] x);
  512. public static double svm_predict_probability(svm_model model, svm_node[] x, double[] prob_estimates);
  513. public static void svm_save_model(String model_file_name, svm_model model) throws IOException
  514. public static svm_model svm_load_model(String model_file_name) throws IOException
  515. public static String svm_check_parameter(svm_problem prob, svm_parameter param);
  516. public static int svm_check_probability_model(svm_model model);
  517. public static void svm_set_print_string_function(svm_print_interface print_func);
  518. }
  519. The library is in the "libsvm" package.
  520. Note that in Java version, svm_node[] is not ended with a node whose index = -1.
  521. Users can specify their output format by
  522. your_print_func = new svm_print_interface()
  523. {
  524. public void print(String s)
  525. {
  526. // your own format
  527. }
  528. };
  529. svm.svm_set_print_string_function(your_print_func);
  530. Building Windows Binaries
  531. =========================
  532. Windows binaries are available in the directory `windows'. To re-build
  533. them via Visual C++, use the following steps:
  534. 1. Open a DOS command box (or Visual Studio Command Prompt) and change
  535. to libsvm directory. If environment variables of VC++ have not been
  536. set, type
  537. ""C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\vcvars64.bat""
  538. You may have to modify the above command according which version of
  539. VC++ or where it is installed.
  540. 2. Type
  541. nmake -f Makefile.win clean all
  542. 3. (optional) To build shared library libsvm.dll, type
  543. nmake -f Makefile.win lib
  544. 4. (optional) To build 32-bit windows binaries, you must
  545. (1) Setup "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat" instead of vcvars64.bat
  546. (2) Change CFLAGS in Makefile.win: /D _WIN64 to /D _WIN32
  547. Another way is to build them from Visual C++ environment. See details
  548. in libsvm FAQ.
  549. - Additional Tools: Sub-sampling, Parameter Selection, Format checking, etc.
  550. ============================================================================
  551. See the README file in the tools directory.
  552. MATLAB/OCTAVE Interface
  553. =======================
  554. Please check the file README in the directory `matlab'.
  555. Python Interface
  556. ================
  557. See the README file in python directory.
  558. Additional Information
  559. ======================
  560. If you find LIBSVM helpful, please cite it as
  561. Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support
  562. vector machines. ACM Transactions on Intelligent Systems and
  563. Technology, 2:27:1--27:27, 2011. Software available at
  564. http://www.csie.ntu.edu.tw/~cjlin/libsvm
  565. LIBSVM implementation document is available at
  566. http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf
  567. For any questions and comments, please email cjlin@csie.ntu.edu.tw
  568. Acknowledgments:
  569. This work was supported in part by the National Science
  570. Council of Taiwan via the grant NSC 89-2213-E-002-013.
  571. The authors thank their group members and users
  572. for many helpful discussions and comments. They are listed in
  573. http://www.csie.ntu.edu.tw/~cjlin/libsvm/acknowledgements

A Python package for graph kernels, graph edit distances and graph pre-image problem.