5 from subprocess
import *
8 print(
'Usage: {0} training_file [testing_file]'.format(sys.argv[0]))
13 is_win32 = (sys.platform ==
'win32')
15 svmscale_exe =
"../svm-scale" 16 svmtrain_exe =
"../svm-train" 17 svmpredict_exe =
"../svm-predict" 19 gnuplot_exe =
"/usr/bin/gnuplot" 22 svmscale_exe =
r"..\windows\svm-scale.exe" 23 svmtrain_exe =
r"..\windows\svm-train.exe" 24 svmpredict_exe =
r"..\windows\svm-predict.exe" 25 gnuplot_exe =
r"c:\tmp\gnuplot\binary\pgnuplot.exe" 26 grid_py =
r".\grid.py" 28 assert os.path.exists(svmscale_exe),
"svm-scale executable not found" 29 assert os.path.exists(svmtrain_exe),
"svm-train executable not found" 30 assert os.path.exists(svmpredict_exe),
"svm-predict executable not found" 31 assert os.path.exists(gnuplot_exe),
"gnuplot executable not found" 32 assert os.path.exists(grid_py),
"grid.py not found" 34 train_pathname = sys.argv[1]
35 assert os.path.exists(train_pathname),
"training file not found" 36 file_name = os.path.split(train_pathname)[1]
37 scaled_file = file_name +
".scale" 38 model_file = file_name +
".model" 39 range_file = file_name +
".range" 42 test_pathname = sys.argv[2]
43 file_name = os.path.split(test_pathname)[1]
44 assert os.path.exists(test_pathname),
"testing file not found" 45 scaled_test_file = file_name +
".scale" 46 predict_test_file = file_name +
".predict" 48 cmd =
'{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file)
49 print(
'Scaling training data...')
50 Popen(cmd, shell =
True, stdout = PIPE).communicate()
52 cmd =
'{0} -svmtrain "{1}" -gnuplot "{2}" "{3}"'.format(grid_py, svmtrain_exe, gnuplot_exe, scaled_file)
53 print(
'Cross validation...')
54 f = Popen(cmd, shell =
True, stdout = PIPE).stdout
61 c,g,rate = map(float,last_line.split())
63 print(
'Best c={0}, g={1} CV rate={2}'.format(c,g,rate))
65 cmd =
'{0} -c {1} -g {2} "{3}" "{4}"'.format(svmtrain_exe,c,g,scaled_file,model_file)
67 Popen(cmd, shell =
True, stdout = PIPE).communicate()
69 print(
'Output model: {0}'.format(model_file))
71 cmd =
'{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file)
72 print(
'Scaling testing data...')
73 Popen(cmd, shell =
True, stdout = PIPE).communicate()
75 cmd =
'{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file)
77 Popen(cmd, shell =
True).communicate()
79 print(
'Output prediction: {0}'.format(predict_test_file))