鉴于该脚本的重要性,很有必要对该脚本做一个全面的注释,以便可以灵活的使用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. winform中利用反射实现泛型数据访问对象基类(2)

    在1的基础上做了一点改进 参数化处理 看上去更简洁 无主键情况下 update 方法需要改进 insert delete没有问题  /// <summary>     /// DAO基类 ...

  2. ContentProvider官方教程(3)ContentResolver查询、遍历 示例

    Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...

  3. 使用mybatis完成通用dao和通用service

    使用mybatis完成通用dao和通用service 概述: 使用通用dao和通用service可以减少代码的开发.可以将常用的增删改查放到通用dao中.对不同的or框架,基本上都有自己的实现如Spr ...

  4. (1)若当前字符不是大于0的数字字符,则复制该字符于新字符串中; (2)若当前字符是一个数字字符,且它之后没有后继字符,则简单地将它复制到新字符串中; (3)若当前字符是一个大于0的数字字符,并且还有后继字符,设该数字字符的面值为n,将它的后继字符重复复制n+1次到新字符串中; (4)以上述一次变换为一组,在不同组之间另插入一个"_"用于分割(5))若字符串中包含有下划线'_',则变换为 \UL

    package b; import java.util.Scanner; public class Zifuchuan { public static void main(String[] args) ...

  5. Create Function

    示例,创建一个名为HelloWorld4的函数,不需要输入参数 CREATE FUNCTION HelloWorld4()RETURNS VARCHAR(20)ASBEGINRETURN 'Hello ...

  6. Javascript this指针

    Javascript是一门基于对象的动态语言,也就是说,所有东西都是对象,一个很典型的例子就是函数也被视为普通的对象.   前言 Javascript是一门基于对象的动态语言,也就是说,所有东西都是对 ...

  7. [HDOJ3911]Black And White(线段树,区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意:一个01串,两种操作: 0 a b:查询[a,b]内连续1的最长长度. 1 a b:翻转[ ...

  8. JAVA开发--U盘EXE恢复工具

    原理比较简单,在学校机房U盘总被感染,写一个工具来方便用 package com.udiskrecover; import java.awt.Container; import java.awt.Fl ...

  9. C#窗体->>随机四则运算(计算表达式)

    用户需求: 程序能接收用户输入的整数答案,并判断对错程序结束时,统计出答对.答错的题目数量.补充说明:0——10的整数是随机生成的用户可以选择四则运算中的一种用户可以结束程序的运行,并显示统计结果.在 ...

  10. (Theano 1)Theano自述文件

    Theano在GitHub上的自述文件 https://github.com/Theano/Theano 也不知道这个Theano好不好,但是从Theano到Lasagne:基于Python的深度学习 ...