课程设计模式识别大作业Word格式文档下载.docx
《课程设计模式识别大作业Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《课程设计模式识别大作业Word格式文档下载.docx(73页珍藏版)》请在冰点文库上搜索。
voidrandd3(doubleA[][6],doubletest[][7]);
voidrand4(doubleA[][6],doublestudy[][6]);
voidrandd4(doubleA[][6],doubletest[][7]);
voidrand5(doubleA[][6],doublestudy[][6]);
voidrandd5(doubleA[][6],doubletest[][7]);
voidSEARCH(doubleA[],intlow,inthigh);
voidOUTPUT(doubletest[][7],FILE*fp);
doubleMAX(doublet1,doublet2,doublet3);
doubledistance(inti,intj,doublesample[][6],doubletest[][7]);
//主程序:
voidmain()
{
chars[100];
doubleA[150][6];
inti,j;
doublestudy[60][6];
//学习样本
doubletest[90][7];
//测试样本
FILE*fp;
if((fp=fopen("
Iris.txt"
"
r"
))==NULL)
{
printf("
cannotopenfile\n"
);
exit(0);
}
fgets(s,100,fp);
for(i=0;
i<
150;
i++)
for(j=0;
j<
6;
j++)
fscanf(fp,"
%lf"
&
A[i][j]);
//读取样本集存入A[][]
fclose(fp);
fp=fopen("
ClassificationOutputs(k=7).doc"
w"
//建立“分类结果(k=5).doc”文档存放分类结果
fprintf(fp,"
\nThefirstoutput:
\n"
rand1(A,study);
//初始化数组study
randt1(A,test);
//初始化数组test
RESULT(A,study,test);
//RESULT聚类
OUTPUT(test,fp);
//输出聚类结果并计算正确率
\nThesecondoutput:
rand2(A,study);
randd2(A,test);
\nThethirdoutput:
rand3(A,study);
randd3(A,test);
Thefourthoutput:
rand4(A,study);
randd4(A,test);
Thefifthoutput:
rand5(A,study);
randd5(A,test);
//K近邻算法得到分类结果:
voidRESULT(doubleA[][6],doublestudy[][6],doubletest[][7])
inti,j,t;
doubleu;
doublet1,t2,t3;
doubletemp[60];
doublem[k];
doubleD[90][60];
90;
i++)
60;
D[i][j]=distance(i,j,study,test);
t1=0;
t2=0;
t3=0;
temp[j]=D[i][j];
SEARCH(temp,0,59);
for(t=0;
t<
k;
t++)
for(j=0;
{
if(temp[t]==D[i][j])
m[t]=j;
}
{
if(study[(int)m[t]][5]==1.0)
t1++;
elseif(study[(int)m[t]][5]==2.0)
t2++;
elset3++;
}
u=MAX(t1,t2,t3);
test[i][6]=u;
}
//初始化样本集及测试集:
voidrand1(doubleA[][6],doublestudy[][6])
20;
study[i][j]=A[i][j];
study[i+20][j]=A[i+50][j];
study[i+40][j]=A[i+100][j];
voidrandt1(doubleA[][6],doubletest[][7])
for(i=20;
50;
test[i-20][j]=A[i][j];
test[i+10][j]=A[i+50][j];
test[i+40][j]=A[i+100][j];
voidrand2(doubleA[][6],doublestudy[][6])
for(i=10;
30;
study[i-10][j]=A[i][j];
study[i+10][j]=A[i+50][j];
study[i+30][j]=A[i+100][j];
voidrandd2(doubleA[][6],doubletest[][7])
10;
test[i][j]=A[i][j];
test[i+20][j]=A[i+100][j];
for(i=30;
test[i+20][j]=A[i+30][j];
test[i+40][j]=A[i+60][j];
voidrand3(doubleA[][6],doublestudy[][6])
40;
study[i-20][j]=A[i][j];
study[i][j]=A[i+50][j];
study[i+20][j]=A[i+100][j];
voidrandd3(doubleA[][6],doubletest[][7])
test[i+20][j]=A[i+50][j];
for(i=40;
test[i+20][j]=A[i][j];
test[i+30][j]=A[i+50][j];
voidrand4(doubleA[][6],doublestudy[][6])
study[i-30][j]=A[i][j];
study[i-10][j]=A[i+50][j];
study[i+10][j]=A[i+100][j];
voidrandd4(doubleA[][6],doubletest[][7])
test[i+60][j]=A[i+100][j];
voidrand5(doubleA[][6],doublestudy[][6])
study[i+10][j]=A[i+40][j];
study[i+30][j]=A[i+90][j];
study[i+50][j]=A[i+140][j];
voidrandd5(doubleA[][6],doubletest[][7])
test[i-10][j]=A[i][j];
test[i+50][j]=A[i+100][j];
//搜索检验位置样本x的最近邻是否存在:
voidSEARCH(doubleA[],intlow,inthigh)
inti=low;
intj=high;
doublepivot=A[low];
while(i<
j)
while(i<
j&
&
A[j]>
=pivot)
j--;
A[i]=A[j];
A[i]<
i++;
A[j]=A[i];
A[i]=pivot;
if(low<
i-1)
SEARCH(A,low,i-1);
if(high>
i+1)
SEARCH(A,i+1,high);
//结果输出聚类结果及正确率:
voidOUTPUT(doubletest[][7],FILE*fp)
inti,j,n=0;
7;
fprintf(fp,"
%lf"
test[i][j]);
fprintf(fp,"
if(test[i][6]==test[i][5])
n++;
正确率%lf\n"
n/90.0);
//找最大值
doubleMAX(doublet1,doublet2,doublet3)
if(t1>
=t2&
t1>
=t3)return1;
elseif(t2>
=t3&
t2>
=t1)return2;
elsereturn3;
//求距离函数
doubledistance(inti,intj,doublesample[][6],doubletest[][7])
intm;
doubles=0;
for(m=1;
m<
4;
m++)
s+=pow(test[i][m]-sample[j][m],2);
s=sqrt(s);
returns;
附:
k=7时归类结果
Thefirstresult:
21.0000005.4000003.4000001.7000000.2000001.0000001.000000
22.0000005.1000003.7000001.5000000.4000001.0000001.000000
23.0000004.6000003.6000001.0000000.2000001.0000001.000000
24.0000005.1000003.3000001.7000000.5000001.0000001.000000
25.0000004.8000003.4000001.9000000.2000001.0000001.000000
26.0000005.0000003.0000001.6000000.2000001.0000001.000000
27.0000005.0000003.4000001.6000000.4000001.0000001.000000
28.0000005.2000003.5000001.5000000.2000001.0000001.000000
29.0000005.2000003.4000001.4000000.2000001.0000001.000000
30.0000004.7000003.2000001.6000000.2000001.0000001.000000
31.0000004.8000003.1000001.6000000.2000001.0000001.000000
32.0000005.4000003.4000001.5000000.4000001.0000001.000000
33.0000005.2000004.1000001.5000000.1000001.0000001.000000
34.0000005.5000004.2000001.4000000.2000001.0000001.000000
35.0000004.9000003.1000001.5000000.2000001.0000001.000000
36.0000005.0000003.2000001.2000000.2000001.0000001.000000
37.0000005.5000003.5000001.3000000.2000001.0000001.000000
38.0000004.9000003.6000001.4000000.1000001.0000001.000000
39.0000004.4000003.0000001.3000000.2000001.0000001.000000
40.0000005.1000003.4000001.5000000.2000001.0000001.000000
41.0000005.0000003.5000001.3000000.3000001.0000001.000000
42.0000004.5000002.3000001.3000000.3000001.0000001.000000
43.0000004.4000003.2000001.3000000.2000001.0000001.000000
44.0000005.0000003.5000001.6000000.6000001.0000001.000000
45.0000005.1000003.8000001.9000000.4000001.0000001.000000
46.0000004.8000003.0000001.4000000.3000001.0000001.000000
47.0000005.1000003.8000001.6000000.2000001.0000001.000000
48.0000004.6000003.2000001.4000000.2000001.0000001.000000
49.0000005.3000003.7000001.5000000.2000001.0000001.000000
50.0000005.0000003.3000001.4000000.2000001.0000001.000000
71.0000005.9000003.2000004.8000001.8000002.0000002.000000
72.0000006.1000002.8000004.0000001.3000002.0000002.000000
73.0000006.3000002.5000004.9000001.5000002.0000002.000000
74.0000006.1000002.8000004.7000001.2000002.0000002.000000
75.0000006.4000002.9000004.3000001.3000002.0000002.000000
76.0000006.6000003.0000004.4000001.4000002.0000002.000000
77.0000006.8000002.8000004.8000001.4000002.0000002.000000
78.0000006.7000003.0000005.0000001.7000002.0000003.000000
79.0000006.0000002.9000004.5000001.5000002.0000002.000000
80.0000005.7000002.6000003.5000001.0000002.0000002.000000
81.0000005.5000002.4000003.8000001.1000002.0000002.000000
82.0000005.5000002.4000003.7000001.0000002.0000002.000000
83.0000005.8000002.7000003.9000001.2000002.0000002.000000
84.0000006.0000002.7000005.1000001.6000002.0000003.000000
85.0000005.4000003.0000004.5000001.5000002.0000002.000000
86.0000006.0000003.4000004.5000001.6000002.0000002.000000
87.0000006.7000003.1000004.7000001.5000002.0000002.000000
88.0000006.3000002.3000004.4000001.3000002.0000002.000000
89.0000005.6000003.0000004.1000001.3000002.0000002.000000
90.0000005.5000002.5000004.0000001.3000002.0000002.000000
91.0000005.5000002.6000004.4000001.2000002.0000002.000000
92.0000006.1000003.0000004.6000001.4000002.0000002.000000
93.0000005.8000002.6000004.0000001.2000002.0000002.000000
94.0000005.0000002.3000003.3000001.0000002.0000002.000000
95.0000005.6000002.7000004.2000001.3000002.0000002.000000
96.0000005.7000003.0000004.2000001.2000002.0000002.000000
97.0000005.7000002.9000004.2000001.3000002.0000002.000000
98.0000006.2000002.9000004.3000001.3000002.0000002.000000
99.0000005.1000002.5000003.0000001.1000002.0000002.000