QQQQ 发表于 2017-6-9 15:06:02

DBSCAN密度聚类matlab程序遇到的一些问题

dbscan代码如下:
function =dbscan(x,k,Eps)
% Function: =dbscan(x,k,Eps)
%
% Aim:
% Clustring 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 - neighborhoog radius, if not known put []
%
% Output:
% class - vector specifying belongingness 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=];
% =dbscan(x,5,[])
%         
% References:
% 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
% 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

% Michal Daszykowski
% Department of Chemometrics
% The University of Silesia
% 9 Szkolna Street
% 40-006 Katowice, Poland


=size(x);

if nargin<3 | isempty(Eps)
   =epsilon(x,k);
end

x=[' x];
=size(x);
type=zeros(1,m);
no=1;
touched=zeros(m,1);

for i=1:m
    if touched(i)==0;
       ob=x(i,:);
       D=dist(ob(2:n),x(:,2:n));
       ind=find(D<=Eps);

       if length(ind)>1 & length(ind)<k+1      
          type(i)=0;
          class(i)=0;
       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);

                if length(i1)>1
                   class(i1)=no;
                   if length(i1)>=k+1;
                      type(ob(1))=1;
                   else
                      type(ob(1))=0;
                   end

                   for i=1:length(i1)
                     if touched(i1(i))==0
                        touched(i1(i))=1;
                        ind=;   
                        class(i1(i))=no;
                     end                  
                   end
                end
          end
          no=no+1;
       end
    end
end

i1=find(class==0);
class(i1)=-1;
type(i1)=-1;


%...........................................
function =epsilon(x,k)

% Function: =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)
%
% Output:
% Eps - estimated neighborhood radius

=size(x);

Eps=((prod(max(x)-min(x))*k*gamma(.5*n+1))/(m*sqrt(pi.^n))).^(1/n);


%............................................
function =dist(i,x)

% function: =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)


=size(x);
D=sqrt(sum((((ones(m,1)*i)-x).^2)'));

if n==1
   D=abs((ones(m,1)*i-x))';
end


会出现图中的错误,求教怎么处理??加global也效果啊

QQQQ 发表于 2017-6-9 21:32:25

加一个class变量就可以了,但为啥Global不行,还是我global位置加错了??

Halcom 发表于 2017-6-9 22:47:12

QQQQ 发表于 2017-6-9 21:32
加一个class变量就可以了,但为啥Global不行,还是我global位置加错了??

global要在主函数里面放,子函数也要放

你的那个函数【class, type】 = dbscan(x,k,Eps),class应该在函数里面初始化一下。
页: [1]
查看完整版本: DBSCAN密度聚类matlab程序遇到的一些问题