資源簡(jiǎn)介
該程序是svm和模糊的源程序,挺好用的,值得一看哦
代碼片段和文件信息
//?suanfa.cpp?:?Defines?the?entry?point?for?the?DLL?application.
//
#include?
#include?
#include?
#include?
#include?
#include?“svm.h“
#include?“arithmetic.h“
#include?
#include?se.h>
BOOL?APIENTRY?DllMain(?HANDLE?hModule?
???????????????????????DWORD??ul_reason_for_call?
???????????????????????LPVOID?lpReserved
?)
{
????return?TRUE;
}
//**************************************************
//LPSTR?input_file_name;
//LPSTR?model_file_name;
CHAR?input_file_name[100];
CHAR?model_file_name[100];
//***************************************************
#define?Malloc(typen)???(type?*)malloc((n)*sizeof(type))
void?exit_with_help()
{
exit(1);
}
void?parse_command_line(char?*input_file_name?char?*model_file_nameint?c_s);
void?read_problem(const?char?*filename);
void?do_cross_validation(); //交叉驗(yàn)證
struct?svm_parameter?param; //?set?by?parse_command_line
struct?svm_problem?prob; //?set?by?read_problem
struct?svm_model?*model;
struct?svm_node?*x_space;
static?int?cross_validation=0;
int?nr_fold=5;
double?best_C=1best_gamma=1best_accuracy=0;
extern?“C“?_declspec(dllexport)?void?name(LPSTR?in_nameLPSTR?model_name)
{
???//input_file_name=in_name;
???//model_file_name=model_name;
????strcpy(input_file_name?in_name);
strcpy(model_file_name?model_name);
}
extern?“C“?_declspec(dllexport)?int?TrainModel(int?signal)
{??
//初始化c和gamma的起始值,步長(zhǎng)和最大值
????double?begin_C=-4step_C=1end_C=8;
????double?begin_gamma=-10step_gamma=1end_gamma=0;??
const?char?*error_msg;
parse_command_line(input_file_name?model_file_namesignal);??//在該步驟中,把signal付給了cross_validation,并做了一些初始化操作
read_problem(input_file_name);
error_msg?=?svm_check_parameter(&prob¶m);
if(error_msg)
{
fprintf(stderr“Error:?%s\n“error_msg);
exit(1);
}
if(cross_validation)????????//cross_validation是1時(shí)代表進(jìn)行訓(xùn)練,cross_validation是0時(shí)代表
{
??????for(param.C=pow(2begin_C);param.C<=pow(2end_C);param.C*=pow(2step_C))
???????for(param.gamma=pow(2begin_gamma);param.gamma<=pow(2end_gamma);param.gamma*=pow(2step_gamma))
do_cross_validation();
????//c=best_C;g=begin_gamma;
??????? printf(“\n*************\nbest_C?=?%g\n“best_C);
printf(“best_gamma=%g\n“best_gamma);
printf(“best_accuracy?=?%g%%\n“best_accuracy);
}
else
{
model?=?svm_train(&prob¶m);
svm_save_model(model_file_namemodel);
svm_destroy_model(model);
}
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
return?0;
}
void?do_cross_validation()???????????//對(duì)c和gamma的值加以修正
{
int?i;
int?total_correct?=?0;
double?total_error?=?0;
double?sumv?=?0?sumy?=?0?sumvv?=?0?sumyy?=?0?sumvy?=?0;
double?*target?=?Malloc(doubleprob.l);????//分配行數(shù)個(gè)double個(gè)類型的存儲(chǔ)單元
double?accuracy=0;
svm_cross_validation(&prob¶mnr_foldtarget);???//svm的交叉驗(yàn)證
for(i=0;i if(target[i]?==?prob.y[i])
+
評(píng)論
共有 條評(píng)論