Skip to content

Instantly share code, notes, and snippets.

@xypaul
Created June 5, 2015 05:30
Show Gist options
  • Save xypaul/cb481cebb8093cf76e49 to your computer and use it in GitHub Desktop.
Save xypaul/cb481cebb8093cf76e49 to your computer and use it in GitHub Desktop.
My own implementation of kMeans Machine Learning algorithm in Matlab
function[r,l] = kmyself(dataSet, k)
% Determine the size of dataSet
[nRow, nCol] = size(dataSet);
% Empty array for cluster assesment
clusterAssment = zeros(nRow,2);
% Setup centroid and choose on to start of with
centroids = zeros(k,nCol);
for j = 1:1:nCol
minJ = min(dataSet(:,j));
rangeJ = max(dataSet(:,j) - minJ);
centroids(:,j) = minJ + rangeJ * rand(k,1);
end
% Conditional looping variable
clusterChanged = true;
while (clusterChanged)
clusterChanged = false;
for i = 1:1:nRow
minDist = Inf;
minIndex = -1;
for j = 1:1:k
distJI = dist(centroids(j,:), dataSet(i, :));
if distJI < minDist
minDist = distJI;
minIndex = j;
end
end
if clusterAssment(i,1) ~= minIndex
clusterChanged = true;
end
clusterAssment(i,:) = [minIndex,minDist^2];
end
for cent = 1:1:k
ptsInClust = [];
for item = 1:1:nRow
if (clusterAssment(item,1) == cent)
ptsInClust = [ptsInClust; dataSet(item, :)];
end
end
centroids(cent,:) = mean(ptsInClust);
end
end
r = clusterAssment;
l = centroids;
end
function d = dist(A,B)
% d = ((A(1)-B(1))^2+(A(2)-B(2))^2)^0.5
d = sqrt(sum(power(A-B, 2)));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment