前面提到,很多人看到 libSVM这么多的参数,估计要犯晕了。没关系,我之前把相关的libSVM参数已经讲解了一遍,这里,再给出libSVM的用法。如果你不想花时间去仔细研究libSVM,完全可以参照我的函数来直接调用libSVM完成你的工作。

首先是训练SVM得到模型;假设,有10个训练样本,每个训练样本,有12个特征值,即:每个训练样本的维数是12,也就是说,训练样本构成了一个10*12的矩阵(当然,所有数据应该被归一化到[-1,1]或[0,1]范围内),另外,所有训练样本,应该有一个明确的类别标签,以此来表明当前样本所属的类别。所以,完整的训练样本,应该是10*13的矩阵,这里,我们给出一个10*13的矩阵,并认为,它就是训练样本。每个训练样本是个行向量,而该矩阵的第一列则代表训练样本的“标签”。即:第一个训练样本,类别标签为“1”,第二个训练样本类别标签为“-1”。第一个训练样本的第一个特征值为0.708333,第二个特征值为1,第三个特征值为1...

double inputArr[10][13] = {1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1, -1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,-1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,-1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,-1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1};

另外,我们给出一个待分类的输入向量,并用行向量形式表示:

double testArr[]={0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1};

下面,给出完整的SVM训练及预测程序:

#include "stdafx.h"#include "svm.h"#include "iostream"#include "fstream"using namespace std;  double inputArr[10][13] = {1,0.708333,1,1,-0.320755,-0.105023,-1,1,-0.419847,-1,-0.225806,0,1, -1,0.583333,-1,0.333333,-0.603774,1,-1,1,0.358779,-1,-0.483871,0,-1,1,0.166667,1,-0.333333,-0.433962,-0.383562,-1,-1,0.0687023,-1,-0.903226,-1,-1,-1,0.458333,1,1,-0.358491,-0.374429,-1,-1,-0.480916,1,-0.935484,0,-0.333333,-1,0.875,-1,-0.333333,-0.509434,-0.347032,-1,1,-0.236641,1,-0.935484,-1,-0.333333,-1,0.5,1,1,-0.509434,-0.767123,-1,-1,0.0534351,-1,-0.870968,-1,-1,1,0.125,1,0.333333,-0.320755,-0.406393,1,1,0.0839695,1,-0.806452,0,-0.333333,1,0.25,1,1,-0.698113,-0.484018,-1,1,0.0839695,1,-0.612903,0,-0.333333,1,0.291667,1,1,-0.132075,-0.237443,-1,1,0.51145,-1,-0.612903,0,0.333333,1,0.416667,-1,1,0.0566038,0.283105,-1,1,0.267176,-1,0.290323,0,1};double testArr[]={0.25,1,1,-0.226415,-0.506849,-1,-1,0.374046,-1,-0.83871,0,-1};void DefaultSvmParam(struct svm_parameter *param){param->svm_type = C_SVC;param->kernel_type = RBF;param->degree = 3;param->gamma = 0; // 1/num_featuresparam->coef0 = 0;param->nu = 0.5;param->cache_size = 100;param->C = 1;param->eps = 1e-3;param->p = 0.1;param->shrinking = 1;param->probability = 0;param->nr_weight = 0;param->weight_label = NULL;param->weight = NULL;}void SwitchForSvmParma(struct svm_parameter *param, char ch, char *strNum, int nr_fold, int cross_validation){switch(ch){case 's':{param->svm_type = atoi(strNum);break;}case 't':{param->kernel_type = atoi(strNum);break;}case 'd':{param->degree = atoi(strNum);break;}case 'g':{param->gamma = atof(strNum);break;}case 'r':{param->coef0 = atof(strNum);break;}case 'n':{param->nu = atof(strNum);break;}case 'm':{param->cache_size = atof(strNum);break;}case 'c':{param->C = atof(strNum);break;}case 'e':{param->eps = atof(strNum);break;}case 'p':{param->p = atof(strNum);break;}case 'h':{param->shrinking = atoi(strNum);break;}case 'b':{param->probability = atoi(strNum);break;}case 'q':{break;}case 'v':{cross_validation = 1;nr_fold = atoi(strNum);if (nr_fold < 2){cout<<"nr_fold should > 2!!! file: "<<__FILE__<<" function: ";cout<<__FUNCTION__<<" line: "<<__LINE__<<endl;}break;}case 'w':{++param->nr_weight;param->weight_label = (int *)realloc(param->weight_label,sizeof(int)*param->nr_weight);param->weight = (double *)realloc(param->weight,sizeof(double)*param->nr_weight);param->weight_label[param->nr_weight-1] = atoi(strNum);param->weight[param->nr_weight-1] = atof(strNum);break;}default:{break;}}}void SetSvmParam(struct svm_parameter *param, char *str, int cross_validation, int nr_fold){DefaultSvmParam(param);cross_validation = 0;char ch = ' ';int strSize = strlen(str);for (int i=0; i<strSize; i++){if (str[i] == '-'){ch = str[i+1];int length = 0;for (int j=i+3; j<strSize; j++){if (isdigit(str[j])){length++;}else{break;}}char *strNum = new char[length+1];int index = 0;for (int j=i+3; j<i+3+length; j++){strNum[index] = str[j];index++;}strNum[length] = '/0';SwitchForSvmParma(param, ch, strNum, nr_fold, cross_validation);delete strNum;}}}void SvmTraining(char *option){struct svm_parameter param;   struct svm_problem prob;    struct svm_model *model;struct svm_node *x_space;int cross_validation = 0;int nr_fold = 0;int sampleCount = 10;int featureDim = 12;prob.l =  sampleCount;prob.y = new double[sampleCount];prob.x = new struct svm_node*[sampleCount];x_space = new struct svm_node[(featureDim+1)*sampleCount];SetSvmParam(¶m, option, cross_validation, nr_fold);for (int i=0; i<sampleCount; i++){prob.y[i] = inputArr[i][0];}int j = 0;for (int i=0; i<sampleCount; i++){prob.x[i] = &x_space[j];for (int k=1; k<=featureDim; k++){x_space[i*featureDim+k].index = k;x_space[i*featureDim+k].value = inputArr[i][k];}x_space[(i+1)*featureDim].index = -1;j = (i+1)*featureDim + 1; }model = svm_train(&prob, ¶m);const char* model_file_name = "C://Model.txt";svm_save_model(model_file_name, model);svm_destroy_model(model);svm_destroy_param(¶m);delete[] prob.y;delete[] prob.x;delete[] x_space; }int SvmPredict(const char* modelAdd){struct svm_node *testX;struct svm_model* testModel;testModel = svm_load_model(modelAdd);int featureDim = 12;testX = new struct svm_node[featureDim+1];for (int i=0; i<featureDim; i++){testX[i].index = i+1;testX[i].value = testArr[i];}testX[featureDim].index = -1;double p = svm_predict(testModel, testX);svm_destroy_model(testModel);delete[] testX;if (p > 0.5){return 1;}else{return -1;}}int _tmain(int argc, _TCHAR* argv[]){SvmTraining("-c 100 -t 1 -g 4 -r 1 -d 4");int flag = SvmPredict("c://model.txt");cout<<"flag = "<<flag<<endl;system("pause");return 0;}

出处:http://blog.csdn.net/carson2005/article/details/6539218

libSVM应用举例相关推荐

  1. [转]libsvm介绍及使用

    支持向量机简介 支持向量机SVM是从线性可分情况下的最优分类面提出的.所谓最优分类,就是要求分类线不但能够将两类无错误的分开,而且两类之间的分类间隔最大,前者是保证经验风险最小(为0),而通过后面的讨 ...

  2. 支持向量机SVM 参数选择

    http://ju.outofmemory.cn/entry/119152 http://www.cnblogs.com/zhizhan/p/4412343.html 支持向量机SVM是从线性可分情况 ...

  3. 使用libsvm实现归一化

    使用libsvm实现归一化 我在使用svm进行分类的过程中接触了非常好用的库libsvm,但是一开始我是在matlab下使用的,libsvm中有matlab下使用的函数,如下: libsvmwrite ...

  4. (转)python 搭建libsvm方法。python版本和libsvm版本匹配很重要!

    <集体智慧编程>关于婚介数据集的SVM分类 转自:http://muilpin.blog.163.com/blog/static/165382936201131875249123/ 作者写 ...

  5. libSVM + VS2013 + C++使用介绍

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lhanchao/article/details/53367532 </div><l ...

  6. linux代码动态分析软件,举例分析Linux动态库和静态库

    函数库分为静态库和动态库两种.创建Linux静态库和Linux动态库和使用它们在这里将以举例的形式详述一下.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不 ...

  7. libsvm java api文档,libsvm-javaAPI

    获得 tw.edu.ntu.csie libsvm 3.17 libsvm-3.17.jar的结构见下图, default package:这里是工具制作者自己封装出来的一些类,它们都有main函数, ...

  8. TCP/IP基础概念及通信过程举例

    TCP/IP基础概念及通信过程举例 出现 上个世纪60年代,由于中央集中式网络的容灾性较弱,以美国国防部为中心的一家组织研究出分组交换网络.后来为了验证分组交换技术的实用性,ARPANET出现了,并且 ...

  9. Tesseract-OCR 3.04简单使用举例(读入图像输出识别结果)

    下面code是对Tesseract-OCR 3.04版本进行简单使用的举例:包括两段,一个是读入带有中文字符的图像,一个是读入仅有英文字符的图像: #include "funset.hpp& ...

最新文章

  1. Linux三剑客之grep详解
  2. _id 和 ObjectId
  3. iis Service Unavailable解决方法(权限问题)
  4. Java实现算法导论中求解模线性方程解(基于最大公约数欧几里得扩展算法)
  5. 合并k个有序链表 python_[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
  6. ASIHTTPRequest下载数据
  7. 算法设计与分析——回溯法——圆排列问题
  8. c++ 智能指针用法详解
  9. Redis整合springboot实现消息队列
  10. Kafka——使用spring进行集成
  11. golang使用http client发起get和post请求示例
  12. Win32学习笔记(21)内存映射文件
  13. java实现模拟多道程序设计
  14. c语言ifi=1 2,第1部分_C语言程序设计练习一全解.doc
  15. Xshell “所选的用户密钥未在远程主机上注册,请再试一次”SSH 登录远程linux服务器(良心整理)
  16. require.js的简单使用
  17. docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
  18. ImportError: cannot import name ‘get_all_providers‘ from ‘onnxruntime.capi._pybind_state‘
  19. 强化学习中的backups
  20. 2020 icpc 沈阳

热门文章

  1. mysql开启定时器_MySQL定时器开启、调用实现代码
  2. jvm性能调优实战 - 39一次大促导致的内存泄漏和Full GC优化
  3. Java 8 - 收集器Collectors
  4. 小工匠聊架构 - 分布式缓存技术
  5. Elasticsearch-03 CentOS7 / Windows上部署Elasticsearch5.6.16集群模式
  6. Android接收短信-createFromPdu
  7. Linux执行yum不显示时间,Linux停的yum命令详解(朝花夕拾)
  8. java语言 文件上传,java中实现文件上传的方法
  9. android 地图相册,时光地图相册
  10. CSS学习03之基本选择器