-
Notifications
You must be signed in to change notification settings - Fork 0
/
r_cluster_draft.m
63 lines (47 loc) · 1.4 KB
/
r_cluster_draft.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
% Cluster_data
clear,
% add data folder to path
addpath('data')
% load psy measures
load psymeasdata.mat
% ------
% var psymeasname - name of the variables available
% var npsymeas - values of the variables
% ------
%% presets
cluster_num = 3;
%% data preprocessing
% data de-mean and data normalization
data = (npsymeas - min(npsymeas)) ./ ( max(npsymeas) - min(npsymeas) );
% find clusters in data
[c_idx,c_means] = kmeans(data,...
cluster_num,... % number of clusters
'replicates',5,... % repeats the clustering process starting from different randomly selected centroids for each replicate
'display', 'iter',...
'dist', 'sqeuclidean'); % use euclidean distance to determine best centroid for each point
% display silhouette - value corresponds to fit of each point and distance
% to other class
figure
[silh, ~] = silhouette(data,...
c_idx,...
'sqeuclidean');
% disply a 3D plot with cluster
figure
ptsymb = {'bs','r^','md','go','c+'};
for i = 1:cluster_num
clust = find(c_idx == i);
plot3(data(clust,1),data(clust,2),data(clust,3),ptsymb{i});
hold on
end
plot3(c_means(:,1),c_means(:,2),c_means(:,3),'ko');
plot3(c_means(:,1),c_means(:,2),c_means(:,3),'kx');
hold off
view(-137,10);
grid on
%%
eucD = pdist(data,'euclidean');
clustTreeEuc = linkage(eucD,'average');
[h,nodes] = dendrogram(clustTreeEuc,0);
h_gca = gca;
h_gca.TickDir = 'out';
h_gca.TickLength = [.002 0];