-
Notifications
You must be signed in to change notification settings - Fork 0
/
k_means.m
73 lines (61 loc) · 1.79 KB
/
k_means.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [features, targets, label] = k_means(train_features, train_targets, Nmu, region, plot_on)
%Reduce the number of data points using the k-means algorithm
%Inputs:
% train_features - Input features
% train_targets - Input targets
% Nmu - Number of output data points
% region - Decision region vector: [-x x -y y number_of_points]
% plot_on - Plot stages of the algorithm
%
%Outputs
% features - New features
% targets - New targets
% label - The labels given for each of the original features
if (nargin < 5),
plot_on = 0;
end
[D,L] = size(train_features);
dist = zeros(Nmu,L);
label = zeros(1,L);
%Initialize the mu's
mu = randn(D,Nmu);
mu = sqrtm(cov(train_features',1))*mu + mean(train_features')'*ones(1,Nmu);
old_mu = zeros(D,Nmu);
switch Nmu,
case 0,
mu = [];
label = [];
case 1,
mu = mean(train_features')';
label = ones(1,L);
otherwise
while (sum(sum(mu == old_mu)) == 0),
old_mu = mu;
%Classify all the features to one of the mu's
for i = 1:Nmu,
dist(i,:) = sum((train_features - mu(:,i)*ones(1,L)).^2);
end
%Label the points
[m,label] = min(dist);
%Recompute the mu's
for i = 1:Nmu,
mu(:,i) = mean(train_features(:,find(label == i))')';
end
if (plot_on == 1),
plot_process(mu)
end
end
end
%Make the decision region
targets = zeros(1,Nmu);
if (Nmu > 1),
for i = 1:Nmu,
if (length(train_targets(:,find(label == i))) > 0),
targets(i) = (sum(train_targets(:,find(label == i)))/length(train_targets(:,find(label == i))) > .5);
end
end
else
%There is only one center
targets = (sum(train_targets)/length(train_targets) > .5);
end
features = mu;