Skip to content

Commit

Permalink
tweak kmeans to the extreme
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Mar 12, 2017
1 parent e31258d commit 3a9055d
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions chapter09/litekmeans.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
function [label, mu] = litekmeans(X, k)
n = size(X,2);
last = zeros(1,n);
label = ceil(k*rand(1,n));
function [label, mu] = litekmeans(X, label)
idx = 1:size(X,2);
last = idx;
while any(label ~= last)
[~,~,last(:)] = unique(label); % remove empty clusters
mu = X*normalize(sparse(1:n,last,1),1); % compute cluster centers
mu = X*normalize(sparse(idx,last,1),1); % compute cluster centers
[~,label] = min(dot(mu,mu,1)'/2-mu'*X,[],1); % assign sample labels
end
% Perform kmeans clustering.
% Input:
% X: d x n data matrix
% k: number of clusters
% label: initial sample labels
% Output:
% label: 1 x n cluster label
% label: 1 x n sample label
% mu: d x k center of clusters
% Written by Mo Chen ([email protected]).

0 comments on commit 3a9055d

Please sign in to comment.