📄 fisherview.cpp
字号:
// FisherView.cpp : implementation of the CFisherView class
//
#include "stdafx.h"
#include "Fisher.h"
#include "math.h"
#include "FisherDoc.h"
#include "FisherView.h"
#include "Matrix.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFisherView
IMPLEMENT_DYNCREATE(CFisherView, CView)
BEGIN_MESSAGE_MAP(CFisherView, CView)
//{{AFX_MSG_MAP(CFisherView)
ON_COMMAND(ID_TRAINGING, OnTrainging)
ON_COMMAND(ID_TESTDATA, OnTestdata)
ON_COMMAND(ID_ANOTHERTRAINING, OnAnothertraining)
ON_COMMAND(ID_ANOTHERTESTDATA, OnAnothertestdata)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFisherView construction/destruction
CFisherView::CFisherView()
{
// TODO: add construction code here
}
CFisherView::~CFisherView()
{
}
BOOL CFisherView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CFisherView drawing
//显示结果,每个类的错误率,总的错误率
void CFisherView::OnDraw(CDC* pDC)
{
CFisherDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int i,j;
for(i=0; i<vPr.size(); i++)
if(vPr[i].testType)
pDC->SetPixel(CPoint(vPr[i].a[0],vPr[i].a[1]),RGB(255,0,0));
else
pDC->SetPixel(CPoint(vPr[i].a[0],vPr[i].a[1]),RGB(0,0,255));
// TODO: add draw code for native data here
CString otStr;
int x=30, y=200, k=20;
for(i=0; i<vPr.size(); i++)
if(vPr[i].testType!=vPr[i].trueType)
{
otStr = "";
otStr.Format("身高 %.2f,体重 %.2f, 真实类别%d",vPr[i].a[0],vPr[i].a[1],vPr[i].trueType);
pDC->TextOut(x,y,otStr);
y+=k;
}
int boyNum = 0;
int girlNum = 0;
for(i=0; i<vPr.size(); i++)
if(vPr[i].trueType == 1)
boyNum++;
else
girlNum++;
otStr = "";
if(vPr.size()>0)
otStr.Format("类1错误率:%.2f,类2错误率:%.2f,总错误率:%.2f",errBoy*1.0/boyNum,errGrl*1.0/girlNum,errorCnt*1.0/vPr.size());
pDC->TextOut(x,y,otStr);
}
/////////////////////////////////////////////////////////////////////////////
// CFisherView printing
BOOL CFisherView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CFisherView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CFisherView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CFisherView diagnostics
#ifdef _DEBUG
void CFisherView::AssertValid() const
{
CView::AssertValid();
}
void CFisherView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CFisherDoc* CFisherView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFisherDoc)));
return (CFisherDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFisherView message handlers
//男女同学数据训练
void CFisherView::OnTrainging()
{
// TODO: Add your command handler code here
boy.num = 0;
boy.type = 1;
girl.num = 0;
girl.type = 0;
if(!LoadTrainingData())
return;
ChangeData();
GetMeanValue(boy);
GetMeanValue(girl);
GetInnerDiffer(boy);
GetInnerDiffer(girl);
GetTotalDiffer();
if(!GetReveMatrix())
{
AfxMessageBox("没有逆矩阵");
exit(0);
}
GetProjectVector();
GetParaValue();
}
//男女同学数据测试
void CFisherView::OnTestdata()
{
// TODO: Add your command handler code here
if(!LoadTestData())
return;
ChangeTestData();
GetClassLabel();
GetEvalueValue();
LookAtData();
Invalidate();
}
//载入男女同学的训练数据
bool CFisherView::LoadTrainingData()
{
int pathlen;
char curPath[100];
CString filePath = "";
GetCurrentDirectory(pathlen,curPath); //保存当前目录
CFileDialog dlg(true);
if(dlg.DoModal() == IDOK);
filePath = dlg.GetPathName(); //系统的当前目录变了
SetCurrentDirectory(curPath);
if(filePath == "")
return false;
FILE * fp;
if((fp=fopen(filePath,"r")) == NULL)
return false;
char ch;
int tabNum = 0;
ch =fgetc(fp);
CString item = "";
bool sex = true;
while(ch != '\n')
ch = fgetc(fp);
ch = fgetc(fp);
while(ch != '#')
{
if(ch == '\n')
{
if(sex)
{
boy.a[boy.num][1] = atof(item);
boy.num++;
}
else
{
girl.a[girl.num][1] = atof(item);
girl.num++;
}
item = "";
tabNum = 0;
}
else if(ch == ' ')
{
ch = fgetc(fp);
while(ch == ' ')
ch = fgetc(fp);
continue;
}
else
{
if(ch == '\t')
{
if(tabNum == 1)
{
if(item == "+1")
sex = true;
else
sex = false;
}
else if(tabNum == 2)
{
if(sex)
boy.a[boy.num][0] =atof(item);
else
girl.a[girl.num][0] =atof(item);
}
item = "";
tabNum++;
}
else
{
item += ch;
}
}
ch = fgetc(fp);
}
fclose(fp);
return true;
}
//载入男女同学测试数据
bool CFisherView::LoadTestData()
{
int pathlen;
char curPath[100];
CString filePath = "";
GetCurrentDirectory(pathlen,curPath); //保存当前目录
CFileDialog dlg(true);
if(dlg.DoModal() == IDOK);
filePath = dlg.GetPathName(); //系统的当前目录变了
SetCurrentDirectory(curPath);
if(filePath == "")
return false;
FILE * fp;
if((fp=fopen(filePath,"r")) == NULL)
return false;
char ch;
int tabNum = 0;
CString item = "";
TestPer perNode;
if(!vPr.empty())
vPr.clear();
ch = fgetc(fp);
while(ch != '\n')
ch = fgetc(fp);
ch = fgetc(fp);
while(ch != '#')
{
if(ch == '\n')
{
perNode.a[1] = atof(item);
vPr.push_back(perNode);
item = "";
tabNum = 0;
}
else if(ch == ' ')
{
ch = fgetc(fp);
while(ch == ' ')
ch = fgetc(fp);
continue;
}
else
{
if( ch == '\t')
{
if(tabNum == 1)
{
if(item == "+1")
perNode.trueType = 1;
else
perNode.trueType = 0;
}
else if(tabNum == 2)
{
perNode.a[0] = atof(item);
}
item = "";
tabNum++;
}
else
{
item += ch;
}
}
ch = fgetc(fp);
}
fclose(fp);
return true;
}
//察看测试结果
void CFisherView::LookAtData()
{
TestPer per[150];
int k = 0;
for(int i =0; i<vPr.size(); i++)
per[k++] = vPr[i];
k = k;
}
//数据归一化处理
void CFisherView::ChangeData()
{
int i,j;
float sum = 0;
for(j=0; j<ITEMNUM; j++)
{
avg[j] = 0;
square[j] = 0;
sum = 0;
for(i=0; i<boy.num; i++)
sum += boy.a[i][j];
for(i=0; i<girl.num; i++)
sum += girl.a[i][j];
avg[j] = sum/(boy.num + girl.num);
for(i=0; i<boy.num; i++)
square[j] += pow(boy.a[i][j]-avg[j],2);
for(i=0; i<girl.num; i++)
square[j] += pow(girl.a[i][j]-avg[j],2);
square[j] = square[j]/(boy.num + girl.num - 1);
square[j] = sqrt(square[j]);
for(i=0; i<boy.num; i++)
if(square[j]>0.01||square[j]<-0.01)
boy.a[i][j] = (boy.a[i][j] - avg[j])/square[j];
for(i=0; i<girl.num; i++)
if(square[j]>0.01||square[j]<-0.01)
girl.a[i][j] = (girl.a[i][j] - avg[j])/square[j];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -