天天看點

matlab fprintf tabular,Matlab貝葉斯工具箱BNT模組化

采用matlab的貝葉斯工具箱BNT完成以下“過勞死”問題的模組化。

使用BNT工具箱中的貝葉斯結構、參數學習指令。

(t=ture;  f=false)

matlab fprintf tabular,Matlab貝葉斯工具箱BNT模組化

參數學習代碼:

%進階人工智能%

%BNT的參數學習%

N=5;  %四個節點分别是國家政策C,學校政策U,工作壓力大W,身體狀況差B,過勞死D

dag=zeros(N,N);  %網絡連接配接矩陣初始化

C=1;U=2;W=3;B=4;D=5;  %初始化節點順序

dag(C,U)=1;  %定義節點之間的連接配接關系

dag(U,[W B])=1;

dag(W,D)=1;

dag(B,D)=1;

discrete_nodes=1:N;  %離散節點

node_sizes=2*ones(1,N);  %節點狀态數

%建立網絡架構

bnet=mk_bnet(dag,node_sizes,'names',{'國家政策(C)','學校政策(U)','工作壓力大(W)','身體狀況差(B)','過勞死(D)'},'discrete',discrete_nodes);

%手工構造條件機率CPT表

bnet.CPD{C} = tabular_CPD(bnet,C,[0.5 0.5]);

bnet.CPD{U} = tabular_CPD(bnet,U,[0.95 0.01 0.05 0.99]);

bnet.CPD{W} = tabular_CPD(bnet,W,[0.9 0.05 0.1 0.95]);

bnet.CPD{B} = tabular_CPD(bnet,B,[0.3 0.01 0.7 0.99]);

bnet.CPD{D} = tabular_CPD(bnet,D,[0.335 0.3 0.05 0 0.665 0.7 0.95 1]);

%畫出建立好的貝葉斯網絡

figure

draw_graph(dag)

%手動構造樣本資料samples:

nsamples=20000;

samples=cell(N,nsamples);

for i=1:nsamples

samples(:,i)=sample_bnet(bnet);

end

data=cell2num(samples);

bnet2 = mk_bnet(dag,node_sizes,'discrete',discrete_nodes);

%手動構造條件機率表cpt

seed=0;

rand('state',seed);

bnet2.CPD{C}=tabular_CPD(bnet2,C);

bnet2.CPD{U}=tabular_CPD(bnet2,U);

bnet2.CPD{W}=tabular_CPD(bnet2,W);

bnet2.CPD{B}=tabular_CPD(bnet2,B);

bnet2.CPD{D}=tabular_CPD(bnet2,D);

%手動構造得到的樣本作為訓練集代入learn_params()函數進行學習

bnet3=learn_params(bnet2,data);

%檢視學習後的參數

CPT3=cell(1,N);

for i=1:N

s=struct(bnet3.CPD{i});

CPT3{i}=s.CPT;

end

fprintf('輸出學習後的過勞死節點參數:\n');

dispcpt(CPT3{5});

%檢視原來節點參數後的參數

CPT=cell(1,N);

for i=1:N

s=struct(bnet.CPD{i});

CPT{i}=s.CPT;

end

fprintf('輸出真實的過勞死節點參數:\n');

dispcpt(CPT{5});

運作結果:

ans =

0.4500    0.5500    0.2833    0.6167    0.5500

matlab fprintf tabular,Matlab貝葉斯工具箱BNT模組化

輸出學習後的過勞死節點參數:

1 1 : 0.3233 0.6767

2 1 : 0.2458 0.7542

1 2 : 0.0488 0.9512

2 2 : 0.0000 1.0000

輸出真實的過勞死節點參數:

1 1 : 0.3350 0.6650

2 1 : 0.3000 0.7000

1 2 : 0.0500 0.9500

2 2 : 0.0000 1.0000

結構學習代碼:

N=5;%四個節點分别是國家政策C,學校政策U,工作壓力大W,身體狀況差B,過勞死D

dag=zeros(N,N);%網絡連接配接矩陣初始化

C=1;U=2;W=3;B=4;D=5;%初始化節點順序

dag(C,U)=1;%定義節點之間的連接配接關系

dag(U,[W B])=1;

dag(W,D)=1;

dag(B,D)=1;

discrete_nodes=1:N;%離散節點

node_sizes=2*ones(1,N);%節點狀态數

%建立網絡架構

bnet=mk_bnet(dag,node_sizes,'names',{'國家政策(C)','學校政策(U)','工作壓力大(W)','身體狀況差(B)','過勞死(D)'},'discrete',discrete_nodes);

%手工構造條件機率CPT表

bnet.CPD{C} = tabular_CPD(bnet,C,[0.5 0.5]);

bnet.CPD{U} = tabular_CPD(bnet,U,[0.95 0.01 0.05 0.99]);

bnet.CPD{W} = tabular_CPD(bnet,W,[0.9 0.05 0.1 0.95]);

bnet.CPD{B} = tabular_CPD(bnet,B,[0.3 0.01 0.7 0.99]);

bnet.CPD{D} = tabular_CPD(bnet,D,[0.335 0.3 0.05 0 0.665 0.7 0.95 1]);

%畫出建立好的貝葉斯網絡

% figure

% draw_graph(dag)

%手動構造樣本資料samples:

nsamples=2000;

samples=cell(N,nsamples);

for i=1:nsamples

samples(:,i)=sample_bnet(bnet);

end

data=cell2num(samples);

%結構學習

order=[1 2 3 4 5];   % 節點次序

ns=[2 2 2 2 2];       % 節點屬性值的個數

max_fan_in=2;         % 最大父節點數目

dag2 = learn_struct_K2(data,ns,order,'max_fan_in',max_fan_in);

bnet2=mk_bnet(dag2,node_sizes,'names',{'國家政策(C)','學校政策(U)','工作壓力大(W)','身體狀況差(B)','過勞死(D)'},'discrete',discrete_nodes);

%手工構造條件機率CPT表

bnet2.CPD{C} = tabular_CPD(bnet2,C,[0.5 0.5]);

bnet2.CPD{U} = tabular_CPD(bnet2,U,[0.95 0.01 0.05 0.99]);

bnet2.CPD{W} = tabular_CPD(bnet2,W,[0.9 0.05 0.1 0.95]);

bnet2.CPD{B} = tabular_CPD(bnet2,B,[0.3 0.01 0.7 0.99]);

bnet2.CPD{D} = tabular_CPD(bnet2,D,[0.335 0.3 0.05 0 0.665 0.7 0.95 1]);

figure

draw_graph(dag);  %畫出建立好的貝葉斯網絡

CPT2=cell(1,N);

for i=1:N

s=struct(bnet2.CPD{i});

CPT2{i}=s.CPT;

end

fprintf('輸出結構學習之後過勞死節點參數:\n');

dispcpt(CPT2{5});

運作結果:

matlab fprintf tabular,Matlab貝葉斯工具箱BNT模組化

輸出結構學習之後過勞死節點參數:

1 1 : 0.3350 0.6650

2 1 : 0.3000 0.7000

1 2 : 0.0500 0.9500

2 2 : 0.0000 1.0000