K均值聚类

clear; clc;
n = 300; 
c = 3; 
rng('default');
t = randperm(n);
x = [randn(1, n/3) - 2, randn(1, n/3), randn(1, n/3) + 2;
randn(1, n/3), randn(1, n/3) + 4, randn(1, n/3)]';
m = x(t(1 : c), :); 
x2 = sum(x .^ 2, 2); 
s0(1 : c, 1) = inf;
for o = 1:1000
  m2 = sum(m .^ 2, 2);
  [d, y] = min(repmat(m2, 1, n) + repmat(x2', c, 1) - 2*m*x');
  for t = 1 : c
    m(t, :) = mean(x(y == t, :)); 
    s(t, 1) = mean(d(y == t));
  end
  if norm(s - s0) < 1e-3
      break;
  end
  s0 = s;
end
hold on
plot(x( y == 1, 1), x(y == 1, 2), 'bo', 'LineWidth', 2);
plot(x(y == 2, 1), x(y == 2, 2 ), 'rx', 'LineWidth', 2);
plot(x(y == 3, 1), x(y == 3, 2 ), 'gv', 'LineWidth', 2);

猜你喜欢

转载自blog.csdn.net/u012366767/article/details/81564535