Answer To: Matlab Project Computer Software for Sciences COSC2836 XXXXXXXXXXMarch 2021 The goal of this project...
Kshitij answered on Apr 03 2021
knns/computeCentroids.m
function centroids = computeCentroids(X, idx, K)
[m n] = size(X);
centroids = zeros(K, n);
for i=1:K
xi = X(idx==i,:);
ck = size(xi,1);
% centroids(i, :) = (1/ck) * sum(xi);
centroids(i, :) = (1/ck) * [sum(xi(:,1)) sum(xi(:,2))];
end
end
knns/getClosestCentroids.m
function indices = getClosestCentroids(X, centroids)
K = size(centroids, 1);
indices = zeros(size(X,1), 1);
m = size(X,1);
for i=1:m
k = 1;
min_dist = sum((X(i,:) - centroids(1,:)) .^ 2);
for j=2:K
dist = sum((X(i,:) - centroids(j,:)) .^ 2);
if(dist < min_dist)
min_dist = dist;
k = j;
end
end
indices(i) = k;
end
end
knns/initCentroids.m
function centroids = initCentroids(X, K)
centroids = zeros(K,size(X,2));
randidx = randperm(size(X,1));
centroids = X(randidx(1:K), :);
end
knns/reportKNN.docx
1- Implement above mentioned K-means algorithm using MATLAB(it is highly recommended that you write a separate function for each step,
1. The inputs of your code will be:
a. The number of maximum allowed iterations
b. K (number of clusters)
2. Use the Fogy method in the initialization step
3. Use Squared Euclidean distance in the assignment step to finding the nearest mean to a data point.
4....