问题描述
情况是这样的,我现在做一个关于聚类的程序,下面是程序:}classProgram{intnumber;//要分类的记录总个数intK;//聚类中心个数shuzu[]julei;//存放要分类的记录。int[]type;//存放等别。shuzusum=newshuzu();//计算新聚类中心时用到临时变量。shuzu[]newcenter;//存放新的聚类中心。shuzu[]newcenter0;//存放上一次的聚类中心。inttest;//看K个聚类中心中有几个是相等的。intL;//计算迭代次数。double[]array1=newdouble[19]{11.03,5.47,3.58,2.01,2.13,2.06,1.63,1.17,1.03,0.69,0.77,0.59,0.65,0.51,0.73,0.53,0.36,0.52,0.34};double[]array2=newdouble[19]{50.3,19.30,9.85,4.17,5.65,1.74,2.04,1.60,2.34,1.33,1.41,1.25,1.19,0.93,1.13,0.82,0.52,1.03,0.49};double[]array3=newdouble[19]{11.81,5.20,3.14,1.47,1.04,0.17,1.04,0.89,0.53,0.48,0.52,0.30,0.49,0.16,0.35,0.16,0.19,0.30,0.18};double[]array4=newdouble[19]{11.27,7.18,2.11,1.58,2.11,1.57,1.46,0.76,0.89,0.58,0.42,0.14,0.38,0.25,0.55,0.34,0.21,0.55,0.16};publicint_number{set{number=value;}get{returnnumber;}}publicint_K{set{K=value;}get{returnK;}}publicvoidfuzhi(){type=newint[number];julei=newshuzu[number];newcenter=newshuzu[K];newcenter0=newshuzu[K];for(inti=0;i<=number-1;i++){julei[i].a=array1[i];julei[i].b=array2[i];julei[i].c=array3[i];julei[i].d=array4[i];type[i]=0;}for(inti=0;i<=K-1;i++)//初始化聚类中心。这样不会出问题吧?这里应该随机取值。{newcenter[i]=julei[i];newcenter0[i]=julei[i];}}//计算新的聚类中心输入:类别publicshuzunewCenter(intm){intN=0;for(inti=0;i<=number-1;i++){if(type[i]==m){sum.a=julei[i].a+sum.a;sum.b=julei[i].b+sum.b;sum.c=julei[i].c+sum.c;sum.d=julei[i].d+sum.d;N+=1;}}sum.a=sum.a/N;sum.b=sum.b/N;sum.c=sum.c/N;sum.d=sum.d/N;returnsum;}//比较两个聚类中心的是否相等privateboolcompare(shuzua,shuzub){if(((int)(a.a*100)==(int)(b.a*100))&&((int)(a.b*100)==(int)(b.b*100))&&((int)(a.c*100)==(int)(b.c*100))&&((int)(a.d*100)==(int)(b.d*100)))returntrue;elsereturnfalse;}//计算两个点的欧式距离privatedoubledistance(shuzup1,shuzup2){returnMath.Sqrt((p1.a-p2.a)*(p1.a-p2.a)+(p1.b-p2.b)*(p1.b-p2.b)+(p1.c-p2.c)*(p1.c-p2.c)+(p1.d-p2.d)*(p1.d-p2.d));}//进行迭代,对number个样本根据聚类中心进行分类别忘了重新计算新的聚类中心privatevoidorder(){inttemp=0;//记录shuzu[i]暂时在哪个类中for(inti=0;i<=number-1;i++){for(intj=0;j<=K-1;j++){if(distance(julei[i],newcenter0[temp])>distance(julei[i],newcenter0[j]))//对于一个外点,从K个中心点中选择最近的中心点temp=j;}type[i]=temp;//Console.WriteLine("经比较后,{0}归为{1}类",unknown[i],temp);}}staticvoidMain(string[]args){Programaa=newProgram();aa._number=19;aa._K=4;aa.fuzhi();//显示初始聚类中心Console.WriteLine("显示初始的聚类中心");for(inti=0;i<=aa.K-1;i++){Console.WriteLine("{0},{1},{2},{3}",aa.newcenter[i].a,aa.newcenter[i].b,aa.newcenter[i].c,aa.newcenter[i].d);Console.WriteLine("{0},{1},{2},{3}",aa.newcenter0[i].a,aa.newcenter0[i].b,aa.newcenter0[i].c,aa.newcenter0[i].d);}Console.WriteLine("*************************************************************************");//Console.WriteLine(aa.julei[0].b);//Console.WriteLine(aa.julei.Length.ToString());for(inti=0;i<=aa.julei.Length-1;i++){//Console.WriteLine(aa.julei[i].c);}//Console.WriteLine(aa.newcenter[0].b);//Console.ReadLine();Console.WriteLine("开始时共有如下个未知样本:");for(inti=0;i<=aa.julei.Length-1;i++){Console.WriteLine("{0},{1},{2},{3}",aa.julei[i].a,aa.julei[i].b,aa.julei[i].c,aa.julei[i].d);}Console.WriteLine("===================================================================");while(aa.test!=aa.K)//这一句是什么意思?{aa.order();aa.test=0;//为什么要重新赋值0呢?for(inti=0;i<=aa.K-1;i++){aa.newcenter[i]=aa.newCenter(i);//Console.WriteLine("第{0}类新中心{1}",i,aa.newcenter[i]);if(aa.compare(aa.newcenter[i],aa.newcenter0[i]))aa.test=aa.test+1;//看有几个相等的,如果不相等的话......elseaa.newcenter0[i]=aa.newcenter[i];}aa.L=aa.L+1;//Console.WriteLine("******已完成第{0}次迭代*******",l);}//显示类别for(inti=0;i<=aa.julei.Length-1;i++){Console.WriteLine(aa.type[i].ToString());}//Console.Write("{0}","分类后:");for(intj=0;j<=aa.K-1;j++){Console.WriteLine("第{0}类有:",j);for(inti=0;i<=aa.julei.Length-1;i++){if(aa.type[i]==j)Console.WriteLine("{0},{1},{2},{3}",aa.julei[i].a,aa.julei[i].b,aa.julei[i].c,aa.julei[i].d);}Console.WriteLine();}Console.WriteLine("迭代次数:"+aa.L.ToString());//迭代次数应该再去1才对,为什么呢?//显示最后的聚类中心Console.WriteLine("显示最后的聚类中心");for(inti=0;i<=aa.K-1;i++){Console.WriteLine("{0},{1},{2},{3}",aa.newcenter[i].a,aa.newcenter[i].b,aa.newcenter[i].c,aa.newcenter[i].d);}//返回一个距离doublexx;xx=aa.distance(aa.julei[0],aa.julei[1]);Console.WriteLine(xx);Console.WriteLine(aa.distance(aa.newcenter[0],aa.newcenter[1]));//这里出问题?????????????Console.WriteLine(aa.distance(newshuzu(0,0,0,0),newshuzu(1,1,1,1)));Console.ReadLine();}}}请大家复制一下帮我看看,是一个控制面版程序。aa.newcenter[0],aa.newcenter[1];与aa.julei[0],aa.julei[1]应该是一样的,上面输出的“初始聚类中心”也说明了这上点,但是,用distance这个函数求距离时问题出来了,aa.newcenter[0],aa.newcenter[1]这两个参数好像不是一个结构似的,在线等,问题出现在上面问号处。先行谢过
解决方案
解决方案二:
不要沉下去呀,自己先顶下。。。。。。
解决方案三:
shuzu这个类型没有定义
解决方案四:
shuzu应该是个结构,带一个构造器structshuzu{publicdoublea;publicdoubleb;publicdoublec;publicdoubled;publicshuzu(doublea,doubleb,doublec,doubled){this.a=a;this.b=b;this.c=c;this.d=d;}}
解决方案五:
我运行了一下这个程序,发现结果全都是无穷大,我不太懂聚类的概念,不过从程序的意图猜测,这个方法是不是少了如下语句:请楼主添加此句后再运行一遍publicshuzunewCenter(intm){sum=newshuzu();intN=0;for(inti=0;i<=number-1;i++){if(type[i]==m){sum.a=julei[i].a+sum.a;sum.b=julei[i].b+sum.b;sum.c=julei[i].c+sum.c;sum.d=julei[i].d+sum.d;N+=1;}}sum.a=sum.a/N;sum.b=sum.b/N;sum.c=sum.c/N;sum.d=sum.d/N;returnsum;}
解决方案六:
楼主?人呢我半夜给你看程序,你也不回一声