?? 貝葉斯分類dlg.cpp
字號:
// 貝葉斯分類Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "貝葉斯分類.h"
#include "貝葉斯分類Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
Dall=NULL;
// UpdateData(1);
//m_nage=0;
// UpdateData(0);
//{{AFX_DATA_INIT(CMyDlg)
m_nage = 0;
m_nincome = 0;
m_nstudent = 0;
m_ncredit = 0;
m_strout = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_CBIndex(pDX, IDC_COMBO1, m_nage);
DDX_CBIndex(pDX, IDC_COMBO2, m_nincome);
DDX_CBIndex(pDX, IDC_COMBO3, m_nstudent);
DDX_CBIndex(pDX, IDC_COMBO4, m_ncredit);
DDX_Text(pDX, IDC_EDIT1, m_strout);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMyDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CreateTranningElement();
Output(Dall);
}
void CMyDlg::CreateTranningElement(void)
{
AddToD(YOUTH, HIGH, NO, FAIR, NO);
AddToD(YOUTH, HIGH, NO, EXCELLENT, NO);
AddToD(MIDDLE_AGED, HIGH, NO, FAIR, YES);
AddToD(SENIOR, MEDIUM, NO, FAIR, YES);
AddToD(SENIOR, LOW, YES,FAIR, YES);
AddToD(SENIOR, LOW, YES,EXCELLENT, NO);
AddToD(MIDDLE_AGED, LOW, YES,EXCELLENT, YES);
AddToD(YOUTH, MEDIUM, NO, FAIR, NO);
AddToD(YOUTH, LOW, YES,FAIR, YES);
AddToD(SENIOR, MEDIUM, YES,FAIR, YES);
AddToD(YOUTH, MEDIUM, YES,EXCELLENT, YES);
AddToD(MIDDLE_AGED, MEDIUM, NO, EXCELLENT, YES);
AddToD(MIDDLE_AGED, HIGH, YES,FAIR, YES);
AddToD(SENIOR, MEDIUM, NO, EXCELLENT, NO);
}
void CMyDlg::Output(DNode* D)
{
DNode* p;
p=D;
CString str,output;
output=_T("");
str=_T("");
while(p)
{
for(int i=0;i<ATTRIBUTE_NUM;i++)
{
str.Format (_T("%d\t"),p->attribute[i]);
output+=str;
}
output+=_T("\n");
p=p->next ;
}
MessageBox(output);
}
void CMyDlg::AddToD(int age, int income, int student, int credit_rating, int buys_computer)
{
DNode *newnode=new DNode;
newnode->attribute[0]=age;
newnode->attribute[1]=income;
newnode->attribute[2]=student;
newnode->attribute[3] =credit_rating;
newnode->attribute[4]=buys_computer;
newnode->next =Dall;
Dall=newnode;
}
void CMyDlg::Bayes(DNode *D)//D為訓練元組,n為訓練元組的個數(shù)
{
Presult[0]=0;
Presult[1]=0;
for(int i=0;i<3;i++)//初始化Pcondition
{
for(int j=0;j<4;j++)
{
for(int k=0;k<2;k++)
{
Pcondition[i][j][k]=0;
}
}
}
DNode *p;
p=D;
while(p)//對每一組數(shù)據(jù)處理,計算Pcondition
{
for(int j=0;j<4;j++)
{
Pcondition[p->attribute [j]][j][p->attribute [4]]++;
}
Presult[p->attribute [4]]++;
p=p->next ;
}
}
void CMyDlg::OnButton2()
{
// TODO: Add your control notification handler code here
Bayes(Dall);
float P[2];
P[0]=1;
P[1]=1;
UpdateData(1);
for(int i=0;i<4;i++)
{
if(i==0)
{
P[0]=P[0]*Pcondition[m_nage][i][0]/Presult[0];
P[1]=P[1]*Pcondition[m_nage][i][1]/Presult[1];
}
else if(i==1)
{
P[0]=P[0]*Pcondition[m_nincome][i][0]/Presult[0];
P[1]=P[1]*Pcondition[m_nincome][i][1]/Presult[1];
}
else if(i==2)
{
P[0]=P[0]*Pcondition[m_nstudent][i][0]/Presult[0];
P[1]=P[1]*Pcondition[m_nstudent][i][1]/Presult[1];
}
else if(i==3)
{
P[0]=P[0]*Pcondition[m_ncredit][i][0]/Presult[0];
P[1]=P[1]*Pcondition[m_ncredit][i][1]/Presult[1];
}
}
UpdateData(1);
P[0]=P[0]*Presult[0]/(Presult[0]+Presult[1]);
P[1]=P[1]*Presult[1]/(Presult[0]+Presult[1]);
m_strout.Format("P(X|buys_computer=yes)P(buys_computer=yes)=%f\r\nP(X|buys_computer=no)P(buys_computer=no)=%f\r\n",P[1],P[0]);
if(P[0]>P[1])
{
m_strout+="這個分類為buys_computer=no";
}
else
m_strout+="這個分類為buys_computer=yes";
UpdateData(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -