使用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迅速发展和完善,在解决小样本.非线性及高维模式识别问题中表现出许多特有的优势,并能够推广应用到函数拟合等其他机器学习问题中.从此迅速的发展起来,已经在许多领域(生物信息学,文本和手写识别等)都取 ...
随机推荐
- 14.4.2 Configuring InnoDB for Read-Only Operation 配置InnoDB 永于只读操作:
14.4.2 Configuring InnoDB for Read-Only Operation 配置InnoDB 永于只读操作: 你可以查询InnoDB 表 MySQL 数据目录是在只读介质里,通 ...
- 推荐一个IT人必备的东西【用过的都懂,让我们的环境越来越好吧】
有个东西叫IT人手册,不知道各位有用过吗?不过很可惜以前那个关掉了,那个网站说出了我们IT人太多的新声以及一些黑心公司,不过被迫压力下关闭了 我不是托,我只是分享 我觉得这种东西应该存在下去~!!!至 ...
- 从iReport至Jaspersoft Studio
这篇文章同步到http://www.waylau.com/from-ireport-to-jaspersoft-studio/ 从5.5版本号開始,Jaspersoft Studio将代替iRepor ...
- Java Swing界面编程(31)---菜单条:JMenu
package com.beyole.test; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMe ...
- HTML学习笔记——各种居中对齐
0.前言 水平居中基本方法--指定块的宽度并设定块的左右外边距为auto,上下外边距可取0,那么该块能够在父元素中水平居中. 样式例如以下: 1:margin:0px auto 2:margi ...
- 在word 中复选框划勾或叉的方法
输入大写字母R.大写字母Q ,然后将字体改为Wingdings 2, 就分离得到带框的勾和叉.
- Netflix公司监控内部安全的开源项目
Netfix公司已经公布了三个内部工具,用于捕捉黑客在使用互联网服务时留下的痕迹. AndyHoernecke和Netflix公司的云安全团队成员ScottBehrens指出:"很多安全团队 ...
- namespace命名空间
在讨论如何使用命名空间之前,必须了解 PHP 是如何知道要使用哪一个命名空间中的元素的.可以将 PHP 命名空间与文件系统作一个简单的类比.在文件系统中访问一个文件有三种方式: 相对文件名形式如foo ...
- maven 项目中使用 jstl标签
在pom.xml文件下面增加如下的依赖包: <dependency> <groupId>javax.servlet</groupId> <artifactId ...
- ReactNavtive框架教程(2)
, alignItems: 'center' } }); 标准的 CSS 属性.尽管用CSS比在IB设置UI样式的可视化要差.但总比在viewDidLoad()方法中用代码写要好一些. 然后增加下面代 ...