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

📄 bayes.c

📁 bayes数据挖掘;基于贝叶斯定理的分类方法
💻 C
字号:
/*DataSamples
{
	int		XWaverage;
	int		XXaverage;
	int		KY;
	int		Award;
	int		Socialwork;
	int		LJcj;
}*/
int DataSamples [60][7]={
	4,3,1,0,0,1,2,
	2,3,1,0,0,0,2,
	2,3,1,0,0,1,2,
	4,2,1,2,1,0,2,
	3,3,1,0,0,0,3,
	4,3,2,2,1,1,1,
	3,2,1,0,1,1,2,
	4,3,1,0,0,1,2,
	3,2,2,1,0,1,2,
	3,3,1,0,0,1,2,
	3,3,3,2,1,1,1,
	1,2,1,2,0,0,3,
	2,2,1,0,0,1,2,
	2,1,1,0,0,1,2,
	2,2,1,0,1,1,2,
	3,2,3,0,0,1,2,
	3,3,1,2,1,0,2,
	3,3,2,0,1,0,2,
	4,3,2,1,1,1,2,
	3,2,1,2,1,0,2,
	3,1,2,0,0,1,2,
	3,3,1,0,0,1,2,
	4,1,1,0,0,1,2,
	2,2,1,0,0,0,3,
	3,3,2,0,0,0,2,
	3,2,1,0,1,1,2,
	2,2,1,0,0,0,3,
	1,1,1,1,0,0,3,
	3,2,2,2,0,1,2,
	3,2,2,0,0,0,2,
	3,2,2,1,1,1,2,
	2,2,1,0,0,1,2,
	2,1,1,0,0,0,3,
	3,3,1,0,1,1,2,
	4,2,3,2,1,1,1,
	4,3,2,1,0,1,2,
	3,2,1,0,0,0,3,
	3,2,2,1,1,1,2,
	3,3,3,2,1,1,1,
	3,2,2,2,0,1,2,
	4,3,2,0,0,1,2,
	3,2,1,0,1,0,2,
	3,3,2,0,1,1,2,
	4,1,2,1,1,0,2,
	3,3,1,2,1,0,2,
	2,3,3,0,0,1,2,
	3,3,2,0,0,1,2,
	3,2,2,0,0,0,2,
	3,2,1,2,1,0,2,
	3,3,1,0,0,1,2,
	3,3,3,1,1,0,2,
	1,1,1,0,0,0,3,
	3,2,3,0,0,0,2,
	4,3,2,1,0,1,2,
	3,2,1,1,0,1,2,
	2,2,1,0,0,1,2,
	4,2,3,1,0,1,2,
	3,3,2,1,0,1,2,
	4,3,1,1,1,1,2,
	4,2,1,0,0,0,3
};

///////////////////////////////////////////////////////////////////////////
#define NUMLINES ((int) (sizeof DataSamples / sizeof DataSamples [0]))
//#define TESTNUMLINES ((int) (sizeof DateTest / sizeof DateTest [0]))

#include <stdio.h>

