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 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. This directory includes some useful codes:
  2. 1. subset selection tools.
  3. 2. parameter selection tools.
  4. 3. LIBSVM format checking tools
  5. Part I: Subset selection tools
  6. Introduction
  7. ============
  8. Training large data is time consuming. Sometimes one should work on a
  9. smaller subset first. The python script subset.py randomly selects a
  10. specified number of samples. For classification data, we provide a
  11. stratified selection to ensure the same class distribution in the
  12. subset.
  13. Usage: subset.py [options] dataset number [output1] [output2]
  14. This script selects a subset of the given data set.
  15. options:
  16. -s method : method of selection (default 0)
  17. 0 -- stratified selection (classification only)
  18. 1 -- random selection
  19. output1 : the subset (optional)
  20. output2 : the rest of data (optional)
  21. If output1 is omitted, the subset will be printed on the screen.
  22. Example
  23. =======
  24. > python subset.py heart_scale 100 file1 file2
  25. From heart_scale 100 samples are randomly selected and stored in
  26. file1. All remaining instances are stored in file2.
  27. Part II: Parameter Selection Tools
  28. Introduction
  29. ============
  30. grid.py is a parameter selection tool for C-SVM classification using
  31. the RBF (radial basis function) kernel. It uses cross validation (CV)
  32. technique to estimate the accuracy of each parameter combination in
  33. the specified range and helps you to decide the best parameters for
  34. your problem.
  35. grid.py directly executes libsvm binaries (so no python binding is needed)
  36. for cross validation and then draw contour of CV accuracy using gnuplot.
  37. You must have libsvm and gnuplot installed before using it. The package
  38. gnuplot is available at http://www.gnuplot.info/
  39. On Mac OSX, the precompiled gnuplot file needs the library Aquarterm,
  40. which thus must be installed as well. In addition, this version of
  41. gnuplot does not support png, so you need to change "set term png
  42. transparent small" and use other image formats. For example, you may
  43. have "set term pbm small color".
  44. Usage: grid.py [grid_options] [svm_options] dataset
  45. grid_options :
  46. -log2c {begin,end,step | "null"} : set the range of c (default -5,15,2)
  47. begin,end,step -- c_range = 2^{begin,...,begin+k*step,...,end}
  48. "null" -- do not grid with c
  49. -log2g {begin,end,step | "null"} : set the range of g (default 3,-15,-2)
  50. begin,end,step -- g_range = 2^{begin,...,begin+k*step,...,end}
  51. "null" -- do not grid with g
  52. -v n : n-fold cross validation (default 5)
  53. -svmtrain pathname : set svm executable path and name
  54. -gnuplot {pathname | "null"} :
  55. pathname -- set gnuplot executable path and name
  56. "null" -- do not plot
  57. -out {pathname | "null"} : (default dataset.out)
  58. pathname -- set output file path and name
  59. "null" -- do not output file
  60. -png pathname : set graphic output file path and name (default dataset.png)
  61. -resume [pathname] : resume the grid task using an existing output file (default pathname is dataset.out)
  62. Use this option only if some parameters have been checked for the SAME data.
  63. svm_options : additional options for svm-train
  64. The program conducts v-fold cross validation using parameter C (and gamma)
  65. = 2^begin, 2^(begin+step), ..., 2^end.
  66. You can specify where the libsvm executable and gnuplot are using the
  67. -svmtrain and -gnuplot parameters.
  68. For windows users, please use pgnuplot.exe. If you are using gnuplot
  69. 3.7.1, please upgrade to version 3.7.3 or higher. The version 3.7.1
  70. has a bug. If you use cygwin on windows, please use gunplot-x11.
  71. If the task is terminated accidentally or you would like to change the
  72. range of parameters, you can apply '-resume' to save time by re-using
  73. previous results. You may specify the output file of a previous run
  74. or use the default (i.e., dataset.out) without giving a name. Please
  75. note that the same condition must be used in two runs. For example,
  76. you cannot use '-v 10' earlier and resume the task with '-v 5'.
  77. The value of some options can be "null." For example, `-log2c -1,0,1
  78. -log2 "null"' means that C=2^-1,2^0,2^1 and g=LIBSVM's default gamma
  79. value. That is, you do not conduct parameter selection on gamma.
  80. Example
  81. =======
  82. > python grid.py -log2c -5,5,1 -log2g -4,0,1 -v 5 -m 300 heart_scale
  83. Users (in particular MS Windows users) may need to specify the path of
  84. executable files. You can either change paths in the beginning of
  85. grid.py or specify them in the command line. For example,
  86. > grid.py -log2c -5,5,1 -svmtrain "c:\Program Files\libsvm\windows\svm-train.exe" -gnuplot c:\tmp\gnuplot\binary\pgnuplot.exe -v 10 heart_scale
  87. Output: two files
  88. dataset.png: the CV accuracy contour plot generated by gnuplot
  89. dataset.out: the CV accuracy at each (log2(C),log2(gamma))
  90. The following example saves running time by loading the output file of a previous run.
  91. > python grid.py -log2c -7,7,1 -log2g -5,2,1 -v 5 -resume heart_scale.out heart_scale
  92. Parallel grid search
  93. ====================
  94. You can conduct a parallel grid search by dispatching jobs to a
  95. cluster of computers which share the same file system. First, you add
  96. machine names in grid.py:
  97. ssh_workers = ["linux1", "linux5", "linux5"]
  98. and then setup your ssh so that the authentication works without
  99. asking a password.
  100. The same machine (e.g., linux5 here) can be listed more than once if
  101. it has multiple CPUs or has more RAM. If the local machine is the
  102. best, you can also enlarge the nr_local_worker. For example:
  103. nr_local_worker = 2
  104. Example:
  105. > python grid.py heart_scale
  106. [local] -1 -1 78.8889 (best c=0.5, g=0.5, rate=78.8889)
  107. [linux5] -1 -7 83.3333 (best c=0.5, g=0.0078125, rate=83.3333)
  108. [linux5] 5 -1 77.037 (best c=0.5, g=0.0078125, rate=83.3333)
  109. [linux1] 5 -7 83.3333 (best c=0.5, g=0.0078125, rate=83.3333)
  110. .
  111. .
  112. .
  113. If -log2c, -log2g, or -v is not specified, default values are used.
  114. If your system uses telnet instead of ssh, you list the computer names
  115. in telnet_workers.
  116. Calling grid in Python
  117. ======================
  118. In addition to using grid.py as a command-line tool, you can use it as a
  119. Python module.
  120. >>> rate, param = find_parameters(dataset, options)
  121. You need to specify `dataset' and `options' (default ''). See the following example.
  122. > python
  123. >>> from grid import *
  124. >>> rate, param = find_parameters('../heart_scale', '-log2c -1,1,1 -log2g -1,1,1')
  125. [local] 0.0 0.0 rate=74.8148 (best c=1.0, g=1.0, rate=74.8148)
  126. [local] 0.0 -1.0 rate=77.037 (best c=1.0, g=0.5, rate=77.037)
  127. .
  128. .
  129. [local] -1.0 -1.0 rate=78.8889 (best c=0.5, g=0.5, rate=78.8889)
  130. .
  131. .
  132. >>> rate
  133. 78.8889
  134. >>> param
  135. {'c': 0.5, 'g': 0.5}
  136. Part III: LIBSVM format checking tools
  137. Introduction
  138. ============
  139. `svm-train' conducts only a simple check of the input data. To do a
  140. detailed check, we provide a python script `checkdata.py.'
  141. Usage: checkdata.py dataset
  142. Exit status (returned value): 1 if there are errors, 0 otherwise.
  143. This tool is written by Rong-En Fan at National Taiwan University.
  144. Example
  145. =======
  146. > cat bad_data
  147. 1 3:1 2:4
  148. > python checkdata.py bad_data
  149. line 1: feature indices must be in an ascending order, previous/current features 3:1 2:4
  150. Found 1 lines with error.

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