关闭

libSVM应用举例

标签: SVMlibSVM机器学习林智仁支持向量机
387人阅读 评论(0) 收藏 举报
分类:
前面提到,很多人看到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_features

param->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

0
0
查看评论

libSVM应用举例

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

libsvm中tools(easy.py,subset.py,grid.py,checkdata.py)的使用

这几天在用libsvm(2.8.6)中的一些工具,总结一下.libsvm的一些工具还是非常有用的,1.可以调用subset.py将你的样本集合按你所想要的比例进行抽样出两个子样本集合.2.还可以调用grid.py做关于(C,gamma)的交叉验证参数选择,可以轻松的搜索到最好的参数对(C,gamma...
  • chl033
  • chl033
  • 2009-08-01 21:27
  • 5010

LibSVM和LSSVM的使用

LibSVM和LSSVM的使用
  • yuliured
  • yuliured
  • 2016-05-08 01:34
  • 653

LibSVM的使用

LibSVM使用指南 本文包含以下几个部分: 支持向量机--SVM简介LibSVM的安装LibSVM的使用LibSVM参数调优Java版LibSVM库函数的调用 SVM简介 在进行下面的内容时我们认为你已经具备了数据挖掘的基础知识。 SVM是新近出现的强大的数据挖掘工具,它在文...
  • ecnu18918079120
  • ecnu18918079120
  • 2016-10-31 22:11
  • 1520

LibSVM 在matlab中的配置(包括SVDD、libsvm-faruto)

LibSVM的配置是初学LibSVM者遇到的最麻烦的问题,网上已经有很多关于配置的教程但是质量参差不齐,很多内容已过时,本人综合网上的教程和自己的实践,整理出更为实用简洁的教程。
  • ymmily_fyx
  • ymmily_fyx
  • 2015-05-23 12:45
  • 3252

LIBSVM-3.21在python下的使用

 本文源于SVM算法应用中实际遇到的问题。 算法实现的程序是按着麦好的《机器学习实践指南》自己敲的。 问题出现在第18行,找不到LibSvm这个库。 为了解决这个问题,进行了几个小时,一系列的尝试,终于。。。这个问题仍然没有解决,但是成功安...
  • happy22happy
  • happy22happy
  • 2016-07-04 21:46
  • 2223

LibSVM在Java中的简单应用

首先,在这里首先感谢台湾林智仁先生的开源工具包libsvm。使SVM算法更加普及。大家可以到下面的libsvm官网去了解相关的信息。 Libsvm官方网站->https://www.csie.ntu.edu.tw/~cjlin/libsvm/ 其次,我在使用过程中发现,先生svm_scal...
  • qq_18149897
  • qq_18149897
  • 2016-07-12 10:59
  • 6653

用JAVA程序调用LibSVM API

1. LibSVM简介       LibSVM是台湾著名教授陈智仁团队的杰作。具有各个语言版本的接口,包括C/C++、Java、Python、Matlab、C# 等等。这套库运算速度还是挺快的,可以很方便的对数据做分类或回归。由于libS...
  • u013709270
  • u013709270
  • 2017-01-03 21:04
  • 1448

C++语言基础 例程 类和对象的简单应用举例

贺老师的教学链接  本课讲解实例1:求出三角形的周长和面积#include #include #include using namespace std; class Triangle { public: void setABC(double x, double y, do...
  • sxhelijian
  • sxhelijian
  • 2015-03-07 09:45
  • 1245

LIBSVM3.2.0中的matlab文档说明

/**************************************************************/ *该说明文档为最新LIBSVM版本3.2.0中的matlab文件夹下的说明文档; *内容上除了基本介绍外,主要添加的一种SVM核函数(-t 4):预计算(定...
  • on2way
  • on2way
  • 2015-01-06 21:34
  • 2250
    个人资料
    • 访问:5751830次
    • 积分:65648
    • 等级:
    • 排名:第41名
    • 原创:427篇
    • 转载:3869篇
    • 译文:0篇
    • 评论:345条
    最新评论