SOM_choose.m
上传用户:sdcdgc2008
上传日期:2014-04-13
资源大小:365k
文件大小:4k
开发平台:

Matlab

  1. %%  som mapping and distinct the classs of significant and not
  2. %------------------------  Mapping the positve training samples--------------------------------------
  3.  function [pos_train_sample,pos_test_sample,neg_train_sample,neg_test_sample,net]  = SOM_choose(pos_sample,neg_sample,sizeout,SOM_epochs,net)
  4.    P = [pos_sample, neg_sample];
  5.    [feature_num sample_num] = size(P); 
  6.    pos_num = size(pos_sample,2);
  7.    neg_num = size(neg_sample,2);
  8.    SOM_out_num = [sizeout  sizeout];                  % 输出节点数目   
  9.    SOM_input_num = feature_num;           % 输入节点数目
  10.    
  11.     MinMaxValue = [zeros(feature_num ,1),ones(feature_num,1)]; 
  12. %     net  = newsom( MinMaxValue,SOM_out_num);
  13. %     net.trainParam.epochs = SOM_epochs;   
  14. %     net.trainParam.show = 1000;
  15.    
  16. %     -------- som training 
  17.     begin_time = clock;
  18. %     [net,tr,Y,E,Pf,Af] = train(net,P);
  19.     Y = sim(net,P);
  20.     cost_time = etime(clock,begin_time)
  21.     %  map distribution  
  22.     [Maxvalue max_num] = max(Y);    
  23.     distr = zeros(pos_num,2);
  24.     for(i=1:sample_num)
  25.         distr(i,1) = floor((max_num(i)-1)/sizeout)+1;
  26.         distr(i,2) = max_num(i)-( distr(i,1) - 1 )*sizeout+1;
  27.         distr(i,2) = rem(max_num(i)-1,sizeout)+1;
  28.     end
  29.     
  30.     distr_pos = distr(1:pos_num,:);
  31.     distr_neg = distr(pos_num+1:sample_num,:);
  32.     % plot the distribution of the map result
  33.     figure
  34.     for(i=1:pos_num)
  35.         hold on
  36.         plot(distr_pos(i,1),distr_pos(i,2),'r*');
  37.     end
  38.     for(i=1:neg_num)
  39.         hold on
  40.         plot(distr_neg(i,1),distr_neg(i,2),'bsquare');
  41.     end
  42.     title('som result distribution ')
  43.     
  44.     % cal the center of the positive class
  45.     center = floor(mean(distr_pos,1));
  46.     plot(center(1),center(2),'rO','LineWidth',2,...
  47.                 'MarkerEdgeColor','k',...
  48.                 'MarkerFaceColor','g',...
  49.                 'MarkerSize',10);
  50.     % cal the distance of the positive class, the mean distance, the max_num distance
  51.     dislength = zeros(pos_num,1);
  52.     dislength = dist(distr_pos,center');
  53.     mean_dist = mean( dislength,1);
  54.     max_dist = max( dislength);
  55.     thresh_dist = floor((mean_dist+max_dist)/2);
  56.     train_num = 0;
  57.     test_num = 0;
  58.     train_som = zeros(feature_num,pos_num);
  59.     test_som = zeros(feature_num,pos_num);
  60.     for(i=1:pos_num)
  61.         if( dislength(i)<=thresh_dist)
  62.             train_num = train_num + 1;
  63.             train_som(:,train_num) = pos_sample(:,i);
  64.             % choose the train sample
  65.             hold on 
  66.             plot(distr_pos(i,1),distr_pos(i,2),'c+');
  67.         else
  68.             test_num = test_num + 1;
  69.             test_som(:,test_num) = pos_sample(:,i);
  70.         end
  71.     end
  72.     pos_train_sample = train_som(:,1:train_num);
  73.     pos_test_sample = test_som(:,1:test_num);
  74.     
  75.     
  76.    % cal the center of the positive class
  77.     center = floor(mean(distr_neg,1));
  78.    hold on 
  79.     plot(center(1),center(2),'rO','LineWidth',2,...
  80.                 'MarkerEdgeColor','k',...
  81.                 'MarkerFaceColor','g',...
  82.                 'MarkerSize',10);
  83.     % cal the distance of the positive class, the mean distance, the max_num distance
  84.     dislength = zeros(neg_num,1);
  85.     dislength = dist(distr_neg,center');
  86.     mean_dist = mean( dislength,1);
  87.     max_dist = max( dislength);
  88.     thresh_dist = floor((mean_dist+max_dist)/2);
  89.     train_num = 0;
  90.     test_num = 0;
  91.     train_som = zeros(feature_num,neg_num);
  92.     test_som = zeros(feature_num,neg_num);
  93.     for(i=1:neg_num)
  94.         if( dislength(i)<=thresh_dist)
  95.             train_num = train_num + 1;
  96.             train_som(:,train_num) = neg_sample(:,i);
  97.             % choose the train sample
  98.             hold on 
  99.             plot(distr_neg(i,1),distr_neg(i,2),'Mx');
  100.         else
  101.             test_num = test_num + 1;
  102.             test_som(:,test_num) = neg_sample(:,i);
  103.         end
  104.     end
  105.     neg_train_sample = train_som(:,1:train_num);
  106.     neg_test_sample = test_som(:,1:test_num);