Use SVM to classify multi-class Multidimensional Data

Source: Internet
Author: User
Tags svm

Recently, I want to make a little thing. I used SVM to classify 8 types of Three-dimensional data and searched the internet. I found that we all discussed the problem of binary classification of two-dimensional data, so I decided to study it myself. I first referred to the tutorial of opencv, which is also a problem of binary classification of two-dimensional data. Then, through study and research, we found that there were holes in the sky and we achieved our previous goal. The code is posted here. Three types of 3D data are divided for mutual learning.

#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 2labels.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);}



Use SVM to classify multi-class Multidimensional Data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.