鉴于该脚本的重要性,很有必要对该脚本做一个全面的注释,以便可以灵活的使用libsvm。

#!/usr/bin/env python
# 这种设置python路径的方法更为科学 import sys
import os
from subprocess import * # 输入参数太少就会提示程序用法
if len(sys.argv) <= 1:
print('Usage: {0} training_file [testing_file]'.format(sys.argv[0]))
raise SystemExit # svm, grid, and gnuplot executable files is_win32 = (sys.platform == 'win32')
if not is_win32:
# Linux系统下的程序路径配置
svmscale_exe = "../svm-scale"
svmtrain_exe = "../svm-train"
svmpredict_exe = "../svm-predict"
grid_py = "./grid.py"
gnuplot_exe = "/usr/bin/gnuplot" #需要修改次路径,gnuplot为可执行程序的路径,不是文件夹路径
else:
# windows系统下的程序路径配置
svmscale_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" assert os.path.exists(svmscale_exe),"svm-scale executable not found"
assert os.path.exists(svmtrain_exe),"svm-train executable not found"
assert os.path.exists(svmpredict_exe),"svm-predict executable not found"
assert os.path.exists(gnuplot_exe),"gnuplot executable not found"
assert os.path.exists(grid_py),"grid.py not found" # 创建训练数据集相关的文件:".scale",".model",".range"三个文件
train_pathname = sys.argv[1]
assert os.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" # 创建测试数据集相关文件:".scale",".predict"两个文件
if len(sys.argv) > 2:
test_pathname = sys.argv[2]
file_name = os.path.split(test_pathname)[1]
assert os.path.exists(test_pathname),"testing file not found"
scaled_test_file = file_name + ".scale"
predict_test_file = file_name + ".predict" # 流程化命令一:svm-scale缩放,训练集缩放,参数如下:
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() # 流程化命令二:参数选优,使用grid.py脚本,进行交叉验证,参数如下:
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).stdout line = ''
while True:
last_line = line
line = f.readline()
if not line: break
c,g,rate = map(float,last_line.split())
# 输出最优参数c,g
print('Best c={0}, g={1} CV rate={2}'.format(c,g,rate)) # 流程化命令三:svm-train训练,参数设置如下
cmd = '{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))
if len(sys.argv) > 2:
# 流程化命令四:svm-scale缩放,测试数据缩放,参数设置如下:
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() # 流程化命令五:svm-predict预测,参数设置如下:
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(流程化脚本)注释的更多相关文章

  1. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

  2. libsvm easy.py ValueError: need more than 0 values to unpack windows下终极解决

    现象是: python easy.py train test 输出: Scaling training data...WARNING: original #nonzeros 100389 new #n ...

  3. Atitit usrqbg1834 html的逻辑化流程化 规范标准化解决方案

    Atitit usrqbg1834 html的逻辑化流程化 规范标准化解决方案 常用指令1 ..v-if.v-else指令2 v-for指令3 MVVM大比拼4 常用指令 本来按照Vue文档说明,常用 ...

  4. Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题

    日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...

  5. easy.py使用中ValueError: could not convert string to float: svm_options错误问题解决

    在使用easy.py中出现如下图所示问题 解决方法: 1.找到cmd = '{0} -svmtrain "{1}" -gnuplot "{2}" "{ ...

  6. Centos7 系统初试化脚本

    系统初始化设置 # 设置主机名,永久修改,再次登陆生效 hostnamectl set-hostname xxxxx # 安装eprl源,常用命令 yum install -y wget && ...

  7. Wifite.py 修正版脚本代码

    Kali2.0系统自带的WiFite脚本代码中有几行错误,以下是修正后的代码: #!/usr/bin/python # -*- coding: utf-8 -*- """ ...

  8. JsDoc脚本注释文档生成

    使用jsDoc可使用特定注释,将注释的内容生成文档,可用于生成脚本库的API文档 jsdoc 文档:   http://usejsdoc.org/

  9. Linux启动流程和脚本服务-6

    授课笔记:----------------------------------- linux系统启动流程:一.初始化阶段:1.grub引导界面2.识别硬件3.初始化驱动 二.加载/etc/rc.d/r ...

随机推荐

  1. MDK中 use microlib

    microlib 与缺省 C 库之间的主要差异是: microlib 不符合 ISO C 库标准. 不支持某些 ISO 特性,并且其他特性具有的功能也较少. microlib 不符合 IEEE 754 ...

  2. lua中特殊用法

    th> a=torch.zeros(,) [.0001s] th> a [torch.DoubleTensor of size 1x5] [.0001s] th> a[{,floor ...

  3. Codeforces Round #260 (Div. 2) A

    Description One day Dima and Alex had an argument about the price and quality of laptops. Dima think ...

  4. 2016年6月26日 星期日 --出埃及记 Exodus 14:23

    2016年6月26日 星期日 --出埃及记 Exodus 14:23 The Egyptians pursued them, and all Pharaoh's horses and chariots ...

  5. P1533 可怜的狗狗

    http://www.luogu.org/problem/show?pid=1533 题目背景 小卡由于公务需要出差,将新家中的狗狗们托付给朋友嘉嘉,但是嘉嘉是一个很懒的人,他才没那么多时间帮小卡喂狗 ...

  6. IOS文字属性备注

    // Predefined character attributes for text. If the key is not in the dictionary, then use the defau ...

  7. 取出一个int的每一位,用算法

    int a=1234: int current: while(a) { current=a%10://4 cout<<current; a=a%10; }

  8. .Net操作.exe文件

    Process proc = new Process(); proc.StartInfo.FileName = @"D:\Program Files\Foxmail\Foxmail.exe& ...

  9. 在包a中新建一个类A,在类A中有一个int add(int m)方法,用来求1+2+…+m 的和。在包b中新建一个类B,在类B中有一个int cheng(int n)方法,用来求n! 的结果。在包c中新建一个主类C,调用A、B中的方法输出1+2+…+30的和, 以及5!的计算结果。

    package a; public class A { public void add(int m) { int sum=0; for (int i = 1; i <=m; i++) { sum ...

  10. [翻译]观察变换View Transform (Direct3D 9)

    这一节介绍在Direct3d中观察变换的基本概念和怎么去设置观察矩阵. 视口变换把观察者放在世界坐标系中,并把顶点转化到摄像机空间.在摄像机空间,摄像机或者说观察者在原点,观察方向为z轴正向.Dire ...