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

📄 svmprecomputed.cpp

📁 基于支持向量机的分类方法
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	}	return model;}double svm_predict(const svm_model *model, const svm_node *x){	if(model->param.svm_type == ONE_CLASS ||	   model->param.svm_type == EPSILON_SVR ||	   model->param.svm_type == NU_SVR)	{		double *sv_coef = model->sv_coef[0];		double sum = 0;		for(int i=0;i<model->l;i++)			sum += sv_coef[i] * Kernel::k_function(x,model->SV[i],model->param);		sum -= model->rho[0];		if(model->param.svm_type == ONE_CLASS)			return (sum>0)?1:-1;		else			return sum;	}	else	{		int i;		int nr_class = model->nr_class;		int l = model->l;				double *kvalue = Malloc(double,l);		for(i=0;i<l;i++)			kvalue[i] = Kernel::k_function(x,model->SV[i],model->param);		int *start = Malloc(int,nr_class);		start[0] = 0;		for(i=1;i<nr_class;i++)			start[i] = start[i-1]+model->nSV[i-1];		int *vote = Malloc(int,nr_class);		for(i=0;i<nr_class;i++)			vote[i] = 0;		int p=0;		for(i=0;i<nr_class;i++)			for(int j=i+1;j<nr_class;j++)			{				double sum = 0;				int si = start[i];				int sj = start[j];				int ci = model->nSV[i];				int cj = model->nSV[j];								int k;				double *coef1 = model->sv_coef[j-1];				double *coef2 = model->sv_coef[i];				for(k=0;k<ci;k++)					sum += coef1[si+k] * kvalue[si+k];				for(k=0;k<cj;k++)					sum += coef2[sj+k] * kvalue[sj+k];				sum -= model->rho[p++];				if(sum > 0)					++vote[i];				else					++vote[j];			}		int vote_max_idx = 0;		for(i=1;i<nr_class;i++)			if(vote[i] > vote[vote_max_idx])				vote_max_idx = i;		free(kvalue);		free(start);		free(vote);		return model->label[vote_max_idx];	}}const char *svm_type_table[] ={	"c_svc","nu_svc","one_class","epsilon_svr","nu_svr",NULL};const char *kernel_type_table[]={	"linear","polynomial","rbf","sigmoid","matrix",NULL};int svm_save_model(const char *model_file_name, const svm_model *model){	FILE *fp = fopen(model_file_name,"w");	if(fp==NULL) return -1;	const svm_parameter& param = model->param;	fprintf(fp,"svm_type %s\n", svm_type_table[param.svm_type]);	fprintf(fp,"kernel_type %s\n", kernel_type_table[param.kernel_type]);	if(param.kernel_type == POLY)		fprintf(fp,"degree %g\n", param.degree);	if(param.kernel_type == POLY || param.kernel_type == RBF || param.kernel_type == SIGMOID)		fprintf(fp,"gamma %g\n", param.gamma);	if(param.kernel_type == POLY || param.kernel_type == SIGMOID)		fprintf(fp,"coef0 %g\n", param.coef0);	int nr_class = model->nr_class;	int l = model->l;	fprintf(fp, "nr_class %d\n", nr_class);	fprintf(fp, "total_sv %d\n",l);		{		fprintf(fp, "rho");		for(int i=0;i<nr_class*(nr_class-1)/2;i++)			fprintf(fp," %g",model->rho[i]);		fprintf(fp, "\n");	}		if(model->label)	{		fprintf(fp, "label");		for(int i=0;i<nr_class;i++)			fprintf(fp," %d",model->label[i]);		fprintf(fp, "\n");	}	if(model->nSV)	{		fprintf(fp, "nr_sv");		for(int i=0;i<nr_class;i++)			fprintf(fp," %d",model->nSV[i]);		fprintf(fp, "\n");	}	fprintf(fp, "SV\n");	const double * const *sv_coef = model->sv_coef;	const svm_node * const *SV = model->SV;	//when kernel_type is MATRX, saves the real index of SV only	//otherwise, remain the same	if(param.kernel_type == MATRIX)	{		for(int i=0;i<l;i++)		{			for(int j=0;j<nr_class-1;j++)				fprintf(fp, "%.16g ",sv_coef[j][i]);			const svm_node *p = SV[i];			fprintf(fp,"0:%d\n",(int)((p->value)-1));		}			}else	{		for(int i=0;i<l;i++)		{			for(int j=0;j<nr_class-1;j++)				fprintf(fp, "%.16g ",sv_coef[j][i]);			const svm_node *p = SV[i];			while(p->index != -1)			{				fprintf(fp,"%d:%.8g ",p->index,p->value);				p++;			}			fprintf(fp, "\n");		}	}	fclose(fp);	return 0;}svm_model *svm_load_model(const char *model_file_name){	FILE *fp = fopen(model_file_name,"rb");	if(fp==NULL) return NULL;		// read parameters	svm_model *model = Malloc(svm_model,1);	svm_parameter& param = model->param;	model->rho = NULL;	model->label = NULL;	model->nSV = NULL;	char cmd[81];	while(1)	{		fscanf(fp,"%80s",cmd);		if(strcmp(cmd,"svm_type")==0)		{			fscanf(fp,"%80s",cmd);			int i;			for(i=0;svm_type_table[i];i++)			{				if(strcmp(svm_type_table[i],cmd)==0)				{					param.svm_type=i;					break;				}			}			if(svm_type_table[i] == NULL)			{				fprintf(stderr,"unknown svm type.\n");				free(model->rho);				free(model->label);				free(model->nSV);				free(model);				return NULL;			}		}		else if(strcmp(cmd,"kernel_type")==0)		{					fscanf(fp,"%80s",cmd);			int i;			for(i=0;kernel_type_table[i];i++)			{				if(strcmp(kernel_type_table[i],cmd)==0)				{					param.kernel_type=i;					break;				}			}			if(kernel_type_table[i] == NULL)			{				fprintf(stderr,"unknown kernel function.\n");				free(model->rho);				free(model->label);				free(model->nSV);				free(model);				return NULL;			}		}		else if(strcmp(cmd,"degree")==0)			fscanf(fp,"%lf",&param.degree);		else if(strcmp(cmd,"gamma")==0)			fscanf(fp,"%lf",&param.gamma);		else if(strcmp(cmd,"coef0")==0)			fscanf(fp,"%lf",&param.coef0);		else if(strcmp(cmd,"nr_class")==0)			fscanf(fp,"%d",&model->nr_class);		else if(strcmp(cmd,"total_sv")==0)			fscanf(fp,"%d",&model->l);		else if(strcmp(cmd,"rho")==0)		{			int n = model->nr_class * (model->nr_class-1)/2;			model->rho = Malloc(double,n);			for(int i=0;i<n;i++)				fscanf(fp,"%lf",&model->rho[i]);		}		else if(strcmp(cmd,"label")==0)		{			int n = model->nr_class;			model->label = Malloc(int,n);			for(int i=0;i<n;i++)				fscanf(fp,"%d",&model->label[i]);		}		else if(strcmp(cmd,"nr_sv")==0)		{			int n = model->nr_class;			model->nSV = Malloc(int,n);			for(int i=0;i<n;i++)				fscanf(fp,"%d",&model->nSV[i]);		}		else if(strcmp(cmd,"SV")==0)		{			while(1)			{				int c = getc(fp);				if(c==EOF || c=='\n') break;				}			break;		}		else		{			fprintf(stderr,"unknown text in model file\n");			free(model->rho);			free(model->label);			free(model->nSV);			free(model);			return NULL;		}	}	// read sv_coef and SV	int elements = 0;	long pos = ftell(fp);	while(1)	{		int c = fgetc(fp);		switch(c)		{			case '\n':				// count the '-1' element			case ':':				++elements;				break;			case EOF:				goto out;			default:				;		}	}out:	fseek(fp,pos,SEEK_SET);	int m = model->nr_class - 1;	int l = model->l;	model->sv_coef = Malloc(double *,m);	int i;	for(i=0;i<m;i++)		model->sv_coef[i] = Malloc(double,l);	model->SV = Malloc(svm_node*,l);	svm_node *x_space = Malloc(svm_node,elements);	int j=0;	for(i=0;i<l;i++)	{		model->SV[i] = &x_space[j];		for(int k=0;k<m;k++)			fscanf(fp,"%lf",&model->sv_coef[k][i]);		while(1)		{			int c;			do {				c = getc(fp);				if(c=='\n') goto out2;			} while(isspace(c));			ungetc(c,fp);			fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value));			++j;		}	out2:		x_space[j++].index = -1;	}	fclose(fp);	model->free_sv = 1;	// XXX	return model;}svm_model *svm_read_model(double ALPHAY[],double SVs[],double BIAS[],double NSV[],double NLABEL[], double params[], int n, int l){  int i,t,j;  svm_model *model = Malloc(svm_model,1);  svm_parameter& param = model->param;          	  model->l=l;  model->nr_class=n; model->nSV = Malloc(int,n);model->label = Malloc(int,n);for(i=0;i<n;i++)    {      model->nSV[i]=((int)NSV[i]);      model->label[i]=((int)NLABEL[i]);    }model->rho = Malloc(double,n*(n-1)/2);for(i=0;i<n*(n-1)/2;i++)    {      model->rho[i]=BIAS[i];    }model->SV = Malloc(svm_node*,l);svm_node *x_space = Malloc(svm_node,2*l);  j=0; for(i=0;i<l;i++)    {      	model->SV[i] = &x_space[j];	x_space[j].index=0;        x_space[j].value=SVs[i];	++j;        x_space[j++].index = -1;}model->sv_coef = Malloc(double *,n-1);for(i=0;i<n-1;i++)	model->sv_coef[i] = Malloc(double,l);  t=0;   		for(int i=0;i<l;i++)		{			for(int j=0;j<n-1;j++)			  {			  	  model->sv_coef[j][i]=ALPHAY[t];			          t++;			  } 		}		param.svm_type=((int)params[7]);		param.kernel_type=MATRIX;                 param.degree = params[1];		param.gamma = params[2];        // 1/k		param.coef0 = params[3];		param.nu = params[8];		param.cache_size = params[5];		param.C = params[4];		param.eps = params[6];		param.p = params[9];		param.shrinking = ((int)params[10]);		param.nr_weight = 0;		param.weight_label = NULL;		param.weight = NULL;              		  model->free_sv = 1;		  return model;}void svm_copy_model(double ALPHAY[],double SVs[],double BIAS[],double NSV[],double NLABEL[],svm_model* model){  int i,t;  int n,l;  n=model->nr_class;  l=model->l;  for(i=0;i<n;i++)    {      NSV[i]=model->nSV[i];      NLABEL[i]=model->label[i];    }  for(i=0;i<n*(n-1)/2;i++)    {      BIAS[i]=model->rho[i];    }  for(i=0;i<l;i++)    {      SVs[i]=(model->SV[i])->value - 1;    }  t=0;   		for(int i=0;i<l;i++)		{			for(int j=0;j<n-1;j++)			  {			  	  ALPHAY[t]=model->sv_coef[j][i];			          t++;			  } 		}}void svm_destroy_model(svm_model* model){	if(model->free_sv)		free((void *)(model->SV[0]));	for(int i=0;i<model->nr_class-1;i++)		free(model->sv_coef[i]);	free(model->SV);	free(model->sv_coef);	free(model->rho);	free(model->label);	free(model->nSV);	free(model);}const char *svm_check_parameter(const svm_problem *prob, const svm_parameter *param){	// svm_type	int svm_type = param->svm_type;	if(svm_type != C_SVC &&	   svm_type != NU_SVC &&	   svm_type != ONE_CLASS &&	   svm_type != EPSILON_SVR &&	   svm_type != NU_SVR)		return "unknown svm type";		// kernel_type		int kernel_type = param->kernel_type;	if(kernel_type != LINEAR &&	   kernel_type != POLY &&	   kernel_type != RBF &&	   kernel_type != SIGMOID &&	   kernel_type != MATRIX)		return "unknown kernel type";	// cache_size,eps,C,nu,p,shrinking	if(param->cache_size <= 0)		return "cache_size <= 0";	if(param->eps <= 0)		return "eps <= 0";	if(svm_type == C_SVC ||	   svm_type == EPSILON_SVR ||	   svm_type == NU_SVR)		if(param->C <= 0)			return "C <= 0";	if(svm_type == NU_SVC ||	   svm_type == ONE_CLASS ||	   svm_type == NU_SVR)		if(param->nu < 0 || param->nu > 1)			return "nu < 0 or nu > 1";	if(svm_type == EPSILON_SVR)		if(param->p < 0)			return "p < 0";	if(param->shrinking != 0 &&	   param->shrinking != 1)		return "shrinking != 0 and shrinking != 1";	// check whether nu-svc is feasible		if(svm_type == NU_SVC)	{		int l = prob->l;		int max_nr_class = 16;		int nr_class = 0;		int *label = Malloc(int,max_nr_class);		int *count = Malloc(int,max_nr_class);		int i;		for(i=0;i<l;i++)		{			int this_label = (int)prob->y[i];			int j;			for(j=0;j<nr_class;j++)				if(this_label == label[j])				{					++count[j];					break;				}			if(j == nr_class)			{				if(nr_class == max_nr_class)				{					max_nr_class *= 2;					label = (int *)realloc(label,max_nr_class*sizeof(int));					count = (int *)realloc(count,max_nr_class*sizeof(int));				}				label[nr_class] = this_label;				count[nr_class] = 1;				++nr_class;			}		}			for(i=0;i<nr_class;i++)		{			int n1 = count[i];			for(int j=i+1;j<nr_class;j++)			{				int n2 = count[j];				if(param->nu*(n1+n2)/2 > min(n1,n2))				{					free(label);					free(count);					return "specified nu is infeasible";				}			}		}	}	return NULL;}int kernel_type_matrix(svm_model *model){        if(model->param.kernel_type == MATRIX)                return 1;        else                return 0;}

⌨️ 快捷键说明

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