1: %朴素贝叶斯分类器
2:
3: clear
4: tic
5:disp----start----' )
6:
7: distr='normal ';
8: distr='kernel ';
9:
10: %读取数据
1: white _ wine=dataset (xls file,) White_Wine.xlsx );
1: x=double (white _ wine (:1:11 );
13: y=双精度(white _ wine ) :12 );
14:
15: % createacvpartitionobjectthatdefinedthefolds
16:c=cvpartition(y,' holdout ',2);
17:
18: %创建培训集
1933 360 x=x (培训(c,1 ), );
2033 360 y=y (培训(c,1 );
21: %测试集
2:u=x(test(c,1 ), );
2:v=y(test(c,1 ), );
24:
2:Yu=unique(y;
26:NC=length(Yu ); % number of classes
7:Ni=size(x,2 ); % independent variables
8:ns=Length(v; % test set
29:
03: % computeclassprobability
31: for i=1:nc
33:fy(I )=sum double (y==Yu (I ) ) )/length(y ) y;
33:结束
34:
35:交换机磁盘
36:
37: case 'normal '
38:
39: %正规分发
40: % parametersfromtrainingset
41: for i=1:nc
4:Xi=x((y==Yu ) I )、 );
43:mu(I, )=mean ) Xi,1 );
4:sigma(I, )=STD ) Xi,1 );
45:结束
46: % probability for test set
47: for j=1:ns
4:fu=normCDF(Ones(NC,1 ) u ) j, ),mu,sigma );
4:p(j, )=fy.*prod ) fu,2 ) ';
50:结束
51:
52: case 'kernel '
53:
54: %密钥分发
53: % probabilityoftestsetestimatedfromtrainingset
56: for i=1:nc
57: for k=1:ni
8:Xi=x(y==Yu(I ),k ); % thefeatureofdimension-kwithrespecttolabelyu (I ) )。
5:UI=u(3360,k );
60:fustruct(I,k ).f=ksdensity ) Xi,ui );
61:结束
62:结束
63: % re-structure
64: for i=1:ns
65: for j=1:nc
66: for k=1:ni
63:fu(j,k )=fuStruct(j ) j,k ).f ) I;
68:结束
69:结束
7:p(I, )=fy.*prod ) fu,2 ) ';
71:结束
72:
73:以太网
74:
73:集成版(disp ) )。
76:返回
77:
78:结束
79:
8: % getpredictedoutputfortestset
81: [pv0,id]=max(p,[],2 );
8:forI=1:length(id )
83:PV(I,1 )=Yu (id ) I );
84:结束
85:
86: % comparepredictedoutputwithactualoutputfromtestdata
83: conf mat=myconfusionmat (v,pv );
8:磁盘(confusion matrix : ) )
是89:disp(confmat ) )
9:conf=sum(PV==v )/length (PV ) );
91:disp(['accuracy=',num2str ) conf*100 ),' %']
92:
93: toc