⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 start_classify.m

📁 最新的模式识别分类工具箱,希望对朋友们有用!
💻 M
字号:
% Main function for the GUI single-algorithm (main) screen% --------------------------------------------------------% Modified 03/19/02 - Vittorio CastelliNpoints = 100; %Number of points on each axis of the decision regionhm = findobj('Tag', 'Messages'); hParent = get(hm,'Parent'); %Get calling window tagset(hm,'String',''); h =  findobj('Tag', 'TestSetError');set(h, 'String', '');h =  findobj('Tag', 'TrainSetError');set(h, 'String', '');error_method_val = get(findobj('Tag', 'popErrorEstimationMethod'),'Value');error_method_str = get(findobj('Tag', 'popErrorEstimationMethod'),'String');error_method 	 = char(error_method_str(error_method_val(1)));framework_val = get(findobj('Tag', 'frameworkToBeUsed'),'Value');framework_str = get(findobj('Tag', 'frameworkToBeUsed'),'String');framework_str = cellstr(framework_str);framework     = char (framework_str(framework_val(1)));% retrieves the parameters from the interfaceif (isempty(whos('targets')))      set(hm,'String','No targets on workspace. Please load targets.')     breakendif (isempty(whos('features')))  set(hm,'String','No features on workspace. Please load features.')  breakend h = findobj('Tag', 'Redraws');   redraws = str2num(get(h, 'String'));   if isempty(redraws),   set(hm,'String','Please select how many redraws are needed.')        breakelse       if (redraws < 1),     set(hm,'String','Number of redraws must be larger than 0.')         break      end   end      h = findobj('Tag', 'PercentTraining'); percent = str2num(get(h, 'String'));   if strcmp(error_method, 'Holdout'),  if isempty(percent),     set(hm,'String','Please select the percentage of training vectors.')         break;  else         if (floor(percent/100*length(targets)) < 1),           set(hm,'String','Number of training vectors must be larger than 0.')           break;    end       if (percent >= 100),           set(hm,'String','Number of training vectors must be smaller than 100%.')           break;    end       if (floor((1-percent/100)*length(targets)) < 1),           set(hm,'String','Number of test vectors must be larger than 0.')           break        end     endend      % Some variable definitionsNclasses  = find_classes(targets); %Number of classes in targetstest_err  = zeros(Nclasses+1,redraws);   train_err = zeros(Nclasses+1,redraws);  % Find the region for the grid[region,x,y]  = calculate_region(features, [zeros(1,4) Npoints]);    h	      = findobj('Tag', 'Algorithm'); val	      = get(h,'Value');     algorithm     = get(h,'String'); algorithm     = deblank(char(algorithm(val,:)));h	      = findobj('Tag', 'Preprocessing'); val           = get(h,'Value');     preprocessing = get(h,'String'); preprocessing = deblank(char(preprocessing(val,:)));PreprocessingParameters = str2num(char(get(findobj('Tag', 'txtPreprocessingParameters'),'String')));% Get algorithm parameters. % If they don't contain a string, turn them into a numberif strcmp(get(findobj('Tag', 'txtAlgorithmParameters'),'Visible'),'on'),  AlgorithmParameters = char(get(findobj('Tag', 'txtAlgorithmParameters'),'String'));else  AlgorithmParameters = char(get(findobj('Tag', 'txtAlgorithmParametersLong'),'String'));endif (~isempty(str2num(AlgorithmParameters))),  AlgorithmParameters = str2num(AlgorithmParameters);endplot_on = strcmp(get(findobj(gcbf,'Label','&Show center of partitions during training'),'Checked'),'on');%Now that the data is OK, start workingset(gcf,'pointer','watch');hold offplot_scatter(features, targets, hParent)axis(region(1:4))hold ondrawnow%% %if (strcmp(framework, 'Framework_for_Class'))  new_start_classify;else  for i = 1: redraws,      set(hm, 'String', ['Processing iteration ' num2str(i) ' of ' num2str(redraws) ' iterations...']);        %  Make a draw according to the error method chosen    L = length(targets);    switch error_method    case cellstr('Resubstitution')      test_indices  = 1:L;      train_indices = 1:L;          case cellstr('Holdout')      [test_indices, train_indices] = make_a_draw(floor(percent/100*L), L); ...    case cellstr('Cross-Validation')      chunk          = floor(L/redraws);      test_indices   = 1 + (i-1)*chunk : i * chunk;      train_indices  = [1:(i-1)*chunk, i * chunk + 1:L];    end    train_features = features(:, train_indices);        train_targets  = targets (:, train_indices);        test_features  = features(:, test_indices);         test_targets   = targets (:, test_indices);             fprintf('have %d training and %d test samples, fraction = %f\n', ...	length(train_targets), length(test_targets), percent);        %  Preprocess and then find decision region    switch preprocessing    case cellstr('None')      fprintf('Generating decision region\n')      fprintf('algorithm = %s,\n',algorithm);      D = feval(algorithm, train_features, train_targets, AlgorithmParameters, region);       fprintf('Calculating the error\n')      [train_err(:,i), test_err(:,i)] = ...	  calculate_error (D, train_features, train_targets, ...	  test_features, test_targets, region, Nclasses);          case cellstr('PCA')      % preprocessing with PCA      disp('Performing preprocessing\n')      [reduced_features, reduced_targets, uw] = ...	  feval(preprocessing, train_features, train_targets, PreprocessingParameters, region);       disp('Generating decision region')      [region, x, y] = calculate_region(reduced_features, region);          D = feval(algorithm, reduced_features, reduced_targets, AlgorithmParameters, region);       disp('Calculating the error')      [train_err(:,i), test_err(:,i)] = calculate_error (D, reduced_features, reduced_targets, uw*test_features, test_targets, region, Nclasses);                case cellstr('Fishers_linear_discriminant')      disp('Performing preprocessing')      [reduced_features, reduced_targets, w] = feval(preprocessing, train_features, train_targets, [], region);       [region, x, y] = calculate_region(reduced_features, region);          disp('Generating decision region')      D = feval(algorithm, reduced_features, reduced_targets, AlgorithmParameters, region);       disp('Calculating the error')      [train_err(:,i), test_err(:,i)] = calculate_error (D, reduced_features, reduced_targets, [w'*test_features; zeros(1,length(test_targets))], test_targets, region, Nclasses);            % Replot the data      hold off      plot_scatter([w'*features; zeros(1,length(targets))], targets, hParent)      hold on          otherwise      disp('Performing preprocessing')      if strcmp(get(findobj(gcbf, 'Tag', '&Options&SeparatePreprocessing'),'Checked'),'on'),	disp('Perform seperate preprocessing for each class.')	in0 = find(train_targets == 0);	in1 = find(train_targets == 1);	[reduced_features0, reduced_targets0] = ...	    feval(preprocessing, train_features(:,in0), ...	    train_targets(in0), PreprocessingParameters, region, ...	    plot_on);  	[reduced_features1, reduced_targets1] = ...	    feval(preprocessing, train_features(:,in1), ...	    train_targets(in1), PreprocessingParameters, region, ...	    plot_on); 		reduced_features = [reduced_features0, reduced_features1];	reduced_targets  = [reduced_targets0,  reduced_targets1];      else	[reduced_features, reduced_targets] = ...	    feval(preprocessing, train_features, train_targets, ...	    PreprocessingParameters, region, plot_on);       end      pause(1);      plot_process([]);      indices = find(sum(isfinite(reduced_features)) > 0);      reduced_features = reduced_features(:,indices);      reduced_targets  = reduced_targets(:,indices);      if (i == redraws)	%Plot only during the last iteration	plot_scatter(reduced_features, reduced_targets, hParent, 1)	axis(region(1:4))      end      %Show Voronoi diagram      if ~isempty(findobj('Tag','Voronoi diagram')),	%Voronoi diagram figure exists	figure(findobj('Tag','Voronoi diagram'))	clf;      else	figure;	set(gcf,'Tag','Voronoi diagram');      end      hold on      contour(x,y,voronoi_regions(reduced_features, region),length(reduced_targets))      plot_scatter(reduced_features, reduced_targets)      hold off      axis(region(1:4));      grid on;      title('Voronoi regions')      figure(hParent)      if ((sum(reduced_targets) <= 1) & (sum(~reduced_targets) <= 1) & (~strcmp(algorithm,'None')))	error('Too few reduced points (This program needs at least two points of each class). Please restart.')      else	if strcmp(algorithm,'None'),	  %No classification was asked for	  set(gcf,'pointer','arrow');     	  return	end      end      disp('Generating decision region')      D = feval(algorithm, reduced_features, reduced_targets, AlgorithmParameters, region);       disp('Calculating the error')      [train_err(:,i), test_err(:,i)] = calculate_error (D, train_features, train_targets, test_features, test_targets, region, Nclasses);          end        end          %Display error  h =  findobj('Tag', 'TestSetError');  s = 'Test set errors: ';  for j = 1:Nclasses,    s = [s 'Class ' num2str(j) ': ' num2str(mean(test_err(j,:)),2) '. '];  end  s = [s 'Total: ' num2str(mean(test_err(Nclasses+1,:)),2)];  set(h, 'String', s);  h =  findobj('Tag', 'TrainSetError');  h =  findobj('Tag', 'TrainSetError');  s = 'Train set errors: ';  for j = 1:Nclasses,    s = [s 'Class ' num2str(j) ': ' num2str(mean(train_err(j,:)),2) '. '];  end  s = [s 'Total: ' num2str(mean(train_err(Nclasses+1,:)),2)];  set(h, 'String', s);  %Show decision region  [s,h]=contour(x,y,D,1);set(h,'LineWidth',2,'EdgeColor','k');  if strcmp(get(findobj(gcbf,'Label','Shade &Decision Regions'),'Checked'),'on'),    contourf(x,y,D,1);colormap([198 198 255; 240 255 240]/255);  end  %Show Bayes decision region and error (if possible)  hBayes = findobj('Tag','chkBayes');  if ((get(hBayes, 'Value')) & (~isempty(whos('p0')))),    Dbayes = decision_region(m0, s0.^2, w0, m1, s1.^2, w1, p0, region);    [s,h]	 = contour(x,y,Dbayes,1);set(h,'LineWidth',1,'EdgeColor','r');    [classify, Bayes_err]  = classification_error(Dbayes, features, targets, region);    h =  findobj('Tag', 'BayesError');    s = 'Bayes errors: ';    for j = 1:Nclasses,      s = [s 'Class ' num2str(j) ': ' num2str(1 - classify(j,j),2) '. '];    end    s = [s 'Total: ' num2str(mean(1 - diag(classify)),2)];    set(h, 'String', s);  end     grid on  %Replot training points if necessary  if strcmp(get(findobj(gcbf,'Label','Show &Training points'),'Checked'),'on'),    plot_scatter(train_features, train_targets, hParent, 2)  end  hold off  %That's all folks!  s = 'Finished!';  if (redraws > 1),    s = [s ' (Note that only the last decision region is shown)'];  end  set(hm, 'String', s);     set(gcf,'pointer','arrow');end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -