FCM聚类数据算法

int N_max, K;
int* CenterIndex;
double *Center;
double *CenterCopy;
double *DataSet;
double **Cluster;
int *Top;


void InitData(double Gxy_yiwei[40][40]);
void InitCenter();
void CreatRandomArray(int N_max, int K, int* centerIndex);
void CopyCenter();
void UpdateCluster();
void UpdateCenter();
int GetIndex(double value, double* centerIndex);
void AddtoCluster(int index, double value);
void print();
bool IsEqual(double* center, double* centercopy);
void InitData(double Gxy_yiwei[40*40])
{
int i = 0;
int a;


N_max = 40 * 40;
K = 3;
CenterIndex = new int[sizeof(int)*K];
Center = new double[sizeof(double)*K];
CenterCopy = new double[sizeof(double)*K];
DataSet = new double[sizeof(double)*N_max];
Cluster = new double*[sizeof(double*)*K];
Top = new int[sizeof(int)*K];


for (i = 0; i {
Cluster[i] = new double[sizeof(double)*N_max];
Top[i] = 0;
}


for (i = 0; i {
//a = rand() % 100;
DataSet[i] = Gxy_yiwei[i];
}


InitCenter();
UpdateCluster();
}


void InitCenter()
{
int i = 0;
//CreatRandomArray(N,K,CenterIndex);
for (i = 0; i {
//Center[i] = DataSet[CenterIndex[i]];
Center[i] = DataSet[i];
}
CopyCenter();
}


void CreatRandomArray(int N_max, int K, int* centerIndex)
{
int i = 0, j = 0;
for (i = 0; i {
centerIndex[i] = i;
/*
int a = rand()%N;
for(j=0;j {
if(centerIndex[j]==a)
break;
}
if(j>=i)
centerIndex[i]=a;
else
i--;*/
}
}


void CopyCenter()
{
int i = 0;
for (i = 0; i {
CenterCopy[i] = Center[i];
}
}


void UpdateCluster()
{
int i, tindex;
for (i = 0; i {
Top[i] = 0;
}
for (i = 0; i {
tindex = GetIndex(DataSet[i], Center);
AddtoCluster(tindex, DataSet[i]);
}
}


int GetIndex(double value, double* center)
{
int i = 0;
int index = 0;
double min = fabs(value - center[i]);
for (i = 0; i {
if (fabs(value - center[i]) {
index = i;
min = fabs(value - center[i]);
}
}
return index;
}


void AddtoCluster(int index, double value)
{
Cluster[index][Top[index]] = value;
Top[index]++;
}


void UpdateCenter()
{
int i, j;
double sum;


for (i = 0; i {
sum = 0.0;
for (j = 0; j {
sum += Cluster[i][j];
}
if (Top[i]>0)
{
Center[i] = sum / Top[i];
}
}
}


bool IsEqual(double* center, double* centercopy)
{
int i;
for (i = 0; i {
if (center[i] != centercopy[i])
return 0;
}
return 1;
}


void print(int *min_center,int *max_center)
{
int i, j;
double min_0, max_0, min_1, max_1, min_2, max_2;
cout << "==========================================" << endl;
for (i = 0; i < K; i++)
{
cout << "第" << i << "组:质心为:" << Center[i] << endl;
cout << "数据元素为:\n";
}
int a = Center[0];
int b = Center[1];
int c = Center[2];
*min_center = a;
*max_center = a;


//if ((a < b&&b < c)|| (c < b&&b //{
// *min_center = b;
//}
//else if ((b < a&&a < c)|| (c < a&&a < b))
//{
// *min_center = a;
//}
//else if ((a < c&&c < b) || (b < c&&c < a))
//{
// *min_center = c;
//}


/*if (*min_center > b)
{
*min_center = b;
}
if (*min_center > c)
{
*min_center = c;
}


if (*max_center < b)
{
*max_center = b;
}
if (*max_center < c)
{
*max_center = c;
}*/
min_0 = Cluster[0][0]; max_0 = Cluster[0][0];
min_1 = Cluster[1][0]; max_1 = Cluster[1][0];
min_2 = Cluster[2][0]; max_2 = Cluster[2][0];
for (j = 0; j {
if (min_0 > Cluster[0][j])
{
min_0 = Cluster[0][j];
}
if (max_0< Cluster[0][j])
{
max_0 = Cluster[0][j];
}
//cout << Cluster[i][j] << '\t';
}
for (j = 0; j {
if (min_1 > Cluster[1][j])
{
min_1 = Cluster[1][j];
}
if (max_1< Cluster[1][j])
{
max_1 = Cluster[1][j];
}
//cout << Cluster[i][j] << '\t';
}
for (j = 0; j {
if (min_2 > Cluster[2][j])
{
min_2 = Cluster[2][j];
}
if (max_2< Cluster[2][j])
{
max_2 = Cluster[2][j];
}
//cout << Cluster[i][j] << '\t';
}
cout << endl;
//cout << "第 " << i << "组元素输出完毕" << endl;
cout << "+++++++++++++++++++" << endl;


*min_center = max_2;
*max_center = max_1;
cout << "第一类为:(" << min_0 << "," << max_0 << ")," << endl;
cout << "第二类为:(" << min_1 << "," << max_1 << ")," << endl;
cout << "第三类为:(" << min_2 << "," << max_2 << ")" << endl;
}

主函数里内容

int Flag_2 = 1;

InitData(Gxy_yiwei);
while (Flag_2)
{
UpdateCluster();
UpdateCenter();
if (IsEqual(Center, CenterCopy))
{
Flag_2 = 0;
}
else
{
CopyCenter();
}
}
int min_cen = 0;
int max_cen = 0;
print(&min_cen, &max_cen);

你可能感兴趣的:(c++编程)