Question: question: (25 points) K-means step by step, k=2 A. Initialize the cluster centers (the means) B. Calculate the distance between each data point and each

question:

(25 points) K-means step by step, k=2

A. Initialize the cluster centers (the means)

B. Calculate the distance between each data point and each center (you should have an array with a row for every data point and a distance value for each cluster mean).

C. Assign each data point with the label of its nearest cluster

 (cluster_ind) 

D. Plot the clustered data points using different colors for each cluster

E. Update the means

F. Continue for 4 iterations, creating one plot for each iteration

G. Repeat for a new initialization of cluster centers

i have provided the code below just gotta fill in the missing code, also if you want to download "cluster_shell" file here's the link:

https://files.fm/u/gdey24ea#_

code:

% kmeandata

clear all;

close all;

load('cluster_data.mat');

%%

% Plot data without any assigned clusters

figure();

scatter(kmeandata(:,1),kmeandata(:,2),20,'k','filled');

% k = 2

% Initialize centers

centers = [-2,-2;4,4];

%%

% Create figure for plotting

figure();

for i = 1:4

subplot(2,2,i);

% Calculate the distance from each point to each cluster center

for j = 1:size(centers,2); %# of centers (means)

dist(:,j) = sqrt((kmeandata(:,1)-centers(j,1))^2-(kmeandata(:,2)-centers(j,2))^2) ; %calculate distance between that center and k means dat

%

end

% Identify the nearest cluster for every point

[v, cluster_ind] = min( ) %missing

hold on % This makes sure your graph doesn't clear when plotting different clusters

% Plot all points in cluster 1 (color 1)

scatter( , ,20,'r','filled'); %missing

% Plot all points in cluster 2 (color 2)

scatter( , ,20,'g','filled'); %missing

% Plot the mean of cluster 1 with X marker

scatter(centers(1,1),centers(1,2),80,'Marker','x','MarkerEdgeColor',[0.6 0 0],'LineWidth',4);

% Plot the mean of cluster 2 with X marker

scatter(centers(2,1),centers(2,2),80,'Marker','x','MarkerEdgeColor',[0 0.6 0],'LineWidth',4);

% Update the cluster centers (nothing to plot, but they're ready for next iteration)

centers(1,:) = mean( ); %missing

centers(2,:) = mean( ); %missing

hold off;

% Label the plot with its iteration number

title(sprintf('Iteration %d', i));

end;

%%

% k = 2, different initialization

centers = [0,-1;-1,4];

figure();

for i = 1:4

subplot(2,2,i);

hold on;

hold off;

title(sprintf('Iteration %d', i));

end;

%%

% k = 3;

figure();

%

centers = [2,-2;-2,-2;2,2];

%

for i = 1:4

subplot(2,2,i);

hold on;

%

for j = 1:size(centers,1)

dist(:,j) = ;

end

%

[v cluster_ind] = ;

%

scatter( ,20,'r','filled'); %missing

scatter( ,20,'g','filled'); %missing

scatter( ,20,'b','filled'); %missing

%

scatter( ,80,'Marker','x','MarkerEdgeColor',[0.6 0 0],'LineWidth',4); %missing

scatter( ,80,'Marker','x','MarkerEdgeColor',[0 0.6 0],'LineWidth',4); %missing

scatter( ,80,'Marker','x','MarkerEdgeColor',[0 0 0.6],'LineWidth',4); %missing

%

centers(1,:) = mean( ); %missing

centers(2,:) = mean( ); %missing

centers(3,:) = mean( ); %missing

hold off;

title(sprintf('Iteration %d', i));

end;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!