Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the MATLAB solution below for the question above. I need to change the c

ID: 3743693 • Letter: H

Question

Here is the MATLAB solution below for the question above. I need to change the code below to something else, but I need the solutions to match each other. (Also try not to copy answer posted on chegg already)

%
% Pb 1.10
%

clc; clear all; close all; % reset matlab

load mnist_train % load data

ns = 500;   % nb of images to be used
ng = 20;     % nb of groups

digits = digits(:, 1:ns); % use the first ns images
group = randi(ng, 1, ns); % randoml initial group assignment

z = []; DJ = 1; J0 = 0; it =0; cvg=[]; eps = 1e-5; ifg = 0; % Initialization

while DJ>eps

it = it+1; % iteration #

% step 2 of k-means algorithm 4.1
for i=1:ng
I = find(group == i);
        M = digits(:,I);
        z(:,i) = mean(M,2);
end

%% plot the first 3 results
if (ifg<3)

ifg = ifg+1;
figure(ifg)
for k=1:ng
subplot(4,5,k)
imshow(reshape(z(:,k), 28, 28));
end

end

% step 1 of k-means algorithm 4.1
J = 0;
for i=1:ns
u = [];
for j=1:ng
u(j) = norm(digits(:,i)-z(:,j))^2;
end
[p,q] = min(u);
J = J+p;
group(i) = q;
end

J = J/ns;
DJ = abs(J-J0)/J;
J0 = J;


cvg(it) = DJ;

end

%% plot final results
ifg = ifg+1;
figure(ifg)
for k=1:ng
subplot(4,5,k)
imshow(reshape(z(:,k), 28, 28));
end

%% plot convergence of k-means algorithm

ifg=ifg+1;
figure(ifg)
semilogy(cvg,'o-')

%% plot 8 randomly selected images of group ig

ifg=ifg+1;
figure(ifg)

ig = 1;
I = find(group==ig);
is = randi(length(I),1,8); % 8 randomly selected images

for k=1:8
subplot(3,3,k)
ir = I(is(k));
imshow(reshape(digits(:,ir), 28, 28));
end
subplot(3,3,9)
imshow(reshape(z(:,ig), 28, 28));

1.10 The k-means algorithm. In this exercise we apply the k-mcans algorithm to the example in $4.4.1 of the textbook Download the file mnist train.mat from the course website and load it in MATLAB or Octave using the command load mnist-train. This creates two variables: a 784 × 60000 matrix digits and a 1 x 60000 matrix labels. We will not need labels. Each column of digits is a 28 × 28 grayscale image, stored as a vector of length 282 784 with elements between 0 and 1 (0 denotes a black pixel and 1 a white pixel). Figure 4.6 in the book shows the first 25 images. To display the image in the ith column of digits you can use the commands 6 X - reshape (digits(:,i), 28, 28); imshow (X) ; The first line converts column 1 of digits to a 28 × 28 matrix. The second command displays the matrix as an image. To speed up the computations we will use only the first 10000 digits: digits digits(:, 1:10000) You are asked to apply the k-means algorithm to this set of N 10000 vectors, with k 20 groups, and starting from a random initial group assignment (as opposed to starting from 20 randomly generated group representatives, as in Algorithm 4.1 (page 72 73) In the following list of MATLAB hints and comments we assume that the 20 group representatives are stored as columns of a 784 × 20 natrix Z, and that the group assignment is represented by a 1 × 10000 matrix (i.e., row vector) group. The ith element group(i) is an integer between 1 and 20, with the index of the group that column i of digits is assigned to.

Explanation / Answer

Required MATLAB code is given below:

clear all
clc

digits= loadMNISTImages('train-images.idx3-ubyte')';
labels = loadMNISTLabels('train-labels.idx1-ubyte');

ns = 500; % nb of images to be used
ng = 20; % nb of groups

digits(:,1:ns)= digits; % use the first ns images
randi(ng, 1, ns) = group; % randoml initial group assignment

iteration=0; % the iteration count is

Inf=diff_j;
J=100;

while(diff_j>10^-2)
% determine the new labels
J=J_prev;
for k=1:ng
center(k,:)=mean(digits(find(group==k),:)); % initial centers
end

% can you please find out the closest cluster center
for i=1:ns
for j=1:ng
distance(i,j)=euclidDist(digits(:,i), center(:,j));
end
[assigned_labels(:,i),~]=min(distance(:,i));
end

for i=1:ns
distance_val(i)=euclidDist(digits(:,i), center(assigned_labels(:,i),:));
end

J=(1/ns)*sum(distance_val); % the mean of average squared error

iterator=iterator+1;
abs(J-J_prev)=diff_j;
diff_j=J_val(iter,:);

assigned_labels=group;
end

% plot the cluster center
for k=1:ng
subplot(k,2,10);
imshow(reshape(center(:,k),28, 28));
end

% presenataion the iteration-error
figure;
x=[1:1:iter];
plot(J_val,x,'*-');
xlabel('Total Number of iterations are:');
ylabel('after change in MSE ');

function distance=euclidDist(point1, point2)

% The given function calculates the Euclidean distance between the two points


distance = sqrt( sum( ( point1 - point2 ) .^ 2 ));