recently, I did a small thing, using SVM correctly 83-dimensional data classification, online search, we found that the two-dimensional problem we are discussing two-dimension data, some decided their own research. I first refer to opencvtutorial. This is also the two classification problem of two-dimensional data. then, by studying and discovering Shia, we realized the goal before. Put the code out here. Three types of data are divided into four dimensions. For everyone to learn from each other.
#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 (3, CV_32FC1); Mat labels (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 *));//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 theClass 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 are in [0.4, 0.6) c = Trainclass.colrange (for each); Rng.fill (c, Rng::uniform, scalar (0.4*100), scalar (0.6*100));//The z coordinate of the points are in [0.4, 0.6) c = Trai Nclass.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). Setto (2); Class 2labels.rowrange (+). 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" << E Ndl 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);}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Using SVM for many types of multidimensional data classification