数据挖掘中聚类算法之DBSCAN算法的matlab实现
- function [class]=dbscan(X,k,eps)
- [m,n]=size(X);
- class=zeros(1,m);
- clusterId = 0;
- X=[class',X];
- for i = 1:m
- clusterId = clusterId + 1;
- if X(i,1) == 0
- [X]=expandcluster(i,X,clusterId,eps,k,n);
- end
- end
- class = X(:,1)';
- function [X]=expandcluster(index,X,clusterId,eps,k,n)
- Di=dist(X(index,(2:n)),X(:,(2:n)));
- D = find(Di<eps);
- if length(D) < k
- X(index,1) = -1; %noise
- return;
- else
- X(index,1) = clusterId;
- D(1)=[];
- for i=1:length(D)
- Dmatrix(i,:)=X(D(i),:);
- end
- while length(D)~=0
- Di=dist(X(D(1),(2:n)),X(:,(2:n)));
- Result = find(Di<eps);
- if length(Result) > k
- for i = 1: length(Result)
- if X(Result(i),1) == 0|-1
- if X(Result(i),1)==0
- D(length(D)+1)=Result(i);
- end
- X(Result(i),1)=clusterId;
- end;
- end;
- end;
- D(1)=[];
- end;
- end;
-
- %function distan = dist(x,y)
- % sum = 0;
- %nx = size(x);
- %for i = 1:nx
- % sum = (x(i)-y(i))^2+sum;
- %end
- %distan = sqrt(sum)
-
- %............................................
- function [Di]=dist(i,x)
- % function: [D]=dist(i,x)
- %
- % Aim:
- % Calculates the Euclidean distances between i 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);
- Di=sqrt(sum((((ones(m,1)*i)-x).^2)'));
- if n==1
- Di=abs((ones(m,1)*i-x))';
- end
复制代码 |