1、void randd3(double A6,double test7);void rand4(double A6,double study6);void randd4(double A6,double test7);void rand5(double A6,double study6);void randd5(double A6,double test7);void SEARCH(double A,int low,int high);void OUTPUT(double test7,FILE *fp); double MAX(double t1,double t2,double t3);dou
2、ble distance(int i,int j,double sample6,double test7);/主程序:void main() char s100; double A1506; int i,j; double study606; /学习样本 double test907; /测试样本 FILE *fp; if(fp=fopen(Iris.txt,r)=NULL) printf(cannot open file n); exit(0); fgets(s,100,fp); for(i=0;i150;i+) for(j=0;j6;j+) fscanf(fp,%lf,&Aij); /读取
3、样本集存入A fclose(fp); fp=fopen(Classification Outputs (k=7).docw/建立“分类结果(k=5).doc”文档存放分类结果 fprintf(fp,nThe first output:n rand1(A,study); /初始化数组study randt1(A,test); /初始化数组test RESULT(A,study,test); /RESULT聚类 OUTPUT(test,fp); /输出聚类结果并计算正确率 nThe second output: rand2(A,study); randd2(A,test);nThe third o
4、utput: rand3(A,study); randd3(A,test);The fourth output: rand4(A,study); randd4(A,test);The fifth output: rand5(A,study); randd5(A,test);/K近邻算法得到分类结果:void RESULT(double A6,double study6,double test7) int i,j,t; double u; double t1,t2,t3; double temp60; double mk; double D9060;90;i+)60; Dij=distance(
5、i,j,study,test); t1=0;t2=0;t3=0; tempj=Dij; SEARCH(temp,0,59); for(t=0;tk;t+) for(j=0; if(tempt=Dij) mt=j; if(study(int)mt5=1.0) t1+; else if(study(int)mt5=2.0) t2+; else t3+; u=MAX(t1,t2,t3); testi6=u;/初始化样本集及测试集:void rand1(double A6,double study6)20; studyij=Aij; studyi+20j=Ai+50j; studyi+40j=Ai+1
6、00j;void randt1(double A6,double test7) for(i=20;50; testi-20j=Aij; testi+10j=Ai+50j; testi+40j=Ai+100j;void rand2(double A6,double study6) for(i=10;30; studyi-10j=Aij; studyi+10j=Ai+50j; studyi+30j=Ai+100j;void randd2(double A6,double test7)10; testij=Aij; testi+20j=Ai+100j; for(i=30; testi+20j=Ai+
7、30j; testi+40j=Ai+60j;void rand3(double A6,double study6)40; studyi-20j=Aij; studyij=Ai+50j; studyi+20j=Ai+100j;void randd3(double A6,double test7) testi+20j=Ai+50j; for(i=40; testi+20j=Aij; testi+30j=Ai+50j;void rand4(double A6,double study6) studyi-30j=Aij; studyi-10j=Ai+50j; studyi+10j=Ai+100j;vo
8、id randd4(double A6,double test7) testi+60j=Ai+100j;void rand5(double A6,double study6) studyi+10j=Ai+40j; studyi+30j=Ai+90j; studyi+50j=Ai+140j;void randd5(double A6,double test7) testi-10j=Aij; testi+50j=Ai+100j;/搜索检验位置样本x的最近邻是否存在:void SEARCH(double A,int low,int high) int i=low; int j=high; doubl
9、e pivot=Alow; while (ij) while(i=pivot) j-; Ai=Aj;Ai i+; Aj=Ai; Ai=pivot; if(lowi+1) SEARCH(A,i+1,high);/结果输出聚类结果及正确率:void OUTPUT(double test7,FILE *fp) int i,j,n=0;7; fprintf(fp,%lf ,testij); fprintf(fp, if(testi6=testi5) n+;正确率%lfn,n/90.0);/找最大值double MAX(double t1,double t2,double t3) if(t1=t2&t1
10、=t3) return 1; else if(t2=t3&t2=t1) return 2; else return 3;/求距离函数double distance(int i,int j,double sample6,double test7) int m; double s=0; for(m=1;m4;m+) s+=pow(testim-samplejm,2); s=sqrt(s); return s;附:k=7时归类结果The first result:21.000000 5.400000 3.400000 1.700000 0.200000 1.000000 1.000000 22.00
11、0000 5.100000 3.700000 1.500000 0.400000 1.000000 1.000000 23.000000 4.600000 3.600000 1.000000 0.200000 1.000000 1.000000 24.000000 5.100000 3.300000 1.700000 0.500000 1.000000 1.000000 25.000000 4.800000 3.400000 1.900000 0.200000 1.000000 1.000000 26.000000 5.000000 3.000000 1.600000 0.200000 1.0
12、00000 1.000000 27.000000 5.000000 3.400000 1.600000 0.400000 1.000000 1.000000 28.000000 5.200000 3.500000 1.500000 0.200000 1.000000 1.000000 29.000000 5.200000 3.400000 1.400000 0.200000 1.000000 1.000000 30.000000 4.700000 3.200000 1.600000 0.200000 1.000000 1.000000 31.000000 4.800000 3.100000 1
13、.600000 0.200000 1.000000 1.000000 32.000000 5.400000 3.400000 1.500000 0.400000 1.000000 1.000000 33.000000 5.200000 4.100000 1.500000 0.100000 1.000000 1.000000 34.000000 5.500000 4.200000 1.400000 0.200000 1.000000 1.000000 35.000000 4.900000 3.100000 1.500000 0.200000 1.000000 1.000000 36.000000
14、 5.000000 3.200000 1.200000 0.200000 1.000000 1.000000 37.000000 5.500000 3.500000 1.300000 0.200000 1.000000 1.000000 38.000000 4.900000 3.600000 1.400000 0.100000 1.000000 1.000000 39.000000 4.400000 3.000000 1.300000 0.200000 1.000000 1.000000 40.000000 5.100000 3.400000 1.500000 0.200000 1.00000
15、0 1.000000 41.000000 5.000000 3.500000 1.300000 0.300000 1.000000 1.000000 42.000000 4.500000 2.300000 1.300000 0.300000 1.000000 1.000000 43.000000 4.400000 3.200000 1.300000 0.200000 1.000000 1.000000 44.000000 5.000000 3.500000 1.600000 0.600000 1.000000 1.000000 45.000000 5.100000 3.800000 1.900
16、000 0.400000 1.000000 1.000000 46.000000 4.800000 3.000000 1.400000 0.300000 1.000000 1.000000 47.000000 5.100000 3.800000 1.600000 0.200000 1.000000 1.000000 48.000000 4.600000 3.200000 1.400000 0.200000 1.000000 1.000000 49.000000 5.300000 3.700000 1.500000 0.200000 1.000000 1.000000 50.000000 5.0
17、00000 3.300000 1.400000 0.200000 1.000000 1.000000 71.000000 5.900000 3.200000 4.800000 1.800000 2.000000 2.000000 72.000000 6.100000 2.800000 4.000000 1.300000 2.000000 2.000000 73.000000 6.300000 2.500000 4.900000 1.500000 2.000000 2.000000 74.000000 6.100000 2.800000 4.700000 1.200000 2.000000 2.
18、000000 75.000000 6.400000 2.900000 4.300000 1.300000 2.000000 2.000000 76.000000 6.600000 3.000000 4.400000 1.400000 2.000000 2.000000 77.000000 6.800000 2.800000 4.800000 1.400000 2.000000 2.000000 78.000000 6.700000 3.000000 5.000000 1.700000 2.000000 3.000000 79.000000 6.000000 2.900000 4.500000
19、1.500000 2.000000 2.000000 80.000000 5.700000 2.600000 3.500000 1.000000 2.000000 2.000000 81.000000 5.500000 2.400000 3.800000 1.100000 2.000000 2.000000 82.000000 5.500000 2.400000 3.700000 1.000000 2.000000 2.000000 83.000000 5.800000 2.700000 3.900000 1.200000 2.000000 2.000000 84.000000 6.00000
20、0 2.700000 5.100000 1.600000 2.000000 3.000000 85.000000 5.400000 3.000000 4.500000 1.500000 2.000000 2.000000 86.000000 6.000000 3.400000 4.500000 1.600000 2.000000 2.000000 87.000000 6.700000 3.100000 4.700000 1.500000 2.000000 2.000000 88.000000 6.300000 2.300000 4.400000 1.300000 2.000000 2.0000
21、00 89.000000 5.600000 3.000000 4.100000 1.300000 2.000000 2.000000 90.000000 5.500000 2.500000 4.000000 1.300000 2.000000 2.000000 91.000000 5.500000 2.600000 4.400000 1.200000 2.000000 2.000000 92.000000 6.100000 3.000000 4.600000 1.400000 2.000000 2.000000 93.000000 5.800000 2.600000 4.000000 1.20
22、0000 2.000000 2.000000 94.000000 5.000000 2.300000 3.300000 1.000000 2.000000 2.000000 95.000000 5.600000 2.700000 4.200000 1.300000 2.000000 2.000000 96.000000 5.700000 3.000000 4.200000 1.200000 2.000000 2.000000 97.000000 5.700000 2.900000 4.200000 1.300000 2.000000 2.000000 98.000000 6.200000 2.900000 4.300000 1.300000 2.000000 2.000000 99.000000 5.100000 2.500000 3.000000 1.100000 2.000000 2.000
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2