Skip to content

Instantly share code, notes, and snippets.

@vtta
Created April 11, 2019 08:26
Show Gist options
  • Save vtta/4040eb0d20d3ef1bfe6f2d8850e1722f to your computer and use it in GitHub Desktop.
Save vtta/4040eb0d20d3ef1bfe6f2d8850e1722f to your computer and use it in GitHub Desktop.
GNU Octave script to test my k-means algorithm using the iris data set
% usage:
% octave-cli iris.m
fid = fopen('iris.data');
irisTextData = textscan(fid,'%f %f %f %f %s', 200, 'Delimiter',',');
fclose (fid);
irisMatrix = cell2mat(irisTextData(:,1:4));
k = 3;
clusterID = kMeans(irisMatrix, k);
result = cell2mat(irisTextData(:,5));
for i = 1:size(result, 1)
result(i, 2) = clusterID(i);
end
rightSampleCnt = 0;
clusterNameMap = cellstr(["Iris-virginica"; "Iris-versicolor"; "Iris-setosa"]);
for i = 1:size(result, 1)
printf("%.1f %.1f %.1f %.1f %s\t%d", ...
irisMatrix(i,:), cell2mat(result(i, 1)) , cell2mat(result(i, 2)) );
if strcmp(result(i, 1), clusterNameMap(cell2mat(result(i, 2))) )
rightSampleCnt = rightSampleCnt+1;
% printf(" √");
else
printf(" X");
end
printf("\n");
end
printf("Total Sample: %d\nRight Sample: %d\nAccuracy: %f\n", ...
size(result, 1), rightSampleCnt, rightSampleCnt / size(result, 1) );
% writetable(cell2table(result), 'result.csv') % only avaliable in Matlab 😒
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment