1 clear all;
2 close all;
3 clc;
4
5 %生成两组已标记数据
6 randn(\'seed\',1);
7 mu1=[0 0];
8 S1=[0.5 0;
9 0 0.5];
10 P1=mvnrnd(mu1,S1,100);
11
12 mu2=[0 6];
13 S2=[0.5 0;
14 0 0.5];
15 P2=mvnrnd(mu2,S2,100);
16
17 mu3=[6 6];
18 S3=[0.5 0;
19 0 0.5];
20 P3=mvnrnd(mu3,S3,100);
21
22 mu4=[6 0];
23 S4=[0.5 0;
24 0 0.5];
25 P4=mvnrnd(mu4,S4,100);
26
27 P = [P1;P2;P3;P4]\';
28
29 %设置标记
30 T1 = zeros(100,1);
31 T2 = ones(100,1);
32
33 T11 = [T1;T1;T2;T2]\';
34 T22 = [T1;T2;T2;T1]\';
35 %T11 = [T1;T2;T1;T1]\';
36 %T22 = [T1;T1;T1;T2]\';
37
38 net1 = newp([1 1; 1 1],1);
39 net1 = train(net1,P,T11);
40
41 net2 = newp([1 1; 1 1],1);
42 net2 = train(net2,P,T22);
43
44 plotpv(P,[T1;T2;T1;T2]\'); %画出数据
45 plotpc(net1.iw{1,1},net1.b{1,1}) %画出分类线
46 plotpc(net2.iw{1},net2.b{1}) %画出分类线
47
48 %生成测试数据
49 mu2=[3 3 ];
50 S2=[2 0 ;
51 0 2];
52 Q=mvnrnd(mu2,S2,100)\';
53 Y1 = sim(net1,Q) ; %Y是利用感知器net对Q进行分类的结果
54 Y2 = sim(net2,Q);
55 Y = xor(Y1,Y2);
56 figure;
57 plotpv(Q,Y); %画出输入的结果表示的点
58 plotpc(net1.iw{1},net1.b{1}) %画出分类线
59 plotpc(net2.iw{1},net2.b{1}) %画出分类线
对已标记数据分类:
对测试数据分类: