Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A report detailing the performance of the algorithm you implemented and the performance of the Matlab implementation of the algorithm. So I need to add

A report detailing the performance of the algorithm you implemented and the performance of the Matlab implementation of the algorithm. So I need to add the performance of a prediction algorithm would refer to how many samples the algorithm was able to predict as the correct class.

This is my code so far:

%% Clear all close all, clear all, clc, format compact %% Download data load fisheriris X = meas(:,3:4); clear meas species %% Plot data on scatter fig = figure; plot(X(:,1),X(:,2),'ko','MarkerEdgeColor','blue') title 'Iris Data'; xlabel 'Petal Lengths (cm)'; ylabel 'Petal Widths (cm)';
%% Start with k random points k = 2; xmin = min(X(:,1)); xmax = max(X(:,1)); ymin = min(X(:,2)); ymax = max(X(:,2)); rx = (xmax-xmin).*rand(k,1) + xmin; ry = (ymax-ymin).*rand(k,1) + ymin; groupid = decimalToBinaryVector(linspace(1,k,k)); r = [groupid rx ry]; clear xmin xmax ymin ymax rx ry

%% Initialize cluster % Add binary columns to mark which cluster the row is currently in X = [X repmat(zeros(1,k), size(X,1),1)]; % Also add column that shows distance away from the centroid of that % cluster X = [X zeros(size(X,1),1)];
%% Add cluster to all data % Loop through each element in X assigning it the nearest cluster for row=1:size(X,1) % join row with k points row1 = [r repmat(X(row,[1 2]), size(r,1),1)]; % calculate euclidean distance between all k points dist = sqrt(((row1(:,3) - row1(:,5)).^2) + ((row1(:,4) - row1(:,6)).^2)); % find k point with minimum distance [v1,r1] = min(dist); % add id of nearest k point X(row,[end - k, end - 1]) = groupid(r1,:); % add distance away from centroid of cluster X(row,end) = v1; end

%% Calculate J % Store J in a 1-column matrix J = sum(X(:,end));

%% Calculate new centroid for clusters [~,~,ind]=unique(X(:,3:end - 1),'rows'); meanX = accumarray(ind, X(:,1), [k 1], @mean); meanY = accumarray(ind, X(:,2), [k 1], @mean); rNew = [groupid meanX meanY];

%% Show scatter plot fig = figure; hold on gscatter(X(:,1),X(:,2),ind) gscatter(meanX,meanY,unique(ind),'','x',20) title 'Iris Data'; xlabel 'Petal Lengths (cm)'; ylabel 'Petal Widths (cm)'; hold off

%% If new centroids are different to old centroids, repeat if isequal(r,rNew) == 0 r = rNew; end

%% Repeat routine until convergence % Set condition to exit loop condition = true; % Create holder for iteration number iterationnumber = 1; while condition % Loop through each element in X assigning it the nearest cluster for row=1:size(X,1) % join row with k points row1 = [r repmat(X(row,[1 2]), size(r,1),1)]; % calculate euclidean distance between all k points dist = sqrt(((row1(:,3) - row1(:,5)).^2) + ((row1(:,4) - row1(:,6)).^2)); % find k point with minimum distance [vi,r1] = min(dist); % add id of nearest k point X(row,[end - k, end - 1]) = groupid(r1,:); % add distance away from centroid of cluster X(row,end) = v1; end % Calculate J j = sum(X(:,end)); J = [J;j]; % Calculate new centroid for clusters [~,~,ind]=unique(X(:,3:end - 1),'rows'); meanX = accumarray(ind, X(:,1), [k 1], @mean); meanY = accumarray(ind, X(:,2), [k 1], @mean); rNew = [groupid meanX meanY]; % Close current figure if ishandle(fig) close(fig); end % Show scatter plot fig = figure; hold on gscatter(X(:,1),X(:,2),ind) gscatter(meanX,meanY,unique(ind),'','x',20) title(sprintf('Iris Data. Iteration: %d', iterationnumber)); xlabel 'Petal Lengths (cm)'; ylabel 'Petal Widths (cm)'; hold off % Pause for 1 second to show chart pause(1); % If centroids have changed, repeat if isequal(r,rNew) == 0 r = rNew; condition = true; else condition = false; end iterationnumber = iterationnumber + 1; end
%% Show line plot for J fig = figure plot(linspace(1,size(J,1),size(J,1)),J,'b-o') title('Iris Data. J minimisation'); xlabel 'Iteration'; ylabel 'J';

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

f. Did they change their names? For what reasons?

Answered: 1 week ago