转载请说明出处 http://blog.csdn.net/u013491262/article/details/37344193 点击打开链接
step1 : 理论学习,资料随便找
step2: 源码分析,资料随便找
step3; 简单实践
一、生成数据
为了将问题简化,特意生成训练集 tain_data.txt
类1 : y<=x-1 ,
类2 : y>= x+1 .
个100组数据,特征为2维。
测试集100组数据test_data.txt
int main(){
freopen("tain_data.txt" , "w" , stdout) ;
srand( (unsigned int) time(NULL) ) ;
int n = 0 , x , y ;
while(n < 100){
x = rand() % 10 + 1 ;
y = rand() % 10 + 1 ;
if(y <= x - 1) printf("-1 %d %d\n" , x, y) ;
else continue ;
n++ ;
}
n = 0 ;
while(n < 100){
x = rand() % 10 + 1 ;
y = rand() % 10 + 1 ;
if(y >= x + 1) printf("1 %d %d\n" , x, y) ;
else continue ;
n++ ;
}
freopen("test_data.txt" , "w" , stdout) ;
n = 0 ;
while(n < 100){
x = rand() % 10 + 1 ;
y = rand() % 10 + 1 ;
if(y <= x - 1) printf("-1 %d %d\n" , x, y) ;
else if(y >= x + 1) printf("1 %d %d\n" , x, y) ;
else continue ;
n++ ;
}
return 0;
}
二: 实验
引用《交大源码分析》
引用《交大源码分析》
代码部分: 此代码难度系数很小,没有细讲必要。
#include "svm.h"
using namespace std ;
const int feature_size = 2 ;
const int train_size = 200 ;
svm_problem prob ;
void init_svm_problem(){
prob.l = train_size ;
prob.y = new double[train_size] ;
prob.x = new svm_node* [train_size] ;
svm_node *x_space = new svm_node[train_size*(1+feature_size)] ;
ifstream in ;
in.open("tain_data.txt") ;
double value , lb ;
for(int i = 0 ; i < train_size ; i++){
in>>lb ;
//prob.y[i] = lb ;
if(i < train_size/2) prob.y[i] = 1 ;
else prob.y[i] = -1 ;
for(int j = 0 ; j < feature_size ; j++){
in>>value ;
if(value != 0.0){
x_space[i*(feature_size+1) + j].index = j + 1 ;
x_space[i*(feature_size+1) + j].value = value ;
}
}
x_space[i*(feature_size+1) + feature_size].index = -1 ;
prob.x[i] = &x_space[i*(feature_size+1)] ;
}
in.close() ;
}
svm_parameter param ;
void init_svm_parameter(){
param.svm_type = C_SVC;
param.kernel_type = RBF;
param.degree = 3;
param.gamma = 0.0001;
param.coef0 = 0;
param.nu = 0.5;
param.cache_size = 100;
param.C = 11;
param.eps = 1e-5;
param.p = 0.1;
param.shrinking = 1;
param.probability = 0;
param.nr_weight = 0;
param.weight_label = NULL;
param.weight = NULL;
}
const int test_size = 100 ;
double predict_lable[test_size] ;
double test_lable[test_size] ;
int main(){
init_svm_problem() ;
init_svm_parameter() ;
if(param.gamma == 0) param.gamma = 0.5 ;
svm_model* model = svm_train(&prob , ¶m) ;
ifstream in ;
in.open("test_data.txt") ;
svm_node *test = new svm_node[3] ;
for(int i = 0 ; i < test_size ; i++){
double value ;
in>>test_lable[i] ;
for(int j = 0 ; j < feature_size ; j++){
in>>value ;
if(value != 0.0){
test[j].index = j + 1 ;
test[j].value = value ;
}
}
test[feature_size].index = -1 ;
predict_lable[i] = svm_predict(model , test) ;
}
int yes = 0 ;
for(int i = 0 ; i < test_size ; i++){
// cout<<test_lable[i] <<" , "<<predict_lable[i]<<endl ;
if(test_lable[i] == predict_lable[i]) yes++ ;
}
cout<<yes<<endl ;
printf("%.2lf%%\n" , (0.0+yes)/test_size) ;
in.close() ;
return 0 ;
}
结果
效果不好,没有寻找参数。
待续文。
-----------------------------
问题解决,由于文件单词拼写错误。
--------------------------------------测试数据-----------------------------------------------------------
tain_data.txt
-1 6 2
-1 8 1
-1 9 2
-1 2 1
-1 8 3
-1 5 2
-1 10 2
-1 8 5
-1 2 1
-1 7 6
-1 7 2
-1 8 3
-1 9 8
-1 4 2
-1 4 2
-1 9 8
-1 10 1
-1 10 7
-1 10 2
-1 8 1
-1 9 4
-1 10 1
-1 6 2
-1 8 2
-1 7 5
-1 8 1
-1 5 1
-1 5 2
-1 10 1
-1 6 3
-1 7 6
-1 10 3
-1 8 4
-1 10 5
-1 10 8
-1 7 1
-1 10 2
-1 5 3
-1 9 8
-1 10 1
-1 10 9
-1 7 5
-1 9 7
-1 6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