How to use the LIBSVM Toolbox in Visual Studio (VS) 2012

Source: Internet
Author: User
Tags svm



Original: http://blog.csdn.net/u014691453/article/details/40393137





Software version:


Visual Studio version: VS2012



(Note: The use of the method on the VS2010 above is also available, but the problem may arise is: VS2010 and VS2012 Some of the functions are different, you need to make changes before compiling, such as VS2010 scanf comparison VS2012 of scanf_s)



LIBSVM version: libsvm-3.18





Statement:


If you find the article in my title, that largely means you have a problem using the LIBSVM Toolbox in VS or MFC. You must also know how different the use of LIBSVM in VS engineering is with its use in MATLAB or Python.





Thanks:


If you do not have the help of GG, I am in the VS programming rookie ability, is absolutely impossible 1 days to do well, I will definitely get half a year not only so thank you very much, in the future I must seriously learn programming.





Body:


Divided into three parts: the first part: The framework of the VS project is built first.



Part Two: How to write the CPP file containing the main function in the project



Part III: Porting the LIBSVM Toolbox to MFC for use



Part IV: Data formats that work correctly in the code in this article



Resource address: How to use the LIBSVM Toolbox in Visual Studio (VS) (the code package associated with the blog post)


The first part: Before the framework of the VS project has been in the use of MATLAB in the LIBSVM Toolbox, there has been a period of time in Python used, just contact with the cmd console is also used above, LIBSVM writers really praise, open this toolbox we see Here's the scenario:


For Java, MATLAB, Python, windows have a corresponding folder, the contents of the folder and the Readme in this can help you in the above four cases of use.



How do you use it in VS?



First, we first set up a WIN32 console project, named MM, the following steps:












Then, the files in the LIBSVM folder (such as), are copied to the MM project folder (below), for future use.









Then, in the MM project, we add svm.h and Svm.cpp respectively to the header file and the source file, such as:






Then, in the source file, right--New item: MM.cpp



And then the second part.





Part Two: How to write the CPP file containing the main function in the project MM.cpp that is, the CPP file that contains the main function, which is what we end up doing. First of all, we first write to MM.cpp the need to use the header file, as follows: [CPP]View Plaincopy
    1. #include <stdio.h>
    2. #include <string.h>
    3. #include <ctype.h>
    4. #include <list>
    5. #include <fstream>
    6. #include <iostream>
    7. #include "svm.h"
    8. Using namespace std;

The next section is roughly five parts: first: Read the training data and test data second: construct the structure of the parameter param (the main purpose is to let vs know what parameters you want to use) Third: construct the prob structure of the classification problem (mainly to pass the first reading data to Svm_train and SVM_PREDICT) Fourth: The main function is as follows fifth: Results show first: Read the training data and test data first, before reading the data, define some variables (the whole program or the main reference libsvm/svm_toy/qt/svm_toy.cpp this code, Because of my programming ability is really poor, so some redundant code I have not changed, only to achieve the purpose of using LIBSVM. )

[CPP]View Plaincopy

#define XLEN 10 //Production test data, this feeling is useless
#define YLEN 10
  
  
Ofstream outdata; //All the labels and feature features of all the data that need to be prepared are separated, and the feature feature does not need to be preceded by a colon. Just separate it with spaces.
Ifstream indata; //So here indata contains only features, indata_label only contains label, and each line is a sample.
Ofstream outdata_lable;
Ifstream indata_lable;
  
Int NUM = 1440; // Since my sample feature is 1440 dimensions, when I read the data, I defined the number, and you can change the NUM according to the dimension of the feature.
  
//char default_param[] = "-t 2 -c 100"; // This is the choice of parameters in svm_toy.cpp
Char default_param[] = "-t 2 -c 4 -g 32"; // This is the parameter that matches my data.
Struct point { //This is the way svm_train takes data after training.
    Double *feature;
    //signed char value;
    Int value;
};
  
List<point> point_list; // The data read in by indata is placed in point_list
Int current_value = 1; // passerby A variable
  
Void clear_all() // empty list
{
    Point_list.clear();
}


Then we'll read the training data to train the model[CPP]View Plaincopy

Void readFile1(char *file,char *file_lable) //Define the function that reads the training data is called readFile1
{
  
    Indata.open(file,ios::in); //Read feature
    Indata_lable.open(file_lable,ios::in); // read in the label
    Cout <<"read data begin"<<endl; // screen display
      
    Clear_all();
    While(!indata.eof()) // If you have not finished reading the file, continue reading, knowing to read the entire traindata file
    {
          
        Double *line = new double[NUM];
        For(int i =0;i<NUM;i++)
        {
            Indata >> line[i]; //Save the feature
        }
        Point p;
        Indata_lable >>p.value; //Save the label
  
        P.feature = line;
        Point_list.push_back(p);
    }
      
    Point_list.pop_back(); //If you have a space at the end of the data in the traindata.txt file, you need to add this sentence, otherwise the prediction will be wrong. If there is no space, this sentence is not needed.
    Indata.close();
    Indata_lable.close();
    Cout <<"read data end"<<endl;
}


