一:说明
动态聚类方法是模式识别中一种普遍采用的方法,它具有以下3个要点:
1:选定某种距离度量作为样本间的相似性度量
2:确定某个评价聚类结果质量的准则函数
3:给定某个初始分类,然后用迭代算法找出使准则函数取极值的最好的聚类结果
本文给出了 C-均值算法 的 C++ 实现。
(算法描述参见 边肇祺 张学工等 << 模式识别 >> P237 清华大学出版社)
二:源码
2.1 头文件
#pragma once
#include <list>
#include <vector>
using namespace std;
#define DATANUM 19
#define MAXDIST 333333
struct CData
{
int x1;
int x2;
};
class CCMean
{
public:
CCMean(CData *pdata);
~CCMean(void);
void init();
void work(int InitClassNum);
private:
// calculate the mean of class i:
void CalcuMean( int i );
// calculate the ERROR of class i:
void CalcuJc(int i);
void CalcuJe();
// step 1 of C-Mean algorithm
void InitDeploy();
// step 4 and 5 of C-Mean algorithm
// da is now in class i,
// return ture when moving da from class to class k
// return false when we do not move da
bool MoveItoK( const CData& da, int i, int &k );
// calculate the distance of to data:
int dist( const CData& mean, const CData& da);
// print result:
void OutPut();
// iClassNum is the initial class number, in text book, iClassNum <==> C
int iClassNum;
// pointer to data array:
CData *pData;
// store the mean of all classes. just ueses 0 to iClassNum - 1;