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.

easy.py 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. from subprocess import *
  5. if len(sys.argv) <= 1:
  6. print('Usage: {0} training_file [testing_file]'.format(sys.argv[0]))
  7. raise SystemExit
  8. # svm, grid, and gnuplot executable files
  9. is_win32 = (sys.platform == 'win32')
  10. if not is_win32:
  11. svmscale_exe = "../svm-scale"
  12. svmtrain_exe = "../svm-train"
  13. svmpredict_exe = "../svm-predict"
  14. grid_py = "./grid.py"
  15. gnuplot_exe = "/usr/bin/gnuplot"
  16. else:
  17. # example for windows
  18. svmscale_exe = r"..\windows\svm-scale.exe"
  19. svmtrain_exe = r"..\windows\svm-train.exe"
  20. svmpredict_exe = r"..\windows\svm-predict.exe"
  21. gnuplot_exe = r"c:\tmp\gnuplot\binary\pgnuplot.exe"
  22. grid_py = r".\grid.py"
  23. assert os.path.exists(svmscale_exe),"svm-scale executable not found"
  24. assert os.path.exists(svmtrain_exe),"svm-train executable not found"
  25. assert os.path.exists(svmpredict_exe),"svm-predict executable not found"
  26. assert os.path.exists(gnuplot_exe),"gnuplot executable not found"
  27. assert os.path.exists(grid_py),"grid.py not found"
  28. train_pathname = sys.argv[1]
  29. assert os.path.exists(train_pathname),"training file not found"
  30. file_name = os.path.split(train_pathname)[1]
  31. scaled_file = file_name + ".scale"
  32. model_file = file_name + ".model"
  33. range_file = file_name + ".range"
  34. if len(sys.argv) > 2:
  35. test_pathname = sys.argv[2]
  36. file_name = os.path.split(test_pathname)[1]
  37. assert os.path.exists(test_pathname),"testing file not found"
  38. scaled_test_file = file_name + ".scale"
  39. predict_test_file = file_name + ".predict"
  40. cmd = '{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file)
  41. print('Scaling training data...')
  42. Popen(cmd, shell = True, stdout = PIPE).communicate()
  43. cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{3}"'.format(grid_py, svmtrain_exe, gnuplot_exe, scaled_file)
  44. print('Cross validation...')
  45. f = Popen(cmd, shell = True, stdout = PIPE).stdout
  46. line = ''
  47. while True:
  48. last_line = line
  49. line = f.readline()
  50. if not line: break
  51. c,g,rate = map(float,last_line.split())
  52. print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate))
  53. cmd = '{0} -c {1} -g {2} "{3}" "{4}"'.format(svmtrain_exe,c,g,scaled_file,model_file)
  54. print('Training...')
  55. Popen(cmd, shell = True, stdout = PIPE).communicate()
  56. print('Output model: {0}'.format(model_file))
  57. if len(sys.argv) > 2:
  58. cmd = '{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file)
  59. print('Scaling testing data...')
  60. Popen(cmd, shell = True, stdout = PIPE).communicate()
  61. cmd = '{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file)
  62. print('Testing...')
  63. Popen(cmd, shell = True).communicate()
  64. print('Output prediction: {0}'.format(predict_test_file))

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