使用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迅速发展和完善,在解决小样本.非线性及高维模式识别问题中表现出许多特有的优势,并能够推广应用到函数拟合等其他机器学习问题中.从此迅速的发展起来,已经在许多领域(生物信息学,文本和手写识别等)都取 ...
随机推荐
- VMware vSphere服务器虚拟化实验十五 vCenter vShield Manager
VMware vSphere服务器虚拟化实验十五 vCenter vShield Manager VMware vShield Manager是专为 VMware vCenter Server 集成 ...
- KFC - About KFC - Quality Assurance
KFC - About KFC - Quality Assurance Restaurant Quality The main attributes for KFC restaurant excell ...
- make 2>&1 | tee log.txt之小析
前言 接触过linux的人,或多或少都会了解一点make 2>&1 | tee log.txt这个命令. 1. make是什么? make是linux下一个非常强大的命令,简单点就是你要 ...
- stm32f103 TIM1初始化--定时器应用
//TIM1 分频 #define TIM1_DIV1 (1-1) #define TIM1_DIV2 (2-1) #define TIM1_DIV4 (4-1) #define TIM1_DIV8 ...
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...
- hdu3829(最大独立集)
传送门:Cat VS Dog 题意:动物园有N只猫,M只狗,P个小孩.每个小孩都有自己喜欢的动物和讨厌的动物,如果他喜欢狗,那么就讨厌猫, 如果他讨厌狗,那么他就喜欢猫.某个小孩能开心,当且仅当他喜欢 ...
- hdu4612(双连通缩点+树的直径)
传送门:Warm up 题意:询问如何加一条边,使得剩下的桥的数目最少,输出数目. 分析:tarjan缩点后,重新建图得到一棵树,树上所有边都为桥,那么找出树的直径两个端点连上,必定减少的桥数量最多, ...
- SE 2014年4月24日
如图配置交换网络 由于网络规模较小,企业将网络划分为了接入层和核心层两层 核心层设备(Sw1 Sw2 Sw3)作为用户的网关设备,提供三层转发功能 接入层设备(SW4 SW5)连接用户,分别划分三vl ...
- Python数据结构-字典
tel={,} tel[ print(tel) print(tel['tom']) del tel['tom'] print(tel) print(tel.keys()) 运行结果: {, , } { ...
- hdu3899(树形dp)
题意:给一树,每个结点有人数,边有权值,表示经过这条边所需时间, 问取某个结点作为开会地点,所有人全部到达此结点最少所需总时间? 分析:val[u]表示以u为根节点的总人数,num[u]表示以u为根节 ...