% Sequential forward input selection using leave-one-out error %====== load the iris data set close all % Close all figure windows clear all % Clear all variables in memory %loadiris; loadaba; % ====== Data normalization to have zero mean and unity variance r.v. new_feature = normal(feature); data = [new_feature output]; all_input_n = size(data,2)-1; % No. of all possible inputs input_n = 4; % Select up to input_n inputs model_n = input_n*(2*all_input_n-input_n+1)/2; % No. of KNNR models all_input_index = zeros(model_n, input_n); % List of selected inputs all_misclassify = zeros(model_n, 1); % No. of misclassification K = 1; % "K" in KNNR % ====== You need to replace the following lines to generate % "all_input_index" and "all_misclassify" via sequential forward search all_input_index = [ 1 0 0 0; % input 1 is selected 2 0 0 0; % input 2 is selected 3 0 0 0; % input 3 is selected 4 0 0 0; % input 4 is selected 5 0 0 0; % input 5 is selected 6 0 0 0; % input 6 is selected 7 0 0 0; % input 7 is selected 8 0 0 0; % input 8 is selected 5 1 0 0; % inputs 5 and 1 are selected 5 2 0 0; % inputs 5 and 2 are selected 5 3 0 0; % inputs 5 and 3 are selected 5 4 0 0; % inputs 5 and 4 are selected 5 6 0 0; % inputs 5 and 6 are selected 5 7 0 0; % inputs 5 and 7 are selected 5 8 0 0; % inputs 5 and 8 are selected 5 8 1 0; % inputs 5, 8 and 1 are selected 5 8 2 0; % inputs 5, 8 and 2 are selected 5 8 3 0; % inputs 5, 8 and 3 are selected 5 8 4 0; % inputs 5, 8 and 4 are selected 5 8 6 0; % inputs 5, 8 and 6 are selected 5 8 7 0; % inputs 5, 8 and 7 are selected 5 7 8 1; % inputs 5, 8, 7 and 1 are selected 5 7 8 2; % inputs 5, 8, 7 and 2 are selected 5 7 8 3; % inputs 5, 8, 7 and 3 are selected 5 7 8 4; % inputs 5, 8, 7 and 4 are selected 5 7 8 6]; % inputs 5, 8, 7 and 6 are selected all_misclassify = rand(model_n, 1)*100; % ====== Don't change anything below this line! % ====== Display the results figTitle = 'Sequential Input Selection'; figure('Name', figTitle, 'NumberTitle', 'off'); x = (1:model_n)'; error_rate = all_misclassify/size(data,1)*100; subplot(211); plot(x, error_rate, '-', x, error_rate, 'co'); tmp = x(:, ones(1, 3))'; X = tmp(:); tmp = [zeros(model_n, 1) error_rate nan*ones(model_n, 1)]'; Y = tmp(:); hold on; plot(X, Y, 'g'); hold off; [a, b] = min(error_rate); fprintf('Minimal error rate = %.1f%%.\n', a); fprintf('Selected inputs: '); tmp = all_input_index(b, :); tmp(find(tmp==0))=[]; for p = 1:length(tmp), fprintf(' %s', deblank(input_name(tmp(p), :))); end fprintf('\n'); hold on; plot(b, a, '*'); hold off; axis([1 model_n -inf inf]); set(gca, 'xticklabels', []); ylabel('LOU Error Rates (%)'); grid on h = findobj(gcf, 'type', 'line'); set(h, 'linewidth', 2) % ====== Add text of input variables for k = 1:model_n, index = all_input_index(k, :); index(find(index==0))=[]; leng = length(index); input_string = []; for l=1:leng, input_string = [input_string, ... deblank(input_name(index(l), :)), ' ']; end text(x(k), 0, input_string); end h = findobj(gcf, 'type', 'text'); set(h, 'rot', 90, 'fontsize', 11, 'hori', 'right'); ylabel('LOU Error Rates (%)'); title('LOU Error Rates for Sequential Input Selection');