资源简介
Em算法实现聚类(VC++实现),包含main函数和Em类的控制台程序,带有详细注释。解决高斯混合模型聚类问题。
代码片段和文件信息
#include “em.h“
#include “memory.h“
#include “stdlib.h“
#include “stdio.h“
#include “math.h“
#include “time.h“
namespace ker
{
#define PI 3.1415926
//一个很小的数,用来解决协方差矩阵行列式为0以及高斯密度分布函数为0的问题
#define CONST_E 0.000000000001
cEm::cEm(int nVec int nDim int nPat)
{
_nVec = nVec;
_nDim = nDim;
_nPat = nPat;
//_pplfZ[i][j]表示第i个向量属于第j类的概率初始化为0
_pplfZ = new double *[_nVec];
for (int i = 0; i < _nVec; i++)
{
_pplfZ[i] = new double[_nPat];
memset(_pplfZ[i] 0 sizeof(double) * _nPat);
}
//_pplfU[i]表示第i类的均值向量,其初始化的值有可能导致陷入局部极值
//因此其初始化的值根据待分类数据来给出
_pplfU = new double *[_nPat];
for (i = 0; i < _nPat; i++)
{
_pplfU[i] = new double[_nDim];
//for (int j = 0; j < _nDim; j++) _pplfU[i][j] = i * 2;
}
//_ppplfDelta[i]表示第i类的协方差矩阵并把协方差矩阵初始化成单位阵
_ppplfDelta = new double **[_nPat];
for (i = 0; i < _nPat; i++)
{
_ppplfDelta[i] = new double *[_nDim];
for (int j = 0; j < _nDim; j++)
{
_ppplfDelta[i][j] = new double[_nDim];
memset(_ppplfDelta[i][j] 0 sizeof(double) * _nDim);
_ppplfDelta[i][j][j] = 1;
}
}
//_plfPi[i]表示第i类的先验概率初始化为1 / _nPat
_plfPi = new double [_nPat];
for (i = 0; i < _nPat; i++) _plfPi[i] = 1.0 / _nPat;
}
cEm::~cEm()
{
for (int i = 0; i < _nVec; i++) delete []_pplfZ[i];
delete []_pplfZ;
for (i = 0; i < _nPat; i++) delete[]_pplfU[i];
delete []_pplfU;
for (i = 0; i < _nPat; i++)
{
for (int j = 0; j < _nDim; j++) delete[]_ppplfDelta[i][j];
delete []_ppplfDelta[i];
}
delete []_ppplfDelta;
delete []_plfPi;
}
double **cEm::Inverse(double **pplfMatSrc)
{
//复制原矩阵
double **pplfMat = new double *[_nDim];
for (int i = 0; i < _nDim; i++)
{
pplfMat[i] = new double [_nDim];
memcpy(pplfMat[i] pplfMatSrc[i] sizeof(double) * _nDim);
}
//创建一个单位阵
double **pplfI = new double *[_nDim];
for (i = 0; i < _nDim; i++)
{
pplfI[i] = new double [_nDim];
memset(pplfI[i] 0 sizeof(double) * _nDim);
pplfI[i][i] = 1;
}
//Gaussian消元法求逆矩阵--正向消元
for (i = 0; i < _nDim; i++)
{
double lfTmp = pplfMat[i][i];
for (int j = 0; j < _nDim; j++)
{
pplfMat[i][j] /= lfTmp;
pplfI[i][j] /= lfTmp;
}
for (j = i + 1; j < _nDim; j++)
{
double lfTmp = -pplfMat[j][i];
for (int k
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 35 2009-02-14 21:12 Em\data1.txt
文件 110 2009-02-11 02:36 Em\data2.txt
文件 258116 2009-02-13 23:04 Em\Debug\Em.exe
文件 266204 2009-02-13 23:04 Em\Debug\Em.ilk
文件 17727 2009-02-13 23:04 Em\Debug\em.obj
文件 210440 2009-02-13 23:04 Em\Debug\Em.pch
文件 467968 2009-02-13 23:04 Em\Debug\Em.pdb
文件 7792 2009-02-13 23:04 Em\Debug\main.obj
文件 41984 2009-02-15 16:25 Em\Debug\vc60.idb
文件 53248 2009-02-13 23:04 Em\Debug\vc60.pdb
文件 12161 2009-02-13 23:04 Em\em.cpp
文件 4348 2009-02-13 23:02 Em\Em.dsp
文件 510 2009-02-13 23:01 Em\Em.dsw
文件 2410 2009-02-10 18:41 Em\em.h
文件 58368 2009-02-15 16:41 Em\Em.ncb
文件 53760 2009-02-15 16:41 Em\Em.opt
文件 1314 2009-02-13 23:04 Em\Em.plg
文件 1720 2009-02-13 23:04 Em\main.cpp
目录 0 2009-02-15 15:22 Em\Debug
目录 0 2009-02-15 16:41 Em
----------- --------- ---------- ----- ----
1458215 20
评论
共有 条评论