Search code examples
matlabmachine-learningcluster-analysismathematical-optimizationk-means

Internal Operations of K-Means


Using Matlabs K-means I am unsure on the specifics of the clustering. To explain this I will use an example:

My data has been normalized and the outputs look like this:

enter image description here

Each row represents a network packet after normalization. So row 1 would represent a packet from Computer A.

Now I am wondering when I run my K-means in Matlab does it cluster each column or does it cluster via row?

i.e Will Column A belong to Cluster 1 Column B Cluster 2 etc.

The reason behind asking is I need each packet (Row) to remain bound and each packet clustered based on its intrinsic qualities. My fear however is this may seriously nerf its capabilities. But I am hoping there is an aggregation method available that can solve this riddle.

Code:

        %% generate sample data
K = 4;
numObservarations = 5000;
dimensions = 42;
%% cluster
opts = statset('MaxIter', 500, 'Display', 'iter');
[clustIDX, clusters, interClustSum, Dist] = kmeans(data, K, 'options',opts, ...
'distance','sqEuclidean', 'EmptyAction','singleton', 'replicates',3);
%% plot data+clusters
figure, hold on
scatter3(data(:,1),data(:,2),data(:,3), 5, clustIDX, 'filled')
scatter3(clusters(:,1),clusters(:,2),clusters(:,3), 100, (1:K)', 'filled')
hold off, xlabel('x'), ylabel('y'), zlabel('z')
%% plot clusters quality
figure
[silh,h] = silhouette(data, clustIDX);
avrgScore = mean(silh);
%% Assign data to clusters
% calculate distance (squared) of all instances to each cluster centroid
D = zeros(numObservarations, K);     % init distances
for k=1:K
%d = sum((x-y).^2).^0.5
D(:,k) = sum( ((data - repmat(clusters(k,:),numObservarations,1)).^2), 2);
end
% find  for all instances the cluster closet to it
[minDists, clusterIndices] = min(D, [], 2);
% compare it with what you expect it to be
sum(clusterIndices == clustIDX)

Results:

enter image description here

This is based on 5000 rows. Unfortunately being unable to rebuild the data after clustering limits my knowledge on what is happening. (See related question: MATLAB - Classification output)


Solution

  • The standard format for clustering and classification data inputs in Matlab is:

    1. a row per sample
    2. different features for a certain sample (row) in each column.