-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclustering_DBSCAN_2dim.m
205 lines (174 loc) · 5.93 KB
/
clustering_DBSCAN_2dim.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
% -------------------------------------------------------------------------
% Function: [class,type]=dbscan(x,k,Eps)
% -------------------------------------------------------------------------
% Aim:
% Clustering the data with Density-Based Scan Algorithm with Noise (DBSCAN)
% -------------------------------------------------------------------------
% Input:
% x - data set (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object
% (minimal number of objects considered as a cluster)
% Eps - neighborhood radius, if not known avoid this parameter or put []
% -------------------------------------------------------------------------
% Output:
% class - vector specifying assignment of the i-th object to certain
% cluster (m,1)
% type - vector specifying type of the i-th object
% (core: 1, border: 0, outlier: -1)
% -------------------------------------------------------------------------
% Example of use:
% x=[randn(30,2)*.4;randn(40,2)*.5+ones(40,1)*[4 4]];
% [class,type]=dbscan(x,5,[])
% clusteringfigs('Dbscan',x,[1 2],class,type)
% -------------------------------------------------------------------------
% References:
% [1] M. Ester, H. Kriegel, J. Sander, X. Xu, A density-based algorithm for
% discovering clusters in large spatial databases with noise, proc.
% 2nd Int. Conf. on Knowledge Discovery and Data Mining, Portland, OR, 1996,
% p. 226, available from:
% www.dbs.informatik.uni-muenchen.de/cgi-bin/papers?query=--CO
% [2] M. Daszykowski, B. Walczak, D. L. Massart, Looking for
% Natural Patterns in Data. Part 1: Density Based Approach,
% Chemom. Intell. Lab. Syst. 56 (2001) 83-92
% -------------------------------------------------------------------------
% Written by Michal Daszykowski
% Department of Chemometrics, Institute of Chemistry,
% The University of Silesia
% December 2004
% http://www.chemometria.us.edu.pl
function clustering_DBSCAN_2dim
Points=rand(20,2);
figure(1);
hold off;
scatter(Points(:,1),Points(:,2),[],'b','c');
for i=1:length(Points);
text(Points(i,1)+.02,Points(i,2), strcat(' ',num2str(i)) ,'FontSize',10)
end
cc=hsv(100);
x=Points;
k=3;
[m,n]=size(x);
if nargin<3 | isempty(Eps)
[Eps]=epsilon(x,k);
end
class(1:size(x,1))=0;
x=[[1:m]' x];
[m,n]=size(x);
type=zeros(1,m);
no=1;
touched=zeros(m,1);
for i=1:m
if touched(i)==0;
ob=x(i,:);
%---------------
hold on;
scatter(Points(i,1),Points(i,2),[],'r');
%---------------
D=dist(ob(2:n),x(:,2:n));
ind=find(D<=Eps);
%---------------
hold on;
scatter(Points(ind',1),Points(ind',2),[],'g');
hold on;
scatter(Points(i,1),Points(i,2),[],'r');
%---------------
if length(ind)>1 & length(ind)<k+1
type(i)=0;
class(i)=0;
%---------------
hold on;
scatter(Points(ind',1),Points(ind',2),[],'b');
%---------------
end
if length(ind)==1
type(i)=-1;
class(i)=-1;
touched(i)=1;
end
if length(ind)>=k+1;
type(i)=1;
class(ind)=ones(length(ind),1)*max(no);
while ~isempty(ind)
ob=x(ind(1),:);
touched(ind(1))=1;
ind(1)=[];
D=dist(ob(2:n),x(:,2:n));
i1=find(D<=Eps);
%---------------
hold on;
scatter(Points(i1,1),Points(i1,2),[],'g');
hold on;
scatter(Points(ob(1),1),Points(ob(1),2),[],'r');
%---------------
if length(i1)>1
class(i1)=no;
if length(i1)>=k+1;
type(ob(1))=1;
%---------------
hold on;
scatter(Points(ob(1),1),Points(ob(1),2),[],'r','filled');
%---------------
else
type(ob(1))=0;
end
for i=1:length(i1)
if touched(i1(i))==0
touched(i1(i))=1;
ind=[ind i1(i)];
class(i1(i))=no;
end
end
end
end
no=no+1;
%-----------------------------------
vv=class';
vv=vv+1;
for pp=2:(max(vv));
% plot(Points((vv==pp),1),Points((vv==pp),2),'g+--');
hold on;
scatter(Points((vv==pp),1),Points((vv==pp),2),[],cc(pp*5,:),'filled');
end
%-----------------------------------
end
end
end
i1=find(class==0);
class(i1)=-1;
type(i1)=-1;
%...........................................
function [Eps]=epsilon(x,k)
% Function: [Eps]=epsilon(x,k)
%
% Aim:
% Analytical way of estimating neighborhood radius for DBSCAN
%
% Input:
% x - data matrix (m,n); m-objects, n-variables
% k - number of objects in a neighborhood of an object
% (minimal number of objects considered as a cluster)
[m,n]=size(x);
a=(prod(max(x)-min(x))).^(1/n);
b=k.^(1/n);
c=(gamma(.5*n+1)).^(1/n);
d=(m*sqrt(pi.^n)).^(1/n);
Eps=a*b*c/d;
%Eps=((prod(max(x)-min(x))*k*gamma(.5*n+1))/(m*sqrt(pi.^n))).^(1/n);
%............................................
function [D]=dist(i,x)
% function: [D]=dist(i,x)
%
% Aim:
% Calculates the Euclidean distances between the i-th object and all objects in x
%
% Input:
% i - an object (1,n)
% x - data matrix (m,n); m-objects, n-variables
%
% Output:
% D - Euclidean distance (m,1)
[m,n]=size(x);
D=sqrt(sum((((ones(m,1)*i)-x).^2)'));
if n==1
D=abs((ones(m,1)*i-x))';
end