SOM_choose.m
上传用户:sdcdgc2008
上传日期:2014-04-13
资源大小:365k
文件大小:4k
- %% som mapping and distinct the classs of significant and not
- %------------------------ Mapping the positve training samples--------------------------------------
- function [pos_train_sample,pos_test_sample,neg_train_sample,neg_test_sample,net] = SOM_choose(pos_sample,neg_sample,sizeout,SOM_epochs,net)
- P = [pos_sample, neg_sample];
- [feature_num sample_num] = size(P);
- pos_num = size(pos_sample,2);
- neg_num = size(neg_sample,2);
- SOM_out_num = [sizeout sizeout]; % 输出节点数目
- SOM_input_num = feature_num; % 输入节点数目
-
- MinMaxValue = [zeros(feature_num ,1),ones(feature_num,1)];
- % net = newsom( MinMaxValue,SOM_out_num);
- % net.trainParam.epochs = SOM_epochs;
- % net.trainParam.show = 1000;
-
- % -------- som training
- begin_time = clock;
- % [net,tr,Y,E,Pf,Af] = train(net,P);
- Y = sim(net,P);
- cost_time = etime(clock,begin_time)
- % map distribution
- [Maxvalue max_num] = max(Y);
- distr = zeros(pos_num,2);
- for(i=1:sample_num)
- distr(i,1) = floor((max_num(i)-1)/sizeout)+1;
- distr(i,2) = max_num(i)-( distr(i,1) - 1 )*sizeout+1;
- distr(i,2) = rem(max_num(i)-1,sizeout)+1;
- end
-
- distr_pos = distr(1:pos_num,:);
- distr_neg = distr(pos_num+1:sample_num,:);
- % plot the distribution of the map result
- figure
- for(i=1:pos_num)
- hold on
- plot(distr_pos(i,1),distr_pos(i,2),'r*');
- end
- for(i=1:neg_num)
- hold on
- plot(distr_neg(i,1),distr_neg(i,2),'bsquare');
- end
- title('som result distribution ')
-
- % cal the center of the positive class
- center = floor(mean(distr_pos,1));
- plot(center(1),center(2),'rO','LineWidth',2,...
- 'MarkerEdgeColor','k',...
- 'MarkerFaceColor','g',...
- 'MarkerSize',10);
- % cal the distance of the positive class, the mean distance, the max_num distance
- dislength = zeros(pos_num,1);
- dislength = dist(distr_pos,center');
- mean_dist = mean( dislength,1);
- max_dist = max( dislength);
- thresh_dist = floor((mean_dist+max_dist)/2);
- train_num = 0;
- test_num = 0;
- train_som = zeros(feature_num,pos_num);
- test_som = zeros(feature_num,pos_num);
- for(i=1:pos_num)
- if( dislength(i)<=thresh_dist)
- train_num = train_num + 1;
- train_som(:,train_num) = pos_sample(:,i);
- % choose the train sample
- hold on
- plot(distr_pos(i,1),distr_pos(i,2),'c+');
- else
- test_num = test_num + 1;
- test_som(:,test_num) = pos_sample(:,i);
- end
- end
- pos_train_sample = train_som(:,1:train_num);
- pos_test_sample = test_som(:,1:test_num);
-
-
- % cal the center of the positive class
- center = floor(mean(distr_neg,1));
- hold on
- plot(center(1),center(2),'rO','LineWidth',2,...
- 'MarkerEdgeColor','k',...
- 'MarkerFaceColor','g',...
- 'MarkerSize',10);
- % cal the distance of the positive class, the mean distance, the max_num distance
- dislength = zeros(neg_num,1);
- dislength = dist(distr_neg,center');
- mean_dist = mean( dislength,1);
- max_dist = max( dislength);
- thresh_dist = floor((mean_dist+max_dist)/2);
- train_num = 0;
- test_num = 0;
- train_som = zeros(feature_num,neg_num);
- test_som = zeros(feature_num,neg_num);
- for(i=1:neg_num)
- if( dislength(i)<=thresh_dist)
- train_num = train_num + 1;
- train_som(:,train_num) = neg_sample(:,i);
- % choose the train sample
- hold on
- plot(distr_neg(i,1),distr_neg(i,2),'Mx');
- else
- test_num = test_num + 1;
- test_som(:,test_num) = neg_sample(:,i);
- end
- end
- neg_train_sample = train_som(:,1:train_num);
- neg_test_sample = test_som(:,1:test_num);