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));
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 ));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.