easy.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys
00004 import os
00005 from subprocess import *
00006 
00007 if len(sys.argv) <= 1:
00008         print('Usage: {0} training_file [testing_file]'.format(sys.argv[0]))
00009         raise SystemExit
00010 
00011 # svm, grid, and gnuplot executable files
00012 
00013 is_win32 = (sys.platform == 'win32')
00014 if not is_win32:
00015         svmscale_exe = "../svm-scale"
00016         svmtrain_exe = "../svm-train"
00017         svmpredict_exe = "../svm-predict"
00018         grid_py = "./grid.py"
00019         gnuplot_exe = "/usr/bin/gnuplot"
00020 else:
00021         # example for windows
00022         svmscale_exe = r"..\windows\svm-scale.exe"
00023         svmtrain_exe = r"..\windows\svm-train.exe"
00024         svmpredict_exe = r"..\windows\svm-predict.exe"
00025         gnuplot_exe = r"c:\tmp\gnuplot\binary\pgnuplot.exe"
00026         grid_py = r".\grid.py"
00027 
00028 assert os.path.exists(svmscale_exe),"svm-scale executable not found"
00029 assert os.path.exists(svmtrain_exe),"svm-train executable not found"
00030 assert os.path.exists(svmpredict_exe),"svm-predict executable not found"
00031 assert os.path.exists(gnuplot_exe),"gnuplot executable not found"
00032 assert os.path.exists(grid_py),"grid.py not found"
00033 
00034 train_pathname = sys.argv[1]
00035 assert os.path.exists(train_pathname),"training file not found"
00036 file_name = os.path.split(train_pathname)[1]
00037 scaled_file = file_name + ".scale"
00038 model_file = file_name + ".model"
00039 range_file = file_name + ".range"
00040 
00041 if len(sys.argv) > 2:
00042         test_pathname = sys.argv[2]
00043         file_name = os.path.split(test_pathname)[1]
00044         assert os.path.exists(test_pathname),"testing file not found"
00045         scaled_test_file = file_name + ".scale"
00046         predict_test_file = file_name + ".predict"
00047 
00048 cmd = '{0} -s "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, train_pathname, scaled_file)
00049 print('Scaling training data...')
00050 Popen(cmd, shell = True, stdout = PIPE).communicate()   
00051 
00052 cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{3}"'.format(grid_py, svmtrain_exe, gnuplot_exe, scaled_file)
00053 print('Cross validation...')
00054 f = Popen(cmd, shell = True, stdout = PIPE).stdout
00055 
00056 line = ''
00057 while True:
00058         last_line = line
00059         line = f.readline()
00060         if not line: break
00061 c,g,rate = map(float,last_line.split())
00062 
00063 print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate))
00064 
00065 cmd = '{0} -c {1} -g {2} "{3}" "{4}"'.format(svmtrain_exe,c,g,scaled_file,model_file)
00066 print('Training...')
00067 Popen(cmd, shell = True, stdout = PIPE).communicate()
00068 
00069 print('Output model: {0}'.format(model_file))
00070 if len(sys.argv) > 2:
00071         cmd = '{0} -r "{1}" "{2}" > "{3}"'.format(svmscale_exe, range_file, test_pathname, scaled_test_file)
00072         print('Scaling testing data...')
00073         Popen(cmd, shell = True, stdout = PIPE).communicate()   
00074 
00075         cmd = '{0} "{1}" "{2}" "{3}"'.format(svmpredict_exe, scaled_test_file, model_file, predict_test_file)
00076         print('Testing...')
00077         Popen(cmd, shell = True).communicate()  
00078 
00079         print('Output prediction: {0}'.format(predict_test_file))


ml_classifiers
Author(s): Scott Niekum
autogenerated on Thu Aug 27 2015 13:59:04