项目需要做分类任务,没法直接使用libsvm库自带的train.exe和predict.exe,于是参考这里https://www.cnblogs.com/cv-pr/p/5646434.html一篇博文,封装libsvm为C++类。
在vs2012建立一个工程,把libsvm里的svm.h和svm.cpp导入你的项目中。
CxLibSVM.h
#ifndef _CXLIBSVM_H_H
#define _CXLIBSVM_H_H
#include <string>
#include <vector>
#include <iostream>
#include "svm.h"
using namespace std;
//内存分配
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
class CxLibSVM
{
public:
struct svm_parameter param;
private:
struct svm_model* model_;
struct svm_problem prob;
struct svm_node * x_space;
public:
CxLibSVM();
~CxLibSVM();
void init_svm_param(struct svm_parameter& param);
void train(const vector<vector<double>>& x, const vector<double>& y, const struct svm_parameter& param);
int predict(const vector<double>& x,double& prob_est);
void do_cross_validation(const vector<vector<double>>& x, const vector<double>& y, const struct svm_parameter& param, const int & nr_fold);
int load_model(string model_path);
int save_model(string model_path);
void free_model();
};
#endif // !_CXLIBSVM_H_H
CxLibSVM.cpp
#include "stdafx.h"
#include "CxLibSVM.h"
CxLibSVM::CxLibSVM()
{
model_ = NULL;
}
CxLibSVM::~CxLibSVM()
{
free_model();
}
void CxLibSVM::init_svm_param(struct svm_parameter& param)
{
//参数初始化,参数调整部分在这里修改即可
// 默认参数
param.svm_type = C_SVC; //算法类型
param.kernel_type = LINEAR; //核函数类型
param.degree = 3; //多项式核函数的参数degree
param.coef0 = 0; //多项式核函数的参数coef0
param.gamma = 0.5; //1/num_features,rbf核函数参数
param.nu = 0.5; //nu-svc的参数
param.C = 10; //正则项的惩罚系数
param.eps = 1e-3; //收敛精度
param.cache_size = 100; //求解的内存缓冲 100MB
param.p = 0.1;
param.shrinking = 1;
param.probability = 1; //1表示训练时生成概率模型,0表示训练时不生成概率模型,用于预测样本的所属类别的概率
param.nr_weight = 0; //类别权重
param.weight = NULL; //样本权重
param.weight_label = NULL; //类别权重
}
void CxLibSVM::train(const vector<vector<double>>& x, const vector<double>& y, const struct svm_parameter& param)
{
if (x.size() == 0)
{
return;
}
//释放先前的模型
free_model();
/*初始化*/
long len = x.size();
long dim = x[0].size();
long elements = len * dim;
//转换数据为libsvm格式
prob.l = len;
prob.y = Malloc(double, prob.l);
prob.x = Malloc(struct svm_node *, prob.l);
x_space = Malloc(struct svm_node, elements + len);
int j = 0;
for (int l = 0; l < len; l++)
{
prob.x[l] = &x_space[j];
for (int d = 0; d < dim; d++)
{
x_space[j].index = d+1;
x_space[j].value = x[l][d];
j++;
}
x_space[j++].index = -1;
prob.y[l] = y[l];
}
/*训练*/
model_ = svm_train(&prob, ¶m);
}
int CxLibSVM::predict(const vector<double>& x,double& prob_est)
{
//数据转换
svm_node* x_test = Malloc(struct svm_node, x.size()+1);
for (unsigned int i=0; i<x.size(); i++)
{
x_test[i].index = i + 1;
x_test[i].value = x[i];
}
x_test[x.size()].index = -1;
double *probs = new double[model_->nr_class];//存储了所有类别的概率
//预测类别和概率
int value = (int)svm_predict_probability(model_, x_test, probs);
for (int k = 0; k < model_->nr_class;k++)
{//查找类别相对应的概率
if (model_->label[k] == value)
{
prob_est = probs[k];
break;
}
}
delete[] probs;
return value;
}
void CxLibSVM::do_cross_validation(const vector<vector<double>>& x, const vector<double>& y, const struct svm_parameter& param, const int & nr_fold)
{
if (x.size() == 0)return;
/*初始化*/
long len = x.size();
long dim = x[0].size();
long elements = len*dim;
//转换数据为libsvm格式
prob.l = len;
prob.y = Malloc(double, prob.l);
prob.x = Malloc(struct svm_node *, prob.l);
x_space = Malloc(struct svm_node, elements + len);
int j = 0;
for (int l = 0; l < len; l++)
{
prob.x[l] = &x_space[j];
for (int d = 0; d < dim; d++)
{
x_space[j].index = d + 1;
x_space[j].value = x[l][d];
j++;
}
x_space[j++].index = -1;
prob.y[l] = y[l];
}
int i;
int total_correct = 0;
double total_error = 0;
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
double *target = Malloc(double, prob.l);
svm_cross_validation(&prob, ¶m, nr_fold, target);
if (param.svm_type == EPSILON_SVR ||
param.svm_type == NU_SVR)
{
for (i = 0; i < prob.l; i++)
{
double y = prob.y[i];
double v = target[i];
total_error += (v - y)*(v - y);
sumv += v;
sumy += y;
sumvv += v*v;
sumyy += y*y;
sumvy += v*y;
}
printf("Cross Validation Mean squared error = %g\n", total_error / prob.l);
printf("Cross Validation Squared correlation coefficient = %g\n",
((prob.l*sumvy - sumv*sumy)*(prob.l*sumvy - sumv*sumy)) /
((prob.l*sumvv - sumv*sumv)*(prob.l*sumyy - sumy*sumy))
);
}
else
{
for (i = 0; i < prob.l; i++)
if (target[i] == prob.y[i])
++total_correct;
printf("Cross Validation Accuracy = %g%%\n", 100.0*total_correct / prob.l);
}
free(target);
}
int CxLibSVM::load_model(string model_path)
{
//释放原来的模型
free_model();
//导入模型
model_ = svm_load_model(model_path.c_str());
if (model_ == NULL)return -1;
return 0;
}
int CxLibSVM::save_model(string model_path)
{
int flag = svm_save_model(model_path.c_str(), model_);
return flag;
}
void CxLibSVM::free_model()
{
if (model_ != NULL)
{
svm_free_and_destroy_model(&model_);
svm_destroy_param(¶m);
if (prob.y != NULL)
{
free(prob.y);
prob.y = NULL;
}
if (prob.x != NULL)
{
free(prob.x);
prob.x = NULL;
}
if (x_space != NULL)
{
free(x_space);
x_space = NULL;
}
}
}
main.cpp
#include "stdafx.h"
#include "CxLibSVM.h"
#include <time.h>
#include <iostream>
using namespace std;
void gen_train_sample(vector<vector<double>>& x, vector<double>& y, long sample_num, long dim, double scale);
void gen_test_sample(vector<double>& x, long sample_num, long dim, double scale);
int _tmain(int argc, _TCHAR* argv[])
{
//初始化libsvm对象
CxLibSVM svm;
svm.init_svm_param(svm.param);
/*1、准备训练数据*/
vector<vector<double>> x; //样本集
vector<double> y; //样本类别标签集
gen_train_sample(x, y, 200, 10, 1);
/*1、交叉验证*/
int fold = 10;
svm.do_cross_validation(x, y, svm.param, fold);
/*2、训练*/
svm.train(x, y, svm.param);
/*3、保存模型*/
string model_path = "svm_model.txt";
svm.save_model(model_path);
/*4、导入模型*/
svm.load_model(model_path);
/*5、预测*/
//生成随机测试数据
vector<double> x_test;
gen_test_sample(x_test, 200, 10, 1);
double prob_est;
//预测
double value = svm.predict(x_test, prob_est);
//打印预测类别和概率
printf("label:%f, prob:%f", value, prob_est);
return 0;
}
void gen_train_sample(vector<vector<double>>& x, vector<double>& y, long sample_num, long dim, double scale)
{
srand((unsigned)time(NULL));//随机数
//生成随机的正类样本
for (int i = 0; i < sample_num; i++)
{
vector<double> rx;
for (int j = 0; j < dim; j++)
{
rx.push_back(scale*(rand() % dim));
}
x.push_back(rx);
y.push_back(1);
}
//生成随机的负类样本
for (int i = 0; i < sample_num; i++)
{
vector<double> rx;
for (int j = 0; j < dim; j++)
{
rx.push_back(-scale*(rand() % dim));
}
x.push_back(rx);
y.push_back(2);
}
}
void gen_test_sample(vector<double>& x, long sample_num, long dim, double scale)
{
srand((unsigned)time(NULL));//随机数
//生成随机的正类样本
for (int j = 0; j < dim; j++)
{
x.push_back(-scale*(rand() % dim));
}
}