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

📄 svm_common.cpp

📁 支持向量基预测的小数据量的预测方法。包括数据拟合
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	register long wpos,pos;
	long wnum;
	double weight;
	
	pos=0;
	while(line[pos]) 
	{      /* cut off comments */
		if(line[pos] == '#') 
		{
			line[pos]=0;
		}
		else 
		{
			pos++;
		}
	}
	wpos=0;
	if((sscanf(line,"%ld",label)) == EOF) return(0);
	pos=0;
	while(line[pos]==' ') pos++;
	while(line[pos]>' ') pos++;
	while((sscanf(line+pos,"%ld:%lf",&wnum,&weight) != EOF) && 	(wpos<max_words_doc))
	{
		while(line[pos++]==' ');
		while(line[++pos]>' ');
		if(wnum<=0) 
		{ 
			printe ("Feature numbers must be larger or equal to 1!!!"); 
			sprintf(temstr,"LINE: %s",line);
			printm(temstr);
			return (0);
		 
		}
		if((wpos>0) && ((doc->words[wpos-1]).wnum >= wnum))
		{ 
			printe ("Features must be in increasing order!!!"); 
			sprintf(temstr,"LINE: %s",line);
			printm(temstr);
			return (0);
		 
		}
		(doc->words[wpos]).wnum=wnum;
		(doc->words[wpos]).weight=weight; 
		wpos++;
	}
	(doc->words[wpos]).wnum=0;
	(*numwords)=wpos+1;
	doc->docnum=-1;
	doc->twonorm_sq=sprod_ss(doc->words,doc->words);
	return(1);
}
/* grep through file and count number of lines, 
maximum number of spaces per line, and 
longest line. */
int nol_ll(char *file,long *nol,long *wol,long *ll) 
{
	FILE *fl;
	int ic;
	char c;
	long current_length,current_wol;
	
	if ((fl = fopen (file, "r")) == NULL)
	{ 
		printe (file);   
		return -1;
	}
	current_length=0;
	current_wol=0;
	(*ll)=0;
	(*nol)=1;
	(*wol)=0;
	while((ic=getc(fl)) != EOF) 
	{
		c=(char)ic;
		current_length++;
		if(c == ' ') 
		{
			current_wol++;
		}
		if(c == '\n') 
		{
			(*nol)++;
			if(current_length>(*ll)) 
			{
				(*ll)=current_length;
			}
			if(current_wol>(*wol)) 
			{
				(*wol)=current_wol;
			}
			current_length=0;
			current_wol=0;
		}
	}
	fclose(fl);
}

long minl(long a,long b)
{
	if(a<b)
		return(a);
	else
		return(b);
}

long maxl(long a,long b)
{
	if(a>b)
		return(a);
	else
		return(b);
}

long get_runtime() 
{
	clock_t start;
	start = clock();
	return((long)((double)start*100.0/(double)CLOCKS_PER_SEC));
}


int isnan(double a)
{
	return(_isnan(a));
}

void * my_malloc(long size) 
{
	void *ptr;
	ptr=(void *)malloc(size);
	if(!ptr)
	{ 
		printe ("Out of memory!"); 
		return (NULL);
	}
	return(ptr);
}
//print error on screen

void    printe(char* str)
{
	char err[200]="--error--";
	strcat(err,str);
	theApp.ShowM(err);
}

//print message on screen
void	printm(char* str)
{
	theApp.ShowM(str);
}
//custom kernel function
/////////////////////////////////////////////////////////////////
//chen 2001.09.14
/////////////////////////////////////////////////////////////////

double ktt(int ta,int tb,double pa[],double pb[])
{
	int ya,yb;
	ya=((ta-1)%13)+1;
	yb=((tb-1)%13)+1;
	
	if (ya<13&&yb<13) 
		return pa[ta]*pa[ta+1]*pb[tb]*pb[tb+1];
	else return 0.0;
	
}

double kt(int t,double ta[],double tb[])
{
	
	int x,y;
	double sum=0.0;
	x=((t-1)/16)+1;
	y=((t-1)%16)+1;
	if (x>1) 
		sum+=ta[t]*tb[t]*ta[t-16]*tb[t-16];
	if (x>2) 
		sum+=ta[t]*tb[t]*ta[t-32]*tb[t-32];
	if (x>3) 
		sum+=ta[t]*tb[t]*ta[t-48]*tb[t-48];
	if (x<14) 
		sum+=ta[t]*tb[t]*ta[t-48]*tb[t-48];
	if (x<15) 
		sum+=ta[t]*tb[t]*ta[t-32]*tb[t-32];
	if (x<16)
		sum+=ta[t]*tb[t]*ta[t+16]*tb[t+16];
	if (y>3) 
		sum+=ta[t]*tb[t]*ta[t-3]*tb[t-3];
	if (y>2) 
		sum+=ta[t]*tb[t]*ta[t-2]*tb[t-2];
	if (y>1) 
		sum+=ta[t]*tb[t]*ta[t-1]*tb[t-1];
	if (y<14) 
		sum+=ta[t]*tb[t]*ta[t+3]*tb[t+3];
	if (y<15) 
		sum+=ta[t]*tb[t]*ta[t+2]*tb[t+2];
	if (y<16) 
		sum+=ta[t]*tb[t]*ta[t+1]*tb[t+1];
	return sum;
}

