LIBSVM easy.py (Process script) comments

Source: Internet
Author: User
Tags svm

Given the importance of the script, it is necessary to make a comprehensive comment on the script so that the LIBSVM can be used flexibly.

#!/usr/bin/env python#This method of setting the Python path is more scientificImportSYSImportOS fromSubprocessImport*#too few input parameters will prompt program usageifLen (SYS.ARGV) <= 1:    Print('Usage: {0} training_file [Testing_file]'. Format (sys.argv[0]))RaiseSystemexit#SVM, grid, and gnuplot executable filesIs_win32= (Sys.platform = ='Win32')if  notIs_win32:#program path configuration under Linux systemSvmscale_exe =".. /svm-scale"Svmtrain_exe=".. /svm-train"Svmpredict_exe=".. /svm-predict"grid_py="./grid.py"Gnuplot_exe="/usr/bin/gnuplot"   #you need to modify the secondary path, gnuplot the path to the executable, not the folder pathElse:    #Program path configuration under Windows systemSvmscale_exe = R".. \windows\svm-scale.exe"Svmtrain_exe= R".. \windows\svm-train.exe"Svmpredict_exe= R".. \windows\svm-predict.exe"Gnuplot_exe= R"C:\gnuplot\bin\gnuplot.exe"grid_py= R". \grid.py"assertOs.path.exists (Svmscale_exe),"Svm-scale executable not found"assertOs.path.exists (Svmtrain_exe),"Svm-train executable not found"assertOs.path.exists (Svmpredict_exe),"svm-predict executable not found"assertOs.path.exists (Gnuplot_exe),"gnuplot executable not found"assertOs.path.exists (Grid_py),"grid.py not found"#Create a training dataset related file: ". Scale", ". Model", ". Range" three filesTrain_pathname = sys.argv[1]assertOs.path.exists (Train_pathname),"training File not found"file_name= Os.path.split (train_pathname) [1]scaled_file= file_name +". scale"Model_file= file_name +". Model"Range_file= file_name +". Range"#Create a test dataset related file: ". Scale", ". Predict" two filesifLen (SYS.ARGV) > 2: Test_pathname= Sys.argv[2] file_name= Os.path.split (test_pathname) [1]    assertOs.path.exists (Test_pathname),"testing file not found"Scaled_test_file= file_name +". scale"Predict_test_file= file_name +". Predict"#process Command One: Svm-scale scaling, training set scaling, parameters are as follows:cmd ='{0}-S "{1}" "{2}" > "{3}"'. Format (Svmscale_exe, Range_file, Train_pathname, Scaled_file)Print('Scaling Training Data ...') Popen (cmd, Shell= True, stdout =PIPE). Communicate ()#process command Two: parameter optimization, using grid.py script, cross-validation, the parameters are as follows:cmd ='{0}-svmtrain "{1}"-gnuplot "{2} " "{3}"'. Format (grid_py, Svmtrain_exe, Gnuplot_exe, Scaled_file)Print('Cross validation ...') F= Popen (cmd, Shell = True, stdout =PIPE). Stdoutline="' whileTrue:last_line= Line Line=F.readline ()if  notLine Breakc,g,rate=map (Float,last_line.split ())#output optimal parameter c,gPrint('Best c={0}, G={1} CV rate={2}'. Format (c,g,rate))#process Command Three: Svm-train training, the parameters are set as followscmd ='{0}-c {1}-G {2} ' {3} ' ' {4} ''. Format (svmtrain_exe,c,g,scaled_file,model_file)Print('Training ...') Popen (cmd, Shell= True, stdout =PIPE). Communicate ()Print('Output model: {0}'. Format (model_file))ifLen (SYS.ARGV) > 2:        #process command four: Svm-scale scaling, test data scaling, parameter settings are as follows:cmd ='{0}-R "{1}" "{2}" > "{3}"'. Format (Svmscale_exe, Range_file, Test_pathname, Scaled_test_file)Print('Scaling testing Data ...') Popen (cmd, Shell= True, stdout =PIPE). Communicate ()#process Command Five: svm-predict prediction, the parameters are set as follows:cmd ='{0} "{1} " "{2}" "{3}"'. Format (Svmpredict_exe, Scaled_test_file, Model_file, Predict_test_file)Print('Testing ...') Popen (cmd, Shell=True). Communicate ()Print('Output prediction: {0}'. Format (Predict_test_file))

LIBSVM easy.py (Process script) comments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.