Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 91 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
91
Dung lượng
406,62 KB
Nội dung
D:\CD\Noiseless Data\nb_data_run.m Printed at 20:58 on 14 Mar 2005 Page of 11 function nb_data_run() % Used to inference testing for NB Data Strcuture % This is the corrected code !!!!!! fid=fopen('NB Data Structure Full Summary.txt','w'); fprintf(fid,'This is the Full details for NB Data Structure with varying Data Size.'); fprintf(fid,'\n \n') ; fclose(fid); 10 11 fid1=fopen('NB Data Structure Dataset.txt','w'); 12 fclose(fid1); 13 14 fid2=fopen('NB Data Structure Summarised Results.csv','w'); 15 fprintf(fid2,'Nodes,RunNum,TrainNum,ActualCorrect,ActualNegllhood,ActualUnknown,NBCorrect ,NBNegllhood,NBUnknown,NBTime,TANBCorrect,TANBNegllhood,TANBUnknown,TANBLearntime,TANBInf erTime,TANBtime,BNCorrect,BNNegllhood,BNUnknown,BNLearnTime,BNinferTime,BNtime'); 16 fclose(fid2); 17 18 train_counter_table = [100;500;1000;5000;10000]; 19 20 for num_of_nodes = 80:20:100 21 for run = 1:1:20 22 23 fid=fopen('NB Data Structure Full Summary.txt','a'); 24 fid1=fopen('NB Data Structure Dataset.txt','a'); 25 fid2=fopen('NB Data Structure Summarised Results.csv','a'); 26 27 % ***************************************** 28 % Structure 29 % ***************************************** 30 31 N = num_of_nodes + 1; 32 dag = zeros(N,N); 33 dag(N,1:(N-1))=1; % Root Node to all nodes 34 35 % ***************************************** 36 % Creating Network 37 % ***************************************** 38 observed_node_count = 0; 39 observed_nodes = zeros(1,N-1); 40 for observed_col = 1:1:(N-1) 41 observed_node_count = observed_node_count+1; 42 observed_nodes(1,observed_col) = observed_node_count; 43 end 44 45 false = 1; true = 2; 46 ns = 2*ones(1,N); % binary nodes 47 48 bnet = mk_bnet(dag,ns,'observed',observed_nodes); 49 50 % ***************************************** 51 % Inputing Parameters 52 % ***************************************** 53 54 rand('state',sum(100*clock)); 55 for nodule = 1:1:(N-1) 56 bnet.CPD{nodule} = tabular_CPD(bnet, nodule); 57 end 58 59 bnet.CPD{N} = tabular_CPD(bnet, N, [0.5 0.5]); 60 61 % ***************************************************** 62 % To Display the Actual Parameters D:\CD\Noiseless Data\nb_data_run.m Printed at 20:58 on 14 Mar 2005 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 Page of 11 % ***************************************************** fprintf(fid,'\nDisplaying Actual Parameters for Actual Network.\n'); CPT3 = cell(1,N); for i=1:N s=struct(bnet.CPD{i}); % violate object privacy CPT3{i}=s.CPT; fprintf(fid,'\nDisplaying Actual Parameters for node %2.0d',i); fprintf(fid,'\n'); display_CPT(fid,CPT3{i}) end fprintf(fid,'\n'); for train_counter_row = 1:1:5 train_counter = train_counter_table(train_counter_row,1); fprintf(fid,'\nNumber of Nodes %3.0f\n',num_of_nodes); fprintf(fid,'\nRun Number %3.0f\n',run); fprintf(fid,'\n%3.0f training examples\n',train_counter); fprintf(fid1,'\nNumber of Nodes %3.0f\n',num_of_nodes); fprintf(fid1,'\nRun Number %3.0f\n',run); fprintf(fid1,'\n%3.0f training examples\n',train_counter); fprintf(fid2,'\n%3.0f,%3.0f,%3.0f,',num_of_nodes,run,train_counter); % ***************************************************** % To Generate Training Cases % ***************************************************** seed = 0; rand('state', seed); randn('state', seed); ncases_train = train_counter; ncases_test = 10000; initial_gen_data = zeros(N, ncases_train); for m=1:ncases_train initial_gen_data(:,m) = cell2num(sample_bnet(bnet)); end train_data = initial_gen_data(1:(N-1),:)'; train_names = initial_gen_data(N,:)'; save('art_train_data.txt','train_data','-ASCII','-tabs'); save('art_train_names.txt','train_names','-ASCII','-tabs'); nodes_string = int2str(num_of_nodes); run_string = int2str(run); train_string = int2str(train_counter); train_filename = strcat(nodes_string,'_nodes_',run_string,'_run_',train_strin g,'_train_num_TRAIN.txt'); 117 temp = initial_gen_data'; 118 save(train_filename,'temp','-ASCII','-tabs'); 119 clear temp; 120 121 % ***************************************************** 122 % To Generate Test Cases 123 % ***************************************************** 124 125 126 initial_gen_tdata = zeros(N, ncases_test); D:\CD\Noiseless Data\nb_data_run.m Printed at 20:58 on 14 Mar 2005 127 128 129 130 131 Page of 11 for m=1:ncases_test initial_gen_tdata(:,m) = cell2num(sample_bnet(bnet)); end fprintf(fid1,'\nTest Data Run %3.0f with %3.0f Training Examples\n',run,train _counter); 132 133 134 135 136 137 138 139 test_data = initial_gen_tdata(1:(N-1),:)'; test_names = initial_gen_tdata(N,:)'; save('art_test_data.txt','test_data','-ASCII','-tabs'); save('art_test_names.txt','test_names','-ASCII','-tabs'); test_filename = strcat(nodes_string,'_nodes_',run_string,'_run_',train_string ,'_train_num_TEST.txt'); 140 temp = initial_gen_tdata'; 141 save(test_filename,'temp','-ASCII','-tabs'); 142 clear temp; 143 144 % *************************************************************************** ************************************************** 145 % Calculating Accuracy of Actual Prediction or Bayes Error 146 % *************************************************************************** ************************************************** 147 148 xt=load('art_test_data.txt'); 149 yt=load('art_test_names.txt'); 150 151 engine = jtree_ndx_inf_engine(bnet); 152 evidence = cell(1,N); % for inference, the instances should be fed in one at a time 153 class = zeros(size(xt,1),3); 154 llhood = zeros(size(xt,1),1); 155 correct = 0; 156 unknown = 0; 157 158 % Inference on Test 159 160 for row= 1:1:size(xt,1) 161 for col= 1:1:(N-1) 162 evidence{1,col}=xt(row,col); 163 end 164 engine = enter_evidence(engine,evidence'); 165 m = marginal_nodes(engine,N); 166 167 % to output the marginals calculated into classes 168 class(row,1) = m.T(1); 169 class(row,2) = m.T(2); 170 if class(row,1)>class(row,2) 171 class(row,3) = 1; 172 llhood(row,1) = class(row,1); 173 elseif class(row,3)class(row,2) class(row,3) = 1; llhood(row,1) = class(row,1); elseif class(row,3)class(row,2) class(row,3) = 1; llhood(row,1) = class(row,1); elseif class(row,3)class(row,2) class(row,3) = 1; llhood(row,1) = class(row,1); elseif class(row,3)[...]... end fprintf(fid1,'\nTraining Data for Run %3.0f with %3.0f Training Examples\n',run,train_cou nter); for i=1:1:ncases_train for j=1:1:(N-1) fprintf(fid1,'%3.0f,',initial_gen _data( j,i)); end fprintf(fid1,'%3.0f',initial_gen _data( N,i)); fprintf(fid1,'\n'); end train _data = initial_gen _data( 1:(N-1),:)'; train_names = initial_gen _data( N,:)'; save('art_train _data. txt','train _data' ,'-ASCII','-tabs'); save('art_train_names.txt','train_names','-ASCII','-tabs');... correct unknown BN_llhood BN_learn_time BN_infer_time BN_time; fprintf('\nEND OF SIMULATION for Node %3.0f with %3.0f Training Examples for Run %3.0f\n', N, train_counter,run); end % End of Loop for one specific definition of parameters fclose(fid); fclose(fid1); fclose(fid2); end % end % End of Loop for a run of a number of nodes % ***************************************************** % Additional... BN_llhood BN_learn_time BN_infer_ti me BN_time; 582 583 fprintf('\nEND OF SIMULATION for Node %3.0f with %3.0f Training Examples for Run %3.0f\n' , N, train_counter,run); 584 585 end % End of Loop for one specific definition of parameters 586 fclose(fid); 587 fclose(fid1); 588 fclose(fid2); 589 end % 590 end % End of Loop for a run of a number of nodes 591 592 % *****************************************************... cell2num(sample_bnet(bnet)); end train _data = initial_gen _data( 1:(N-1),:)'; train_names = initial_gen _data( N,:)'; save('art_train _data. txt','train _data' ,'-ASCII','-tabs'); save('art_train_names.txt','train_names','-ASCII','-tabs'); nodes_string = int2str(num _of_ nodes); run_string = int2str(run); train_string = int2str(train_counter); num _of_ links_string = int2str(num _of_ links); D:\CD\Noiseless Data\ bn _data_ run_20.m Printed... correct unknown BN_llhood BN_learn_ time BN_infer_time BN_time; fprintf('\nEND OF SIMULATION for Node %3.0f with %3.0f Training Examples for Run %3.0f\n', N, train_counter,run); end % End of Loop for one specific definition of parameters fclose(fid); fclose(fid1); fclose(fid2); end % end end % End of Loop for a run of a number of nodes % ***************************************************** % Additional... param(1,3:4)=param_temp; Page 12 of 12 D:\CD\Noisy Data\ nb _data_ run_noise.m Printed at 20:58 on 14 Mar 2005 1 2 3 4 5 6 7 8 9 Page 1 of 12 function nb _data_ run_noise() % Used to do inference testing for NB Data Strcuture % This code involves noise in both training and testing sets % This is the corrected code !!!!!! fid=fopen('NB Data Structure Full Summary.txt','w'); fprintf(fid,'This is the Full details for NB Data Structure... inference testing for BN Data Strcuture for 100 Nodes % This code involves noise in both training and testing sets % This is the corrected code !!!!!! fid=fopen('BN Data Structure Full Summary.txt','w'); fprintf(fid,'This is the Full details for NB Data Structure with varying Data Size.'); fprintf(fid,'\n \n') ; 10 fclose(fid); 11 12 fid1=fopen('BN Data Structure Dataset.txt','w');... 14 Mar 2005 1 2 3 4 5 6 7 8 9 Page 1 of 10 function tanb _data_ run() % Used to do inference testing for TANB Data Strcuture % This is used to test out when ordering is opposite to that stated for BN % This is the corrected code !!!!!! fid=fopen('TANB Data Structure Full Summary.txt','w'); fprintf(fid,'This is the Full details for TANB Data Structure with varying Data Size.'); fprintf(fid,'\n ... ***************************************************** initial_gen_tdata = zeros(N, ncases_test); for m=1:ncases_test initial_gen_tdata(:,m) = cell2num(sample_bnet(bnet)); end D:\CD\Noiseless Data\ tanb _data_ run.m Printed at 20:58 on 14 Mar 2005 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 Page 3 of 10 fprintf(fid1,'\nTest Data Run %3.0f with %3.0f Training Examples\n',run,train_counter); for i=1:1:ncases_test for j=1:1:(N-1)... [100;200;300;400;500;1000]; 20 21 for num _of_ nodes = 20:20:100 22 for run = 1:1:20 23 24 fid=fopen('TANB Data Structure Full Summary.txt','a'); 25 fid1=fopen('TANB Data Structure Dataset.txt','a'); 26 fid2=fopen('TANB Data Structure Summarised Results.txt','a'); 27 28 % ***************************************** 29 % TANB Dataset Generating Structure 30 % ***************************************** 31 32 N = num _of_ nodes + ... [100;200;300;400;500;1000]; 20 21 for num _of_ nodes = 20:20:20 22 for num _of_ links = 5:5:20 23 for run = 1:1:20 24 25 fid=fopen('BN Data Structure Full Summary.txt','a'); 26 fid1=fopen('BN Data Structure Dataset.txt','a');... '
'); 611 end D:CDNoiseless Data bn _data_ run_20.m Printed at 20:58 on 14 Mar 2005 Page of 12 function bn _data_ run_20() % Used to inference testing for BN Data Strcuture for 100 Nodes % This code... 21 for num _of_ nodes = 20:20:100 22 for run = 1:1:20 23 24 fid=fopen('TANB Data Structure Full Summary.txt','a'); 25 fid1=fopen('TANB Data Structure Dataset.txt','a'); 26 fid2=fopen('TANB Data