Você está na página 1de 5

Pattern Recognition Exercise 1

Toca Cosmin

File 1 K-Nearest Neighbors Main Program %% ============= Loading data =============== % clear all close all load testx load testy load trainingx load trainingy % %% =============1.2 Exploratory Data Analysis =============== % disp(Contents of workspace :) whos training=zeros(1,118); test=zeros(1,118); for i=1:size(trainingx,1) training(i)=mean(trainingx(i,:)); end for i=1:size(testx,1) test(i)=mean(testx(i,:)); end figure subplot(2,1,1); plot(training), hold on title(Mass Spectrum Plot of Training), xlabel(mass), ylabel(values), subplot(2,1,2); plot(test), hold on title(Mass Spectrum Plot of Test), xlabel(mass), ylabel(values),

b=testx.*repmat(testy,118,1); a=trainingx.*repmat(trainingy,118,1); %we will consider that 1 represent cancer, where the values are ~= 0 in output matrix(a,b %there is cancer % now I do not need to use loops to check where is cancer and where not

trainingc=zeros(1,size(a,1)); testc=zeros(1,size(b,1)); for i=1:size(a,1) trainingc(i)=mean(a(i,:)); end for i=1:size(b,1) testc(i)=mean(b(i,:)); end figure subplot(2,1,1);plot(training,-g), hold on, plot(trainingc,-r), title(Mass Spectrum of Cancer (Training)), xlabel(mass), 2 ylabel(values),legend(Connective Tissue,Cancer,2); subplot(2,1,2); plot(test,-g),hold on, plot(testc,-r), title(Mass Spectrum of Cancer(Test)), xlabel(mass), ylabel(values),

%% =============1.3 K-Nearest Neighbor Classifier =============== % %% ====1.3.1 Distance function computation using loops =============== % tic dist_loop = calc_dist_mat_loop_a_b(trainingx,testx) toc %% ====1.3.2 Distance function computation using vectorization ============ % tic dist_squ = calc_dist_mat_squ_a_b(trainingx, testx) toc %% ====1.3.2 MATLAB Euclidean Distance ============ % tic dist(testx, testy) toc

%% ====1.3.3 Write a k-nearest neighbor classifier ============ % neighbors = kNearestN(trainingx, testx,5); neighbors %% ====1.3.4 Parameter adjustment ============ % prompt = {Enter K :}; dlg_title = Input parameter; num_lines = 1; def = {60}; answer = inputdlg(prompt,dlg_title,num_lines,def); neighbors = kNearestN(a, b,str2double(answer)) plot(neighbors) %% ====1.4. Error likelihood of k-nearest neighbor classification ======== %

File 2 Distance function computation using loops function dist_loop=calc_dist_mat_loop_a_b(trainingx, testx) if (size(trainingx,1) == size(testx,1)) n = size(testx,2); m = size(trainingx,2); dist_loop = zeros(m,n); for i=1:m dist = sum((repmat(trainingx(:,i),n,1)-testx).^2,2); dist_loop(i,:) = sqrt(dist); end else errordlg(The input matrices must have the same size on first position !, ERROR !); end

File 3 Distance function computation using vectorization function dist_squ=calc_dist_mat_squ_a_b(trainingx, testx) if (size(trainingx,1) == size(testx,1)) dist_squ = sqrt(abs(repmat(sum(trainingx.^2,1),[1 size(sum(testx.^2,1),2)]) + repmat(sum(testx.^2,1),[size(sum(trainingx.^2,1),2) 1]) - 2*trainingx*testx)); else errordlg(The input matrices must have the same size on first position !, ERROR !); end

File 4 K-Nearest Neighbor Classifier function neighbors = kNearestN(trainingx, testx,k) dist_squ = calc_dist_mat_squ_a_b(trainingx, testx); m = size(trainingx,2); neighbors = zeros(m,k); for i=1:m [~,pos] = sort(dist_squ(i,:),ascend); neighbors(i,:) = pos(1:k); end

Você também pode gostar