libsvm工具箱C++下编程实践1
来源:互联网 发布:数据透视分析表怎么用 编辑:程序博客网 时间:2024/06/16 22:09
转载请说明出处 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 1-1 8 6-1 5 1-1 7 1-1 9 3-1 9 1-1 7 5-1 5 1-1 10 9-1 4 1-1 7 5-1 4 2-1 9 4-1 7 2-1 9 5-1 10 6-1 10 4-1 5 1-1 8 2-1 7 4-1 5 3-1 5 3-1 7 2-1 10 6-1 10 7-1 8 2-1 10 3-1 10 4-1 10 4-1 8 7-1 6 1-1 3 2-1 9 3-1 5 1-1 10 9-1 9 2-1 5 4-1 10 4-1 9 7-1 4 3-1 4 1-1 10 7-1 5 1-1 9 3-1 6 1-1 6 1-1 3 1-1 6 1-1 8 2-1 8 7-1 10 6-1 8 5-1 7 1-1 7 6-1 9 2-1 8 5-1 9 21 5 101 5 71 4 101 4 91 9 101 2 41 2 81 3 71 7 81 9 101 1 51 5 91 4 51 4 81 6 71 2 81 2 101 4 51 6 81 1 91 4 61 4 51 1 91 2 41 1 91 5 81 3 71 6 71 4 101 3 71 4 61 4 91 3 61 1 41 5 91 1 61 2 51 4 61 1 91 4 81 9 101 7 91 2 31 4 91 1 41 3 81 4 91 4 81 8 101 9 101 6 81 1 101 2 31 1 91 1 81 8 101 3 71 2 71 1 101 5 71 1 101 4 71 3 51 6 91 2 71 2 51 1 51 1 41 5 61 1 61 3 71 5 91 6 101 1 91 3 101 6 101 2 71 4 71 2 71 6 71 2 81 7 91 1 71 4 81 1 51 4 71 3 61 2 41 4 91 5 91 3 101 7 91 3 71 1 101 4 101 6 81 3 41 6 91 3 71 3 5
test_data.txt
1 2 101 1 7-1 7 31 1 6-1 8 11 4 8-1 9 61 3 10-1 4 21 1 9-1 8 5-1 9 4-1 9 3-1 8 3-1 8 61 1 51 1 31 1 5-1 6 3-1 9 5-1 8 21 2 101 3 6-1 9 81 1 41 5 6-1 9 8-1 8 21 3 61 5 6-1 8 41 6 7-1 9 31 9 101 3 51 1 2-1 10 7-1 8 61 4 6-1 9 3-1 8 21 2 8-1 2 1-1 9 7-1 8 3-1 9 31 1 2-1 8 5-1 7 41 3 101 3 8-1 10 71 3 61 5 6-1 6 21 1 61 1 71 5 6-1 9 31 3 101 4 7-1 8 2-1 6 2-1 7 6-1 9 1-1 10 51 3 7-1 7 2-1 7 2-1 10 41 2 8-1 10 21 3 8-1 7 11 5 10-1 5 4-1 3 1-1 10 11 6 101 8 10-1 7 31 1 81 1 101 3 7-1 2 11 2 7-1 7 4-1 6 51 4 81 2 71 5 71 7 10-1 10 9-1 5 1-1 10 1-1 7 41 3 4-1 9 2-1 8 71 2 6
2 0
- libsvm工具箱C++下编程实践1
- libsvm工具箱C++下编程实践2
- libsvm python Linux Ubuntu下编程操作实践
- MATLAB R2013a下libsvm工具箱的安装
- 在matlab下测试libsvm工具箱
- LibSVM学习1:Libsvm工具箱的下载、安装和使用
- libsvm工具箱在64位matlab下的安装说明
- LibSVM 工具箱...不可分肿么办!
- matlab 安装libsvm工具箱
- Libsvm工具箱的使用
- win编程实践(1)【c++】
- libsvm-mat-加强工具箱介绍
- libsvm-mat-加强工具箱介绍
- 安装Libsvm工具箱常见问题解决方法
- libsvm-mat-加强工具箱介绍
- 机器学习:林智仁libsvm 工具箱 在matlab下的应用总结
- 机器学习:林智仁libsvm 工具箱 在matlab下的应用总结
- 关于libsvm工具箱在64位matlab下的安装说明
- Android写通讯录 用到的数据库知识
- hdu 1241 Oil Deposits
- 剑指offer面试题13扩展:带头指针的单链表的操作
- 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。
- Etherpeek监听UDP报文时间戳获取
- libsvm工具箱C++下编程实践1
- ruby on rails学习(一)
- 三种创建二叉树的方式
- 盘位wi为什止得知蹿
- 大牛很通俗地介绍《信号与系统》
- C++ Primer Plus第六版 第五章 编程练习答案
- js上传图片,同时生成缩略图
- ZOJ3228 Searching the String
- hdu 1372 Knight Moves