?? cmeans.cpp
字號:
/*****************************************************************
這個程序是實現了一個聚類CMI算法,對給定一個數據集,按要求將其劃分為
三個類,并計算其分類的效率,并給出一定的結果比較及輸出!!
********************************************************************/
#include<fstream.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<math.h>
#include <stdio.h>
#include <set>
const int c=3;
const int dim=5;//數據的維數
const int size=150;//表明數據集的大小;
class cluster;
//////定義一個數據單元的類
class metadata{
public:
int tag;//定義數據數元的標記,如屬于某一類;
double value[dim];//定義數據單元的值;
// friend class data;
friend class cluster;
metadata(){}
metadata(double tvalue[]){
for(int j=0;j<dim;j++){
value[j]=tvalue[j];}
tag=int(value[4]);
}
};
///////////定義一個cluster集,并進行分類
class cluster{
public:
int clustersize[3];//表示類的大小
metadata clustersample[size];//表示每類中的樣本集
metadata clustercenter[c];//每類樣本的中心
cluster(metadata initdata[]){
for(int j=0;j<size;j++){
//clustersample[j](initdata[j]);
for(int i=0;i<dim;i++){
clustersample[j].value[i]=initdata[j].value[i];}
clustersample[j].tag=int(initdata[j].value[4]);
}
//clustercenter=
initcenter(clustersample);
///////測試//////////////////
cout<<"the initial cluster center is:"<<endl;
for(int m=0;m<c;m++){
for(int n=0;n<dim;n++){
cout<<setw(4)<<clustercenter[m].value[n]<<' ';
}
cout<<endl;
}
///////////////////////
for(int k=0;k<c;k++)
clustersize[k]=0;
}
/* ///////重載=號運算符
metadata operator =(metadata sample){
metadata temp;
for(int i=0;i<=dim;i++){
temp.value[i]=sample.value[i];}
temp.tag=sample.tag;
return temp;
}*/
/* metadata operator =(metadata sample[]){
metadata temp[3];
for(int i=0;i<3;i++){
temp[i]=sample[i];
}
}*/
/////////取得初始的類中心
void initcenter(metadata clustersample[]){
// float t=0.7;
double temp=0;
double maxdistance=0;
double mindistance=0;
// metadata clustercenter[c];
clustercenter[0]=clustersample[0];
for(int i=0;i<=size-1;i++){
temp=distance(clustercenter[0],clustersample[i]);
if(maxdistance<=temp){
maxdistance=temp;
clustercenter[1]=clustersample[i];
}
}
temp=0;
for(int k=0;k<=size-1;k++){
double a=distance(clustercenter[0],clustersample[k]);
double b=distance(clustercenter[1],clustersample[k]);
if(a<=b)
{mindistance=a;
if(mindistance>=temp){
temp=mindistance;
clustercenter[2]=clustersample[k];
}
}
else
{mindistance=b;
if(mindistance>=temp){
temp=mindistance;
clustercenter[2]=clustersample[k];
}
}
}
//return clustercenter;
}
////////距離函數
static double distance(metadata x,metadata y){
//metadata x=x;
//metadata y=y;
double distance;
double temp=0;
double dis=0;
for(int i=0;i<=dim-1;i++){
temp=x.value[i]-y.value[i];
temp=temp*temp;
dis=dis+temp;
}
distance=dis;
return distance;
}
/////求新的類中心的函數
/* static metadata newcenter(metadata tempcluster[],int j)
{
//metadata tempcluster=tempcluster;
metadata tempcenter;
//int j=j;
int n=0;
for(int i=0;i<4;i++)tempcenter.value[i]=0;
tempcenter.value[4]=j;
for(int k=0;k<size;k++){
if(tempcluster[k].value[4]==j)
{for(int l=0;l<4;l++){
tempcenter.value[l]=tempcenter.value[l];
}
n++;
}
}
for(int h=0;h<3;h++){
tempcenter.value[h]=tempcenter.value[h]/n;
}
return tempcenter;
} */
/*//////進行打印并對所得數據和原始數據進行比較
void clusterprint(int it){
//int it=it;
cout<<"the number of the iterance to abtain the cluster is:"<<it<<endl;
cout<<"the final cluster center is:"<<endl;
for(int j=0;j<c;j++){
for(int i=0;i<dim;i++){
cout<<setw(4)<<clustercenter[j].value[i]<<' ';
}
cout<<endl;
}
}
///////////////////////////////////////////////////////////*/
void cmp(metadata initcluster[]){
int wrong=0;
//metadata initcluster[size]=initcluster;
cout<<"the first column is the cluster in fact;"<<endl;
cout<<"the second column is expected cluster the data belong to;"<<endl;
for(int i=0;i<size;i++)
{
int t=int(clustersample[i].value[4]);
if(t==1)t=2;
else if(t==2)t=1;
cout<<setw(4)<<t<<setw(4)<<initcluster[i].value[4]<<endl;
switch(t){
case 0: clustersize[0]++;break;
case 1: clustersize[1]++;break;
case 2: clustersize[2]++;break;
default:break;}
if(t!=initcluster[i].value[4])wrong++;
}
cout<<"the number of cluster that is wrongly classified is:"<<wrong<<endl;
}
///////進行分類
double cmeans(){
int it=1;
double a,b,d;
double th=0.001;
metadata tempcenter[3];
int lable=0;
//clustercenter=initcenter(clustersample);
//cout<<"begin"<<endl;
//////進行初步的分類
/* ///////測試//////////////////
cout<<"the initial cluster center is:"<<endl;
for(int m=0;m<c;m++){
for(int n=0;n<dim;n++){
cout<<setw(4)<<clustercenter[m].value[n]<<' ';
}
cout<<endl;
}
/////////////////////// */
do{
for(int i=0;i<150;i++){
a=distance(clustercenter[0],clustersample[i]);
b=distance(clustercenter[1],clustersample[i]);
d=distance(clustercenter[2],clustersample[i]);
if(a<=b&&a<=d)clustersample[i].value[4]=0;
if(b<=a&&b<=d)clustersample[i].value[4]=1;
if(d<=a&&d<=b)clustersample[i].value[4]=2;
}
/* //////////測試數據/////////////////////////
for(int k=0;k<size;k++){
for(int l=0;l<dim;l++){
cout<<setw(4)<<clustersample[k].value[l]<<' ';
}
cout<<endl;
}
//////////////////////////////// */
cout<<"continue"<<endl;
//////得到新類后求新類的中心
for(int j=0;j<c;j++)//tempcenter[j]=newcenter(clustersample,j);
{
int n=0;
for(int i=0;i<4;i++)tempcenter[j].value[i]=0;
tempcenter[j].value[4]=j;
for(int k=0;k<size;k++){
if(clustersample[k].value[4]==j)
{for(int l=0;l<4;l++){
tempcenter[j].value[l]=tempcenter[j].value[l]+clustersample[k].value[l];
}
n++;
}
}
// cout<<"fsfsdafasf"<<n<<endl;
for(int h=0;h<4 ;h++){
tempcenter[j].value[h]=tempcenter[j].value[h]/n;
}
}
///////
for(int y=0;y<c;y++){
for(int i=0;i<dim;i++){
cout<<setw(4)<<tempcenter[y].value[i]<<' ';
}
cout<<endl;
}
////////
it++;
a=distance(clustercenter[0],tempcenter[0]);
b=distance(clustercenter[1],tempcenter[1]);
d=distance(clustercenter[2],tempcenter[2]);
if(a<=th&&b<=th&&d<=th)lable=0;
else lable=1;
/*if(it>100){
lable=0;
cout<<"numbers of iteration exceeded;"<<endl;
return 0;
}*/
for(int w=0;w<dim;w++){
clustercenter[0].value[w]=tempcenter[0].value[w];
clustercenter[1].value[w]=tempcenter[1].value[w];
clustercenter[2].value[w]=tempcenter[2].value[w];
}
}while(lable);
cout<<"the number of the iterance to abtain the cluster is:"<<it<<endl;
cout<<"the final cluster center is:"<<endl;
for(int j=0;j<c;j++){
for(int i=0;i<dim;i++){
cout<<setw(4)<<clustercenter[j].value[i]<<' ';
}
cout<<endl;
}
cout<<"done!"<<endl;
return 0;
}
};
///////////主函數
void main()
{
double temp[dim];
metadata initdata[size];
// cluster initcluster;
//cluster finalcluster;
ifstream file1("E:\\test\\iris.dat");
int j=0;
while(!file1.eof()){
file1>>temp[0]>>temp[1]>>temp[2]>>temp[3]>>temp[4];
for(int k=0;k<dim;k++){
initdata[j].value[k]=temp[k];}
initdata[j].tag=int(temp[4]);
//initdata[j](temp);
j++;
}
/* //////////測試數據/////////////////////////
for(int k=0;k<size;k++){
for(int l=0;l<dim;l++){
cout<<setw(4)<<initdata[k].value[l]<<' ';
}
cout<<endl;
}
//////////////////////////////// */
//file1.close();
cluster finalcluster(initdata);
//cout<<"all over"<<endl;
finalcluster.cmeans();
finalcluster.cmp(initdata);
system("pause");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -