声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2746|回复: 4

[经典算法] 求DBSCAN算法的matlab代码!!

[复制链接]
发表于 2008-4-26 22:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
各位谁有DB SCAN算法的matlab代码啊?急用啊。:@Q
回复
分享到:

使用道具 举报

发表于 2008-4-30 10:43 | 显示全部楼层
数据挖掘中聚类算法之DBSCAN算法的matlab实现

  1. function [class]=dbscan(X,k,eps)
  2. [m,n]=size(X);
  3. class=zeros(1,m);
  4. clusterId = 0;
  5. X=[class',X];

  6. for i = 1:m
  7.   clusterId = clusterId + 1;
  8.   if X(i,1) == 0
  9.      [X]=expandcluster(i,X,clusterId,eps,k,n);
  10.   end  
  11. end

  12. class = X(:,1)';

  13. function [X]=expandcluster(index,X,clusterId,eps,k,n)
  14.   Di=dist(X(index,(2:n)),X(:,(2:n)));
  15.   D = find(Di<eps);
  16.   if length(D) < k
  17.     X(index,1) = -1; %noise
  18.     return;
  19.   else
  20.     X(index,1) = clusterId;
  21.     D(1)=[];  
  22.     for i=1:length(D)
  23.       Dmatrix(i,:)=X(D(i),:);
  24.     end
  25.     while length(D)~=0
  26.       Di=dist(X(D(1),(2:n)),X(:,(2:n)));
  27.       Result = find(Di<eps);
  28.       if length(Result) > k
  29.         for i = 1: length(Result)   
  30.             if X(Result(i),1) == 0|-1
  31.                if X(Result(i),1)==0
  32.                   D(length(D)+1)=Result(i);
  33.                end
  34.                X(Result(i),1)=clusterId;
  35.             end;
  36.          end;
  37.        end;
  38.         D(1)=[];
  39.      end;
  40.    end;

  41.   
  42. %function distan = dist(x,y)
  43. % sum = 0;
  44.   %nx = size(x);
  45.   %for i = 1:nx
  46.    %   sum = (x(i)-y(i))^2+sum;
  47.    %end
  48. %distan = sqrt(sum)

  49. %............................................
  50. function [Di]=dist(i,x)

  51. % function: [D]=dist(i,x)
  52. %
  53. % Aim:
  54. % Calculates the Euclidean distances between i and all objects in x     
  55. %                                    
  56. % Input:
  57. % i - an object (1,n)
  58. % x - data matrix (m,n); m-objects, n-variables        
  59. %                                                                 
  60. % Output:
  61. % D - Euclidean distance (m,1)


  62. [m,n]=size(x);
  63. Di=sqrt(sum((((ones(m,1)*i)-x).^2)'));

  64. if n==1
  65.    Di=abs((ones(m,1)*i-x))';
  66. end
复制代码
发表于 2008-5-8 01:49 | 显示全部楼层
这个好像是算一维的吧:@L
发表于 2008-5-8 01:54 | 显示全部楼层
再试了一次.原来多了一行注释
Error in ==> dbscan_2>dist at 61
objects in x
发表于 2008-5-8 13:56 | 显示全部楼层
请问2楼噪声点是如何处理的:@(
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-18 17:15 , Processed in 0.057437 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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