使用SVM对多类多维数据进行分类
2014-10-05 10:20
741 查看
最近,本人要做个小东西,使用SVM对8类三维数据进行分类,搜索网上,发现大伙讨论的都是二维数据的二分类问题,遂决定自己研究一番。本人首先参考了opencv的tutorial,这也是二维数据的二分类问题。然后通过学习研究,发现别有洞天,遂实现之前的目标。在这里将代码贴出来,这里实现了对三维数据进行三类划分,以供大家相互学习。
#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对多类多维数据进行分类
- 使用Orange进行数据挖掘之分类(4)------SVM
- opencv用SVM进行多类分类
- SQL笔记(8)_第八章 使用操作符对数据进行分类
- 在R中使用支持向量机(SVM)进行数据挖掘(下)
- 使用不同的SVM对iris数据集进行分类并绘出结果
- BeJavaGod - 如何正确使用数据字典进行分类统一操作(一)
- 使用Weka进行数据挖掘(Weka教程八)Weka分类评价Evaluation输出分析
- 使用sql语句进行数据分类汇总
- 使用LDA线性判别分析进行多类的训练分类
- 数据挖掘-oneR算法-Iris数据集分析-使用oneR算法进行分类预测(五)
- SQL使用操作符对数据进行分类
- OpenCV使用Mat数据进行K-近邻分类
- 使用linq 对 DataTable 中的数据进行 查询 与 分类求合
- 使用 NSData 分类实现,对 NSData 数据类型进行 AES 加密
- 在Struts中使用Validator框架按Action分类对数据进行验证
- 使用Orange进行数据挖掘之分类(1)------朴素贝叶斯分类
- 数据挖掘-K-近邻分类器-Iris数据集分析-使用K-近邻分类器进行分类预测(四)
- 分本分类(六):使用LDA+SVM进行文本分类
- 使用LDA线性判别分析进行多类的训练分类