Next, read in the test data:[CPP]View Plaincopy

Void readFile2(char *file,char *file_lable) // The function that reads the test file is called readFile2
{
  
     Indata.open(file,ios::in); //The next code and the above training data are like this.
     Indata_lable.open(file_lable,ios::in); // <span style="font-family: Arial, Helvetica, sans-serif;">Because the training data is created, the model will be created and then the test data will be read. So don't worry about data coverage issues</span>
     Cout <<"read test data begin"<<endl;
     Clear_all();
     While(!indata.eof())
     {
          
         Double *line = new double[NUM];
         For(int i =0;i<NUM;i++)
         {
             Indata >> line[i];
         }
         Point p;
         Indata_lable >>p.value;
  
         P.feature = line;
         Point_list.push_back(p);
     }
     Point_list.pop_back();
     Cout <<"read test data end size = "<<point_list.size()<<endl;
     Indata.close();
     Indata_lable.close();
}

Second: Construct the structural body of the parameter param

Then, train the model! : Includes building parameters param structure and building prob structure. Because too long, I upload to the CSDN download page, free points download. This code is mainly refer to the Svm_toy.cpp file, students study well. Resource address: How to use the LIBSVM Toolbox in Visual Studio (VS) (the code package associated with the blog post) Third: Constructing the classification problem prob the structure with the second, the code is together, I will put the whole code, including the above write read into feature and label code, Send To download page fourth: The main function is as follows[CPP]View Plaincopy

Int main()
{
     Int choice;
     Cout<<"1 train model\n2 test1\n3 test2\n"<<endl;
     Cin >>choice;
     Switch(choice)
     {
     Case 1:
         {
             readFile1("traindata.txt","trainlabel.txt"); //Select 1 is the training model and the model will be saved as "model.txt"
             Run();
             Break;
         }
     Case 2:
         {
             readFile2("1.txt","1_label.txt"); // Select 2 to test with the first test data
             testData();
             Break;
         }
     Case 3:
         {
             readFile2("11.txt","11_label.txt"); // Select 3 to test with the second test data
             testData();
             Break;
         }
  
     }
     System("pause");
     Return 0;
}

Five: Results show wait ~ training is over ~ Next is the test: the third part: porting the LIBSVM Toolbox to MFC for use in MFC, and the use of LIBSVM in common vs projects is the same. If you don't want to train the model in MFC, then you can completely copy the model that you trained in the second part to this project in MFC, and then load the model into it before testing the data, the specific load method is referenced in my Code Test ()What is written in the function. Then in the MFC code, you can put the second part of the code in the void Run (), which is the training modelPartially removed. Note:I was in the MFC use LIBSVM, found the VS2010 and VS2012 different, svm.cpp and svm.h these two files in some of the functions, can be in the VS2010 of the MFC project run very well, but in the VS2012 will be prompted to say "you Change XX to xx_s will be more safe ", is wrong instead of warning, so if in VS2012 inside with libsvm words, need in svm.cpp and svm.h these two files to modify part of the function of expression form, this on-line has the answer, can self-query. Part IV: The data format in this code can be correctly run in the second part of the resource link, I upload the data included my test data, 1.txt and 1_label.txt is test1 test content, 11.txt and 11_label.txt is Test2 Test the content, if you want to test your own data, remember in Test ()Change the name of your own data into the code. Data formats You can use: you can refer to the 1.txt I uploaded, roughly the following: 0.564 0.436 0.675 0.453 0.000 0.345 0.354 0.345 0.456 0.000 0.346 0.645 0.678 0.678 0.8 66 0.757 0.575 0.867 0.866 0.865 0.856 0.867 0.557 0.754 My 1_label.txt, roughly the following: 1122 as in the format above, because my data is previously processed through MATLAB, so the space position with the is ' \ t '. If your data format is different from mine, there are only two ways: 1: Change it to me. Data format 2: Changes in the code to read the data part of the program, to ensure that the reading of the data is right or there is a way to re-write your code as you need it. Using a precious afternoon to organize these things, I hope to be able to really help people like me in need. Feel that should be said to be finished, if there is an incorrect explanation or missing place, please let me know, I will revise


How to use the LIBSVM Toolbox in Visual Studio (VS) 2012


Related Article

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.