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

📄 classinfo.c

📁 C语言的朴素贝叶斯分类器代码
💻 C
字号:
#include "classInfo.h"

void ClassInfo::print(int n_Inst)
{
#ifdef DBG
	cout << "ClassInfo" << endl;
	cout << "P(" << des << ")= " ;
#endif
	cout <<"\t"<< count << "/" << n_Inst;

//	cout << "\t" <<(float)count/n_Inst;

#ifdef DBG
	cout << "Label: " << label << endl;
#endif
}

// This is to search the list of particular hashed place
// If any description found is the same, simple return that
// pointer, otherwise NULL.

// ClassInfoTable is designed specially to cater the class
// label.
ClassInfo *ClassInfoTable::searchInfo(int hashVal, Description v)
{
	for(ClassInfo *tmp=table[hashVal]; tmp; tmp=tmp->next)
		if(v==tmp->des) return tmp;
	return NULL;
}
	
void ClassInfoTable::cleanHashTable()
{
	for(int r=0; r<MAX_HASH_SIZE; r++)
		table[r] = NULL;
}

ClassInfoTable::ClassInfoTable(SupervisedDataTable &tbl)
{
	int row = tbl.numOfInstances();
	int col = tbl.numOfAttributes();
	n_Inst= tbl.numOfInstances();

	cleanHashTable();
	n_Classes = 0;
	for(int r=0; r<row; r++)
	{
		Description currDescription = tbl.value(r, col);
#ifdef DBG
	cout << "currDescrp="<<currDescription<<endl;
#endif
		int hashValue = hash(currDescription);
		ClassInfo *tmp = searchInfo(hashValue, currDescription);

		if(tmp==NULL) { // not found, new distinct value
			tmp = new ClassInfo(currDescription, n_Classes);
			tmp->next = table[hashValue];
			table[hashValue] = tmp;
			n_Classes++; // count the number of distinct classes
		}
		else
			tmp->inc();
	}
#ifdef DBG
	cout << "ClassInfoTable::row = " << row << ", col = "<<col << endl;
#endif
}

int ClassInfoTable::classLabel(Description cd)
{
	int hashValue = hash(cd);
	ClassInfo *tmp = searchInfo(hashValue, cd);
	ERROR1(tmp, "Error: classLabel never seen");

	return tmp->label;
}

int ClassInfoTable::getCount(Description d)
{
	ClassInfo *tmp = searchInfo(hash(d), d);
	ERROR2(tmp,
		"ClassInfoTable::getCount:Description d=%d never occurs\n", d);
	return tmp->getCount();
}

void ClassInfoTable::print()
{
#ifdef DBG1
	cout << "---- Class Info Table ----" << endl;
#endif
	cout << "P(Ci)" ;
	for(int r=0; r<MAX_HASH_SIZE; r++)
	{
		if(table[r]) {
#ifdef DBG1
			cout << "[" << r <<"] " ;
#endif
			for(ClassInfo *tmp=table[r]; tmp; tmp=tmp->next)
				tmp->print(n_Inst);
		}
	}
	cout << endl;
}

ClassSummary::ClassSummary(ClassInfoTable &cTbl)
{
	n_Classes = cTbl.numOfClasses();
	n_Instances = cTbl.numOfInstances();

	count = new int[n_Classes];
	table = new Description[n_Classes];

	for(int i=0; i<MAX_HASH_SIZE; i++)
	{
		for(ClassInfo *tmp=cTbl.getRow(i); tmp; tmp=tmp->next)
		{
			count[tmp->getLabel()] = tmp->getCount();
			table[tmp->getLabel()] = tmp->getDescription();
		}
	}
}

Description ClassSummary::getMaxClassValue()
{
	Description max = table[0];
	for(int i=1; i<n_Classes; i++)
		if(max<table[i]) max = table[i];
	return max;
}

void ClassSummary::printClassSummary()
{
	cout << "P(Ci)";
	for(int i=0; i<n_Classes; i++)
		cout << "\t" << count[i]<< "/" <<n_Instances;
	cout << endl;
}

⌨️ 快捷键说明

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