void main()
{
	float priorPA,priorPB,priorPC;
	float priorXWA,priorXWB,priorXWC,priorXXA,priorXXB,priorXXC,priorKYA,priorKYB,priorKYC,\
		priorLJA,priorLJB,priorLJC,priorAwardA,priorAwardB,priorAwardC,priorSWA,priorSWB,priorSWC;

	int i,iA,iB,iC;
	int XWaverage,XXaverage,KY,LJcj,Award,Socialwork;
	int iCountA,iCountB,iCountC;

	iA=iB=iC=0;
	for(i = 0; i < NUMLINES; i++ )
	{
		if(1 == DataSamples[i][6])
			iA++;
		else if(2 == DataSamples[i][6])
			iB++;
		else if(3 == DataSamples[i][6])
			iC++;
	}
	priorPA= (float)iA/NUMLINES;
	priorPB= (float)iB/NUMLINES;
	priorPC= (float)iC/NUMLINES;

	scanf ("%d %d %d %d %d %d",&XWaverage, &XXaverage, &KY, &Award, &Socialwork, &LJcj);


	priorXWA=priorXWB=priorXWC=priorXXA=priorXXB=priorXXC=priorKYA=priorKYB=priorKYC=\
		priorLJA=priorLJB=priorLJC=priorAwardA=priorAwardB=priorAwardC=priorSWA=priorSWB=priorSWC=0;
///////////////////////////////////////////////////////////////////////////////////
	iCountA=iCountB=iCountC=0;
	if (1 == XWaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(1 == DataSamples[i][0])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (2 == XWaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(2 == DataSamples[i][0])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (3 == XWaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(3 == DataSamples[i][0])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (4 == XWaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(4 == DataSamples[i][0])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	priorXWA = (float)iCountA/iA;
	priorXWB = (float)iCountB/iB;
	priorXWC = (float)iCountC/iC;

///////////////////////////////////////////////////////////////////////////////////
	iCountA=iCountB=iCountC=0;
	if (1 == XXaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(1 == DataSamples[i][1])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (2 == XXaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(2 == DataSamples[i][1])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (3 == XXaverage)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(3 == DataSamples[i][1])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	priorXXA = (float)iCountA/iA;
	priorXXB = (float)iCountB/iB;
	priorXXC = (float)iCountC/iC;

///////////////////////////////////////////////////////////////////////////////////
	iCountA=iCountB=iCountC=0;
	if (1 == KY)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(1 == DataSamples[i][2])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (2 == KY)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(2 == DataSamples[i][2])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (3 == KY)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(3 == DataSamples[i][2])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	priorKYA = (float)iCountA/iA;
	priorKYB = (float)iCountB/iB;
	priorKYC = (float)iCountC/iC;

///////////////////////////////////////////////////////////////////////////////////
	iCountA=iCountB=iCountC=0;
	if (0 == Award)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(0 == DataSamples[i][3])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (1 == Award)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(1 == DataSamples[i][3])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (2 == Award)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(2 == DataSamples[i][3])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	priorAwardA = (float)iCountA/iA;
	priorAwardB = (float)iCountB/iB;
	priorAwardC = (float)iCountC/iC;

//////////////////////////////////////////////////////////////////////////////////
	iCountA=iCountB=iCountC=0;
	if (0 == Socialwork)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(0 == DataSamples[i][4])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (1 == Socialwork)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(1 == DataSamples[i][4])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	priorSWA = (float)iCountA/iA;
	priorSWB = (float)iCountB/iB;
	priorSWC = (float)iCountC/iC;

//////////////////////////////////////////////////////////////////////////////////
	iCountA=iCountB=iCountC=0;
	if (0 == LJcj)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(0 == DataSamples[i][5])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	else if (1 == LJcj)
	{
		for(i = 0; i < NUMLINES; i++ )
		{
			if(1 == DataSamples[i][5])
				if(DataSamples[i][6] == 1)
					iCountA++;
				else if(DataSamples[i][6] == 2)
					iCountB++;
				else if(DataSamples[i][6] == 3)
					iCountC++;
		}
	}
	priorLJA = (float)iCountA/iA;
	priorLJB = (float)iCountB/iB;
	priorLJC = (float)iCountC/iC;

//////////////////////////////////////////////////////////////////////////////////
	priorPA *= priorXWA * priorXXA * priorKYA * priorAwardA * priorSWA * priorLJA;
	priorPB *= priorXWB * priorXXB * priorKYB * priorAwardB * priorSWB * priorLJB;
	priorPC *= priorXWC * priorXXC * priorKYC * priorAwardC * priorSWC * priorLJC;

	printf("%f,  %f,	%f\n",priorPA, priorPB, priorPC);
}

⌨️ 快捷键说明

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