?? pnn.cpp
字號:
// PNN.cpp: implementation of the CPNN class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "DNAClassification.h"
#include "PNN.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPNN::CPNN()
{
m_bIsTraining = FALSE;
m_cWeight = NULL;
m_nClass = 0;
m_pWeight = NULL;
m_A =NULL;
m_ParzenWidth = 0.45;
m_nSample = 0;
}
CPNN::~CPNN()
{
Clear();
}
/********************************************************/
/*功 能:PNN訓練算法
/*參 數:sample:是一個第一維大小為n,第二維大小為c的二維數組,樣本已歸一化
/* pClass:n維數組,樣本的分類信息,取值是0,1,2,3
/* n :第一維大小,即樣本個數
/* c :第二維大小,即類別數目
/*返回值:訓練是否成功
/********************************************************/
BOOL CPNN::Training(double **ppSample,int* pClass,int n,int c)
{
if(m_bIsTraining)//已經訓練過
{
//if(c!=m_nClass)//類別數不一致
return FALSE;
}
else //沒有訓練過
{
if(c<=0)
return FALSE;
m_nSample = n;
m_nClass = c;
m_cWeight = new double[c];
m_pWeight = new double*[n];
m_A = new BOOL*[n];
for(int i=0;i<n;i++)
{
m_pWeight[i] = new double[c];
m_A[i] = new BOOL[c];
}
//初始化
for(int w = 0;w<n;w++)
{
for(int h = 0;h<c;h++)
{
m_pWeight[w][h] = 0;
m_A[w][h] = 0;
}
}
//訓練
for(int ii=0;ii<n;ii++)
{
for(int jj=0;jj<c;jj++)
{
m_pWeight[ii][jj] = ppSample[ii][jj];//w(jk)<-x(jk)
}
ASSERT(pClass[ii]<c);
m_A[ii][pClass[ii]] = TRUE;
double dRate = (double)((double)ii/(double)n);
ReportStatus(&dRate);
}
m_bIsTraining = TRUE;
}
return TRUE;
}
/********************************************************/
/*功 能:PNN分類算法
/*參 數:testsample:是一個第一維大小為n,第二維大小為c的二維數組
/* pClass:n維數組,樣本的分類信息,取值是0,1,2,3,用于輸出
/* n :第一維大小,即樣本個數,必須與訓練時所傳參數一致
/* c :第二維大小,即類別數目,必須與訓練時所傳參數一致
/*返回值:是否分類成功
/********************************************************/
BOOL CPNN::Classify(double **ppTestSample,int* pClass,int n,int c)
{
if(m_bIsTraining==FALSE)//還沒有訓練
return FALSE;
double* net = new double[m_nSample];
double* g = new double[c];
memset(g,0,c*sizeof(double));
for(int nSam = 0;nSam<n;nSam++)
{
for(int i=0;i<m_nSample;i++)
{
//MatrixMul(m_pWeight,ppTestSample,1,c,1,c,result);
double result = 0.0;
for(int r = 0;r<c;r++)
{
result += m_pWeight[i][r]*ppTestSample[nSam][r];
}
net[i] =result;
for(int j=0;j<c;j++)
{
if(m_A[i][j] ==TRUE)
{
g[j]+=exp((net[i]-1)/(m_ParzenWidth*m_ParzenWidth));
}
}
}
double max =0;
int nClass = 0;
for(int jj=0;jj<c;jj++)
{
if(g[jj]>max)
{
max = g[jj];
nClass =jj;
}
}
pClass[nSam] = nClass;
memset(g,0,c*sizeof(double));
}
delete[] net;
delete[] g;
return TRUE;
}
/********************************************************/
/*功 能:
/*參 數:
/*返回值:
/********************************************************/
void CPNN::Clear()
{
if(m_cWeight)
{
delete[] m_cWeight;
m_cWeight = NULL;
}
if(m_pWeight)
{
for(int i=0;i<m_nSample;i++)
{
delete[] m_pWeight[i];
m_pWeight[i] = NULL;
}
delete m_pWeight;
m_pWeight = NULL;
}
if(m_A)
{
for(int i=0;i<m_nSample;i++)
{
delete[] m_A[i];
m_A[i] = NULL;
}
delete m_A;
m_A = NULL;
}
m_bIsTraining = FALSE;
m_nClass = 0;
m_nSample = 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -