⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classify.c

📁 留一模型选择法leave-one-out model selection
💻 C
字号:
#include "bsvm.h"#include "bsvm_util.h"#include <ctype.h>/* max vector length in test dataset */ #define MAXINDEX 1000int Nsv;sample_t *SV;/* ax_i = a_i*x_i */double *ax;kernel_param_t kernel_param;double (*kernel_function)(sample_t *, sample_t *);double decision_function(sample_t *testsample){	int i;	double f = 0;	for (i=0;i<Nsv;i++)		f += ax[i]*kernel_function(testsample, &(SV[i]));	return f;}int sign(double x){	if (x < 0)		return -1;	return 1;}void read_model(FILE *fmodel){	int i, j, *nnzl, s_index;	double s_value, s_sumofsq;	char line[MAXLEN], *p;	fscanf(fmodel, "%d", &(kernel_param.ktype));	fscanf(fmodel, "%lf", &(kernel_param.degree));	fscanf(fmodel, "%lf", &(kernel_param.gamma));	fscanf(fmodel, "%lf", &(kernel_param.coef0));	fscanf(fmodel, "%d\n", &Nsv);	nnzl = (int *) xmalloc(sizeof(int)*Nsv);	get_nnz(fmodel, nnzl);	SV = (sample_t *) xmalloc(sizeof(sample_t)*Nsv);	ax = (double *) xmalloc(sizeof(double)*Nsv);	/* skip first five lines */	for (i=0;i<5;i++)		fgets(line, MAXLEN, fmodel);	for (i=0;i<Nsv;i++)		SV[i].v = (feature_t *) xmalloc(sizeof(feature_t)*(nnzl[i]+1));		for (i=0;i<Nsv;i++)	{		fgets(line, MAXLEN, fmodel);		p = line;		sscanf(p, "%lf", &(ax[i]));		while(isspace(*p))                        ++p;                while(!isspace(*p))                        ++p;		for (j=0,s_sumofsq=0;sscanf(p, "%d:%lf", &s_index, &s_value)==2;j++)		{                        s_sumofsq += s_value*s_value;                        SV[i].v[j].num = s_index;                        SV[i].v[j].value = s_value;			while(*p!=':') 				++p;                                ++p;                        while(isspace(*p)) 				++p;                        while(*p && !isspace(*p)) 				++p;		}		SV[i].v[j].num = -1;	      	SV[i].twonorm_sq = s_sumofsq;	}	printf("%d support vectors read\n", Nsv);	free(nnzl);}int main(int argc, char **argv){	FILE *fmodel, *ftest, *fpred;	int N = 0, j, mis = 0, s_index;	double s_value, f, s_sumofsq;	sample_t testsample;	char *p, line[MAXLEN];	if (argc < 3)		myerror("Usage: classify test_file model_file output_file");	ftest = fopen(argv[1], "r");	fmodel = fopen(argv[2], "r");	fpred = fopen(argv[3], "w");	if (!ftest || !fmodel || !fpred)		myerror("Cannot open file");	testsample.v = (feature_t *) xmalloc(sizeof(feature_t)*MAXINDEX);	read_model(fmodel);        switch(kernel_param.ktype)        {        case LINEAR_KERNEL:                kernel_function = kernel_linear;                break;        case POLY_KERNEL:                kernel_function = kernel_poly;                break;        case RBF_KERNEL:                kernel_function = kernel_rbf;                break;        case TANH_KERNEL:                kernel_function = kernel_tanh;                break;        default:                myerror("Unknown kernel function");        }		while (fgets(line, MAXLEN, ftest) != NULL)	{		p = line;		sscanf(p, "%d", &(testsample.a));		while(isspace(*p))                        ++p;                while(!isspace(*p))                        ++p;		for (j=0,s_sumofsq=0;sscanf(p, "%d:%lf", &s_index, &s_value)==2;j++)		{                        s_sumofsq += s_value*s_value;                        testsample.v[j].num = s_index;                        testsample.v[j].value = s_value;			while(*p!=':') 				++p;                                ++p;                        while(isspace(*p)) 				++p;                        while(*p && !isspace(*p)) 				++p;		}		testsample.v[j].num = -1;	      	testsample.twonorm_sq = s_sumofsq;	      	f = decision_function(&testsample);	      	if (sign(f) != testsample.a)	      		mis++;	      	fprintf(fpred, "%lf\n", f);		N++;		}	fclose(fmodel);	fclose(ftest);	fclose(fpred);	free(testsample.v);	free(ax);	for (j=0;j<Nsv;j++)		free(SV[j].v);	free(SV);	printf("%d incorrect %d total, accuracy on test set: %.2f %%\n",	mis, N, (float) 100-100.0*mis/N);	printf("user time = %lf seconds\n", get_utime());	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -