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

📄 statistic.cpp

📁 自主式决策树学习的程序源码
💻 CPP
字号:
// Statistic.cpp: implementation of the CStatistic class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "dstl.h"
#include "Statistic.h"
#include "Tree.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStatistic::CStatistic()
{

}

CStatistic::~CStatistic()
{

}
/////////////////////////////////////////////////////////////////
extern int AttNumber;
extern int ClassNumber;
extern CDatabase *pDb;
extern CString strTableName;
extern CTree dtTree;

//选择分裂属性
int CStatistic::SelSplit(char arrAttName[],int AttCount,CPtrList myList[],CString strWh)
//参数说明(属性名数组,属性个数,, ,)
{
  CRecordset Rst(pDb);
  
 // AfxMessageBox("functon ok");
  CString strSQL;
  CString strVal;//条件属性名
  CString strClass;//决策属性名
  strClass="ClassName";
  for(int j=0;j<AttCount;j++)
  {

	
//计算某属性的属性值种类数并保存属性值名于一CStringList
	strVal=arrAttName[j];
	if (strWh.IsEmpty())
	{
		strSQL="select distinct " +strVal+" from "+strTableName;
			
	}
	else
	{
		strSQL="select distinct " +strVal+" from "+strTableName
		      +" where "+strWh;
	}
	
	Rst.Open(CRecordset::dynaset,strSQL);
	int ValueNum=0;//属性值个数
	CStringList ValNameList;
    CString strValueName="";
	int index;
	Rst.MoveFirst();
	while (!Rst.IsEOF())
	{   index=0;
		Rst.GetFieldValue(index,strValueName);
		ValNameList.AddTail(strValueName);//把属性值名加到属性值名数组中
		strValueName="";
		ValueNum++;
		Rst.MoveNext();
	}
	Rst.Close();
//统计每属性值的信息
	
   
	for (int i=0;i<ValueNum;i++)
	{
		POSITION pos;
        pos=ValNameList.FindIndex(i);
		strValueName=ValNameList.GetAt(pos);
		if(strWh.IsEmpty())
		{
			strSQL="select "+strVal+", "+strClass+", count(*) as num from "+strTableName
				+" where "+strVal+"="+"'"+strValueName+"'"
				+" group by "+strVal+", "+strClass
				+" order by "+strVal+" ,"+strClass;
		}
		else
		{
			strSQL="select "+strVal+", "+strClass+", count(*) as num from "+strTableName
				+" where "+strVal+"="+"'"+strValueName+"'"+" and "+strWh
				+" group by "+strVal+", "+strClass
				+" order by "+strVal+" ,"+strClass;
		}
        Rst.Open(CRecordset::dynaset,strSQL);
	
		int iTotal=0;
		int iMax=0;
		int iTemp=0;
		int iPos=1;//有最大值的记录位置,该位置从1开始
        //处理某个属性值
		//CPtrList valList;//每属性一个
		Rst.MoveFirst();
		index=0;
		while (!Rst.IsEOF())
		{
			Rst.GetFieldValue(2,strValueName);//获得统计数
            sscanf(strValueName,"%d",&iTemp);
			strValueName="";
			iTotal+=iTemp;//计算取值为该属性值的总记录数
			index+=1;
			if(iMax<iTemp)
			{
				iMax=iTemp;//该值中最大分类占的记录数
				iPos=index;//最大分类占的记录数,标记此记录,从1开始
			};
			Rst.MoveNext();
		}//end while
        
		Rst.SetAbsolutePosition(iPos);//把上面标记的记录作为当前记录
		index=0;
		
		ValNode * myValNode=new(ValNode);
		Rst.GetFieldValue(index,strValueName);//获得属性值名
	
		myValNode->ValueName=strValueName;
		strValueName="";
		
		Rst.GetFieldValue(1,strValueName);//获得分类名
		myValNode->ValueClass=strValueName;
        strValueName="";
		myValNode->CerVote=(float)iMax/iTotal;//获得条件属性对决策分类的确定性
		myValNode->ValMaxNum=iMax;//获得最大分类占的记录数
		myValNode->ValTotalNum=iTotal;//获得取值为该属性值的总记录数
	
		Rst.Close();
        myList[j].AddTail((void*)myValNode);
       
	}//end for 每一属性值
	
  }//end for每一属性	
//寻找整体确定性最大的属性
  
  int index=0;
  int iMax=0;
  ValNode * tempNode;
  iMax=0;
  for(int k=0;k<AttCount;k++)
  {
	  
	  int iTotal=0;
	  
	  int icount=myList[k].GetCount();
	  for (int i=0;i<icount;i++)
	  {
		tempNode=(ValNode*)myList[k].GetAt(myList[k].FindIndex(i));
		iTotal+=tempNode->ValMaxNum;
		CString str=tempNode->ValueName;
		
	  }//end while
	  if(iMax<iTotal)
	  {
		  iMax=iTotal;
		  index=k;//记录整体确定性最大属性的记录号
	  }
  }//end for
//释放其他的CPtrList链
  for(int n=0;n<AttCount;n++)
  {
	if(n!=index)
	{
		
		int m=myList[n].GetCount();
        ValNode * p;
	    for(int l=0;l<m;l++)
		{
		   p=(ValNode*)myList[n].GetHead();
		   myList[n].RemoveHead();
           delete p;
		}
	}
  } 
  return index;

}

///////////////////////////////////////////////////////////////////
//释放CPtrlist 数组
/*
void CStatistic::FreeList(CPtrList list[],int listCount)
{
	
	for (int i=0;i<listCount;i++)
	{
		if (list[i].IsEmpty())
		return;
	    int k=list[i].GetCount();
        ValNode * p;
	    for(int j=0;j<k;j++)
		{
		   p=(ValNode*)list[i].GetHead();
		   list[i].RemoveHead();
           delete p;
		}
	}
}
*/

⌨️ 快捷键说明

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