使用SVM对于许多类型的多维数据分类
最近,我做了一件小事,使用SVM正确8三维级数据分类,在线搜索,我们发现二分的问题大家都在讨论二维数据,一些决定自己的研究。我首先参考opencvtutorial。这也是二维数据的二分类问题。然后通过学习研究,发现别有洞天,遂实现之前的目标。在这里将代码贴出来。这里实现了对三维数据进行三类划分。以供大家相互学习。
#include "stdafx.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp> using namespace cv;
using namespace std; int main()
{ //--------------------- 1. Set up training data randomly ---------------------------------------
Mat trainData(100, 3, CV_32FC1);
Mat labels (100, 1, CV_32FC1); RNG rng(100); // Random value generation class // Generate random points for the class 1
Mat trainClass = trainData.rowRange(0, 40);
// The x coordinate of the points is in [0, 0.4)
Mat c = trainClass.colRange(0, 1);
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * 100));
// The y coordinate of the points is in [0, 0.4)
c = trainClass.colRange(1, 2);
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * 100));
// The z coordinate of the points is in [0, 0.4)
c = trainClass.colRange(2, 3);
rng.fill(c, RNG::UNIFORM, Scalar(1), Scalar(0.4 * 100)); // Generate random points for the class 2
trainClass = trainData.rowRange(60, 100);
// The x coordinate of the points is in [0.6, 1]
c = trainClass.colRange(0, 1);
rng.fill(c, RNG::UNIFORM, Scalar(0.6*100), Scalar(100));
// The y coordinate of the points is in [0.6, 1)
c = trainClass.colRange(1, 2);
rng.fill(c, RNG::UNIFORM, Scalar(0.6*100), Scalar(100));
// The z coordinate of the points is in [0.6, 1]
c = trainClass.colRange(2, 3);
rng.fill(c, RNG::UNIFORM, Scalar(0.6*100), Scalar(100)); // Generate random points for the classes 3
trainClass = trainData.rowRange( 40, 60);
// The x coordinate of the points is in [0.4, 0.6)
c = trainClass.colRange(0,1);
rng.fill(c, RNG::UNIFORM, Scalar(0.4*100), Scalar(0.6*100));
// The y coordinate of the points is in [0.4, 0.6)
c = trainClass.colRange(1,2);
rng.fill(c, RNG::UNIFORM, Scalar(0.4*100), Scalar(0.6*100));
// The z coordinate of the points is in [0.4, 0.6)
c = trainClass.colRange(2,3);
rng.fill(c, RNG::UNIFORM, Scalar(0.4*100), Scalar(0.6*100)); //------------------------- Set up the labels for the classes ---------------------------------
labels.rowRange( 0, 40).setTo(1); // Class 1
labels.rowRange(60, 100).setTo(2); // Class 2
labels.rowRange(40, 60).setTo(3); // Class 3 //------------------------ 2. Set up the support vector machines parameters --------------------
CvSVMParams params;
params.svm_type = SVM::C_SVC;
params.C = 0.1;
params.kernel_type = SVM::LINEAR;
params.term_crit = TermCriteria(CV_TERMCRIT_ITER, (int)1e7, 1e-6); //------------------------ 3. Train the svm ----------------------------------------------------
cout << "Starting training process" << endl;
CvSVM svm;
svm.train(trainData, labels, Mat(), Mat(), params);
cout << "Finished training process" << endl; Mat sampleMat = (Mat_<float>(1,3) << 50, 50,10);
float response = svm.predict(sampleMat);
cout<<response<<endl; sampleMat = (Mat_<float>(1,3) << 50, 50,100);
response = svm.predict(sampleMat);
cout<<response<<endl; sampleMat = (Mat_<float>(1,3) << 50, 50,60);
response = svm.predict(sampleMat);
cout<<response<<endl; waitKey(0);
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
使用SVM对于许多类型的多维数据分类的更多相关文章
- 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件
将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...
- jquery的ajax向后台servlet传递json类型的多维数组
后台运行结果: 前台运行结果: ...
- 关于Delphi中二维数组的声明和大小调整(对非基本类型数据,小心内存泄漏)
这是一个实例: procedure TMainForm.Button1Click(Sender: TObject);var arr:array of array of string;begin s ...
- opencv7-ml之svm
因为<opencv_tutorial>这部分只有两个例子,就先暂时介绍两个例子好了,在refman中ml板块有:统计模型.普通的贝叶斯分类器.KNN.SVM.决策树.boosting.随机 ...
- SVM:从理论到OpenCV实践
(转载请注明出处:http://blog.csdn.net/zhazhiqiang/ 未经允许请勿用于商业用途) 一.理论 参考网友的博客: (1)[理论]支持向量机1: Maximum Marg ...
- libsvm的安装,数据格式,常见错误,grid.py参数选择,c-SVC过程,libsvm参数解释,svm训练数据,libsvm的使用详解,SVM核函数的选择
直接conda install libsvm安装的不完整,缺几个.py文件. 第一种安装方法: 下载:http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm. ...
- 机器学习技法笔记(2)-Linear SVM
从这一节开始学习机器学习技法课程中的SVM, 这一节主要介绍标准形式的SVM: Linear SVM 引入SVM 首先回顾Percentron Learning Algrithm(感知器算法PLA)是 ...
- 核型SVM
(本文内容和图片来自林轩田老师<机器学习技法>) 1. 核技巧引入 如果要用SVM来做非线性的分类,我们采用的方法是将原来的特征空间映射到另一个更高维的空间,在这个更高维的空间做线性的SV ...
- SVM原理与实践
SVM迅速发展和完善,在解决小样本.非线性及高维模式识别问题中表现出许多特有的优势,并能够推广应用到函数拟合等其他机器学习问题中.从此迅速的发展起来,已经在许多领域(生物信息学,文本和手写识别等)都取 ...
随机推荐
- hdu-4418-Time travel-高斯+概率dp
把N个点先转化为2*N-2个点. 比方说把012345转化成0123454321. 这样,就能够找出随意两两个点之间的关系. 然后依据关系能够得出来一个一元多项式的矩阵. 然后就用高斯消元求出矩阵就可 ...
- 2014辽宁省赛 Repeat Number
问题 C: Repeat Number 时间限制: 1 Sec 内存限制: 128 MB [cid=1073&pid=2&langmask=0">提交][状态][论坛 ...
- linux LVS DR模式配置
拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1. 安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...
- Android支付接入(八):Amazon亚马逊支付
下面跟大家一起走一遍Amazon亚马逊的支付,亚马逊目前刚把业务拓展到大陆市场,但这并不代表Amazon支付不成熟,恰恰相反,Amazon的支付流程,支付结果获取及测试另人称赞,支付流程.测试流程简洁 ...
- 启动、停止、重启 MySQL 常见的操作方法:
启动.停止.重启 MySQL 常见的操作方法: 简单罗列 一.启动方式 1.使用 service 启动:service mysqld start 2.使用 mysqld 脚本启动:/etc/inint ...
- H3C TE BGP拓扑排错报告
BGP排错报告 故障一:PPP链路 ...
- Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的
你是否发现用Texturepacker在打包压缩资源文件之后. 把原稿文件弄丢了,可是又要添加新的小png的时候,却无从下手了,本文就是博主在遇到这个问题后百度了非常多方法,可惜仅仅有plist格式的 ...
- alertify、js、css 使用简介
Alertify.js which helped me resolve my issues regarding prompts, alerts, confirms, etc in iOS7. 1.al ...
- 所有城市list每次从页面花1段时间抽取后写入到数组,
所有城市list每次从页面花1段时间抽取后写入到数组,
- HUNNU11354:Is the Name of This Problem
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11354&courseid=0 Problem des ...