Hello Mat

 找回密码
 立即注册
查看: 6607|回复: 2

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

[复制链接]

7

主题

15

帖子

2

金钱

新手上路

Rank: 1

积分
17
发表于 2017-6-9 15:06:02 | 显示全部楼层 |阅读模式
dbscan代码如下:
function [class,type]=dbscan(x,k,Eps)
% Function: [class,type]=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=[randn(30,2)*.4;randn(40,2)*.5+ones(40,1)*[4 4]];
% [idx,type]=dbscan(x,5,[])
%         
% 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

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


[m,n]=size(x);

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

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,:);
       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=[ind i1(i)];   
                          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 [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)
%
% Output:
% Eps - estimated neighborhood radius

[m,n]=size(x);

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 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);
D=sqrt(sum((((ones(m,1)*i)-x).^2)'));

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


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

7

主题

15

帖子

2

金钱

新手上路

Rank: 1

积分
17
 楼主| 发表于 2017-6-9 21:32:25 | 显示全部楼层
加一个class变量就可以了,但为啥Global不行,还是我global位置加错了??
回复 支持 反对

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-6-9 22:47:12 | 显示全部楼层
QQQQ 发表于 2017-6-9 21:32
加一个class变量就可以了,但为啥Global不行,还是我global位置加错了??

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

你的那个函数【class, type】 = dbscan(x,k,Eps),class应该在函数里面初始化一下。
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-26 19:42 , Processed in 0.228226 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表