double fi(double* tt)
{
	int x,y;
	double sum=0.0;
	for (int t=1;t<=52;t++)
	{
		x=((t-1)/13)+1;
		y=((t-1)%13)+1;
		if (y<13)
			sum+=tt[t]*tt[t+1];
	}
	return sum;
}
double fs(double ta[])
{
	double sum=0.0;
	int x,y;
	for (int i=1;i<256;i++)
	{
		x=((i-1)/16)+1;
		y=((i-1)%16)+1;
		
		if (x<16)
			sum+=ta[i]*ta[i+16];
		if (y<16) 
			sum+=ta[i]*ta[i+1];
	}
	return sum;
}
double sumofword(DOC* a)
{
	double sum=0.0;
	SVM_WORD* pwa=a->words;
	while (pwa->wnum)
	{
		sum+=pwa->weight;
		pwa++;
	}
	return sum;



}
double custom_kernel(KERNEL_PARM *kernel_parm,DOC *a,DOC*b)
{
	double sum=0;
    SVM_WORD *ai,*bj;
    ai=a->words;
    bj=b->words;
    while (ai->wnum || bj->wnum) 
	{
		if(ai->wnum == bj->wnum) 
		{
			sum+=(fabs(ai->weight-bj->weight))*(fabs(ai->weight-bj->weight));
			ai++;bj++;
		}
		else if ((ai!=0) &&(ai->wnum<bj->wnum || bj->wnum==0))
		{
			sum+=fabs(ai->weight)*fabs(ai->weight);
			ai++;
		}
		else if ((bj!=0) &&(bj->wnum<ai->wnum|| ai->wnum==0))
		{
			sum+=fabs(bj->weight)*fabs(bj->weight);
			bj++;
		}
    }
//   case 1: /* polynomial *///
		//return((CFLOAT)pow(kernel_parm->coef_lin*sprod_ss(a->words,b->words)+kernel_parm->coef_const,(double)kernel_parm->poly_degree)); 
    //case 2: /* radial basis function */
	//	return((CFLOAT)exp(-kernel_parm->rbf_gamma*(a->twonorm_sq-2*sprod_ss(a->words,b->words)+b->twonorm_sq)));
    //case 3: /* sigmoid neural net */
	//	return((CFLOAT)tanh(kernel_parm->coef_lin*sprod_ss(a->words,b->words)+kernel_parm->coef_const)); 
    //case 4: /* custom-kernel supplied in file kernel.h*/
	//	return((CFLOAT)custom_kernel(kernel_parm,a,b)); 

/*	
    SVM_WORD *ai,*bj;
    ai=a->words;
    bj=b->words;
	double suma=0.0,sumb=0.0;

    while (ai->wnum ) 
	{
		suma+=ai->weight;
		ai++;
	}
    while (bj->wnum ) 
	{
		sumb+=bj->weight;
		bj++;
	}
	*/
//	double K_rbf=exp(-0.001*(a->twonorm_sq-2*sprod_ss(a->words,b->words)+b->twonorm_sq));
	double K_Laplace=exp(-0.0001*sum);
	double K_poly=pow(sprod_ss(a->words,b->words)+20,3);
//	double K_linear=sprod_ss(a->words,b->words);
	//double sum;
	//double sum=suma*sumb;
	//sum=K_rbf*K_poly;
	//double sum=fabs(pro*pro+pro-tan(pro));
	return K_Laplace*K_poly;

}
void  SetInitParam()
{
		com_param.biased_Hyperplane=1;
		com_param.remove_inconsitant=0;
		com_param.C                =0.0;
		com_param.cost_factor      =1.0;
		com_param.loo              =0;
		com_param.search_depth     = 0;
		com_param.rho           = 	1.0;
		com_param.fraction        =1.0;
		com_param.rbf_gamma        =1.0;
		com_param.poly_c           =0.0;
		com_param.poly_s           =1.0;
		com_param.poly_degree      =1;
		com_param.kernel_type      =0;
		//com_param.user_u   =            pp4.m_strU  ;                            
		com_param.epsion           =0.001;
		com_param.iteration_time   =100;
		com_param.cache_size       =40;
		com_param.new_variable     =com_param.maximum_size;
		com_param.maximum_size     =10;
		com_param.final_test       = 1;

		com_param.blWriteModel=TRUE;
		com_param.Running=FALSE;
		com_param.Finished=TRUE;
		com_param.Close=FALSE;


		//prompt default values
		com_pro.show_action=TRUE;
		com_pro.show_compute_1=TRUE;
		com_pro.show_compute_2=FALSE;
		com_pro.show_compute_3=FALSE;
		com_pro.show_other=FALSE;
		com_pro.show_readfile=FALSE;
		com_pro.show_writefile=FALSE;
		com_pro.show_testresult=TRUE;
		com_pro.show_trainresult=FALSE;
}

⌨️ 快捷键说明

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