一个运用SVM进行回归的例子

#include "cv.h"
#include "highgui.h"
#include "ml.h"
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;


//以下例子用来拟合正弦曲线

int main(int argc, char **argv)
{
   int num_train = 100;
   int num_test = 100;
   CvMat *train_data = cvCreateMat(num_train, 1, CV_32FC1);
   CvMat *train_response = cvCreateMat(num_train, 1, CV_32FC1);
   IplImage *dst=cvCreateImage(cvSize(400,320),8,3);
   cvZero(dst);
   dst->origin=1;

   cvLine( dst, cvPoint(1,160), cvPoint(400,160), cvScalar(255,255,255,0), 3, 8, 0 );
   cvLine( dst, cvPoint(8,1), cvPoint(8,320), cvScalar(255,255,255,0), 3, 8, 0 );


   CvMat *test_data = cvCreateMat(num_test, 1, CV_32FC1);
   CvMat *test_response = cvCreateMat(num_train, 1, CV_32FC1);

   //initilize the training data and testing data
   float *fptr_data = NULL, *fptr_response = NULL;
   const float PI = 3.1415926f;
   int i;
   for(i = 0; i < train_data->rows; i++)
   {
      fptr_data = (float *)(train_data->data.ptr + i * train_data->step); 
      *fptr_data = 2*PI/train_data->rows *i;
      //cout <<*fptr_data<<" ";
      fptr_response = (float *)(train_response->data.ptr + i * train_response->step); 
      *fptr_response = sin(*fptr_data);
      //cout <<*fptr_response<<endl;
   }
   for(i = 0; i < test_data->rows; i++)
   {
      fptr_data = (float *)(test_data->data.ptr + i * test_data->step); 
      *fptr_data = (2*PI/train_data->rows *i+0.5f); 
      fptr_response = (float *)(test_response->data.ptr + i * test_response->step); 
      *fptr_response = sin(*fptr_data); 
   } 

   CvSVM mysvm; 
   CvSVMParams param(103, 2, 2, 1, 1, 10, 1, 0.0001, NULL, 
      cvTermCriteria(CV_TERMCRIT_EPS, 100, 0.0001));
   mysvm.train(train_data, train_response, 0, 0, param); 
   float tmp;
   CvMat *sample = cvCreateMat(1,1, CV_32FC1); 
   cout<<"x\t\t"<<"y\t\t"<<"pre\t\t"<<"err"<<endl;
   
   cvNamedWindow("output",1);

   for(i = 0; i < test_data->rows; i++)
   {
      fptr_data = (float *)(test_data->data.ptr + i * test_data->step);
      *((float *)sample->data.ptr) = *fptr_data;
      tmp = mysvm.predict(sample);
	  float tmp2 = mysvm.predict(sample, false);
      fptr_response = (float *)(test_response->data.ptr + i * test_response->step);
      
      cout<<setprecision(4)<<*fptr_data<< "\t\t";
      cout<<setprecision(4)<<*fptr_response<<"\t\t";
      cout<<setprecision(4)<<tmp<<"\t\t";
      cout<<setprecision(4)<<tmp - *fptr_response<<endl;

      *fptr_data=*fptr_data*180/PI;
      *fptr_response=100*(*fptr_response)+160;
      tmp=100*tmp+160;

      cvCircle( dst , cvPointFrom32f( cvPoint2D32f(*fptr_data,*fptr_response) ) , 
         2, cvScalar(255,0,0,0) , -1 , 8 , 0 );
      cvCircle( dst , cvPointFrom32f( cvPoint2D32f(*fptr_data,tmp) ) , 
         2, cvScalar(0,0,255,0) , -1 , 8 , 0 );

      cvShowImage("output",dst);
      cvWaitKey(20);
   }

   cvWaitKey(0);

   cvReleaseImage(&dst);
   cvReleaseMat(&train_data);
   cvReleaseMat(&test_data);
   cvReleaseMat(&train_response);
   cvReleaseMat(&test_response);
   cvReleaseMat(&sample);
   return 0;
}


一个运用SVM进行回归的例子

你可能感兴趣的:(ITeye)