dualkmeans.m
来自「《模式分析的核方法》一书中的源代码」· M 代码 · 共 44 行
M
44 行
function [f,d] = dualkmeans(K,N)
%function [f,d] = dualkmeans(K,N)
%
% Performs dual K-means for ell samples specified by the kernel K
%
%INPUTS
% K = the kernel matrix
% N = the number of clusters desired
%
%OUTPUTS
% f = the cluster allocation vector
% d = the distances of the samples to their respective cluster centroids
%
%
%For more info, see www.kernel-methods.net
% original kernel matrix stored in variable K
% clustering given by a ell x N binary matrix A
% and cluster allocation function f
% d gives the distances to cluster centroids
ell=size(K,1);
A = zeros(ell,N);
f = ceil(rand(ell,1)* N);
for i=1:ell
A(i,f(i)) = 1;
end
change = 1;
while change == 1
change = 0;
E = A * diag(1./sum(A));
Z = ones(ell,1)* diag(E'*K*E)'- 2*K*E;
[d, ff] = min(Z, [], 2);
for i=1:ell
if f(i) ~= ff(i)
A(i,ff(i)) = 1;
A(i, f(i)) = 0;
change = 1;
end
end
f = ff;
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?