forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
23 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
function [label, energy, model] = kmeans(X, init) | ||
% Perform k-means clustering. | ||
function [label, m, energy] = kmeans(X, init) | ||
% Perform kmeans clustering. | ||
% Input: | ||
% X: d x n data matrix | ||
% init: k number of clusters or label (1 x n vector) | ||
|
@@ -9,20 +9,18 @@ | |
% model: trained model structure | ||
% Written by Mo Chen ([email protected]). | ||
n = size(X,2); | ||
idx = 1:n; | ||
last = zeros(1,n); | ||
if numel(init)==1 | ||
k = init; | ||
label = ceil(k*rand(1,n)); | ||
elseif numel(init)==n | ||
label = init; | ||
end | ||
last = 0; | ||
while any(label ~= last) | ||
[u,~,label(:)] = unique(label); % remove empty clusters | ||
k = numel(u); | ||
E = sparse(1:n,label,1,n,k,n); % transform label into indicator matrix | ||
m = X*(E*spdiags(1./sum(E,1)',0,k,k)); % compute centers | ||
last = label; | ||
[val,label] = max(bsxfun(@minus,m'*X,dot(m,m,1)'/2),[],1); % assign labels | ||
[~,~,last(:)] = unique(label); % remove empty clusters | ||
E = sparse(idx,last,1); % transform label into indicator matrix | ||
m = X*(E./sum(E,1)); % compute centers | ||
[val,label] = min(dot(m,m,1)'/2-m'*X,[],1); % assign labels | ||
end | ||
energy = dot(X(:),X(:))-2*sum(val); | ||
model.means = m; | ||
energy = dot(X(:),X(:),1)+2*sum(val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
function [label, energy] = kmeansPred(model, Xt) | ||
function [label, energy] = kmeansPred(m, X) | ||
% Prediction for kmeans clusterng | ||
% Input: | ||
% model: trained model structure | ||
% Xt: d x n testing data | ||
% model: dx k cluster center matrix | ||
% X: d x n testing data | ||
% Output: | ||
% label: 1 x n cluster label | ||
% energy: optimization target value | ||
% Written by Mo Chen ([email protected]). | ||
[val,label] = min(sqdist(model.means, Xt)); | ||
[val,label] = min(dot(X,X,1)+dot(m,m,1)'-2*m'*X,[],1); % assign labels | ||
energy = sum(val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters