模式识别小作业

  • 格式:docx
  • 大小:151.68 KB
  • 文档页数:9

(1)神经网络模式识别识别加入20%噪声的A-Z 26个字母。

程序代码clear;close all;clc;[alphabet,targets]=prprob;[R,Q]=size(alphabet);[S2,Q]=size(targets);S1=10;P=alphabet;net=newff(minmax(P),[S1,S2],{'logsig' 'logsig'},'traingdx'); net.LW{2,1}=net.LW{2,1}*0.01;net.b{2}=net.b{2}*0.01;T=targets;net.performFcn='sse';net.trainParam.goal=0.1;net.trainParam.show=20;net.trainParam.epochs=5000;net.trainParam.mc=0.95;[net,tr]=train(net,P,T);netn=net;netn.trainParam.goal=0.6;netn.trainParam.epochs=300;T=[targets targets targets targets];for pass=1:10;P=[alphabet,alphabet,...(alphabet+randn(R,Q)*0.1),...(alphabet+randn(R,Q)*0.2)];[netn,tr]=train(netn,P,T);endnetn.trainParam.goal=0.1;netn.trainParam.epochs=500;netn.trainParam.show=5;P=alphabet;T=targets;[netn,tr]=train(netn,P,T);noise_percent=0.2;for k=1:26noisyChar=alphabet(:,k)+randn(35,1)*noise_percent;subplot(6,9,k+floor(k/9.5)*9);plotchar(noisyChar);de_noisyChar=sim(net,noisyChar);de_noisyChar=compet(de_noisyChar);answer=find(de_noisyChar==1);subplot(6,9,k+floor(k/9.5)*9+9);plotchar(alphabet(:,answer));endset(gcf,'Position',[10,60,900,700], 'color','w')运行结果(2)实现最小错误率和最小风险bayes决策w1=input('input the priorp of a1\n');w2=input('input the priorp of a2\n');p1=input('input the similarp of w1\n');p2=input('input the similarp of w2\n');s=input('input the table\n');posteriorp1=w1*p1; %约去总体概率密度的w1的后验概率posteriorp2=w2*p2; %。

w2.。

m=posteriorp1-posteriorp2;n=(s(1,1)-s(2,1) )*posteriorp1-(s(2,2)-s(1,2))*posteriorp2;%考虑风险损失情况下if m>0 disp('the least error probability choice is w1');elseif m==0 disp('the least error probability choices are w1 and w2');elseif m<0 disp('the least error probability choice is w2');endif n>0 disp('the least risk choice is w2');elseif n==0 disp('the least risk choices are w1 and w2');elseif n<0 disp('the least risk choice is w1');end结果(3)支持向量机matlab代码clear all;close allclc;sp=[3,7; 6,6; 4,6;5,6.5] % positive sample pointsnsp=size(sp);sn=[1,2; 3,5;7,3;3,4;6,2.7] % negative sample points nsn=size(sn)sd=[sp;sn]lsd=[true true true true false false false false false]Y = nominal(lsd)figure(1);subplot(1,2,1)plot(sp(1:nsp,1),sp(1:nsp,2),'m+');hold onplot(sn(1:nsn,1),sn(1:nsn,2),'c*');subplot(1,2,2)svmStruct = svmtrain(sd,Y,'showplot',true); 结果(4)matlab实现Kmeans聚类算法X = [randn(100,2).*100;...randn(100,2).*200;randn(100,2).*300;...randn(100,2).*400;randn(100,2).*500;randn(100,2).*600];opts = statset('Display','final');[idx,ctrs]=kmeans(X,6,...'Distance','city',...'Replicates',5,...'Options',opts);plot(X(idx==1,1),X(idx==1,2),'r.','MarkerSize',12)hold onplot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',12)hold onplot(X(idx==3,1),X(idx==3,2),'m.','MarkerSize',12)hold onplot(X(idx==4,1),X(idx==4,2),'g.','MarkerSize',12)hold onplot(X(idx==5,1),X(idx==5,2),'k.','MarkerSize',12)hold onplot(X(idx==6,1),X(idx==6,2),'c.','MarkerSize',12)title('{\bf Kmeans聚类算法图像}' )plot(ctrs(:,1),ctrs(:,2),'kx', 'MarkerSize',12,'LineWidth',2)plot(ctrs(:,1),ctrs(:,2),'ko','MarkerSize',12,'LineWidth',2)plot(ctrs(:,1),ctrs(:,2),'kx', 'MarkerSize',12,'LineWidth',2)plot(ctrs(:,1),ctrs(:,2),'kx', 'MarkerSize',12,'LineWidth',2)plot(ctrs(:,1),ctrs(:,2),'kx', 'MarkerSize',12,'LineWidth',2) plot(ctrs(:,1),ctrs(:,2),'kx', 'MarkerSize',12,'LineWidth',2) plot(ctrs(:,1),ctrs(:,2),'kx', 'MarkerSize',12,'LineWidth',2) legend('Cluster 1','Cluster 2','Cluster 3','Cluster 4','Cluster 5',... 'Cluster 6','Centroids','Location','NW')结果15 iterations, total sum of distances = 17667915 iterations, total sum of distances = 17779515 iterations, total sum of distances = 17667223 iterations, total sum of distances = 176372 16 iterations, total sum of distances = 181658。