聚类算法之BIRCH(Java实现)

来源:互联网 发布:ubuntu docker daemon 编辑:程序博客网 时间:2024/05/12 03:10

聚类算法之BIRCH(Java实现)

分类: 数据挖掘 184人阅读 评论(1) 收藏 举报
算法javastringnullclassimport

BIRCH(Balanced Iterative Reducing and Clustering using Hierarchies)天生就是为处理超大规模(至少要让你的内存容不下)的数据集而设计的,它可以在任何给定的内存下运行。关于BIRCH的更多特点先不介绍,我先讲一下算法的完整实现细节,对算法的实现过程搞清楚后再去看别人对该算法的评价才会感受深刻。

你不需要具备B树的相关知识,我接下来会讲得很清楚。

BIRCH算法的过程就是要把待分类的数据插入一棵树中,并且原始数据都在叶子节点上。这棵树看起来是这个样子:



在这棵树中有3种类型的节点:Nonleaf、Leaf、MinCluster,Root可能是一种Nonleaf,也可能是一种Leaf。所有的Leaf放入一个双向链表中。每一个节点都包含一个CF值,CF是一个三元组,其中data point instance的个数,是与数据点同维度的向量,是线性和,是平方和。比如有一个MinCluster里包含3个数据点(1,2,3),(4,5,6),(7,8,9),则

N=3,

=(1+4+7,2+5+8,3+6+9)=(12,15,18),

=(1+16+49,4+25+64,9+36+81)。 

就拿这个MinCluster为例,我们可以计算它的

簇中心

簇半径

簇直径

我们还可以计算两个簇之间的距离,当然你也可以使用D0,D1,D3等等,不过在这里我们使用D2。

有意思的是簇中心、簇半径、簇直径以及两簇之间的距离D0到D3都可以由CF来计算,比如

簇直径

簇间距离,这里的N,LS和SS是指两簇合并后大簇的N,LS和SS。所谓两簇合并只需要两个对应的CF相加那可

CF1 + CF2 = (N1 + N2 , LS1 + LS2, SS1 + SS2)

每个节点的CF值就是其所有孩子节点CF值之和,以每个节点为根节点的子树都可以看成 是一个簇。

Nonleaf、Leaf、MinCluster都是有大小限制的,Nonleaf的孩子节点不能超过B个,Leaf最多只能有L个MinCluster,而一个MinCluster的直径不能超过T。

算法起初,我们扫描数据库,拿到第一个data point instance--(1,2,3),我们创建一个空的Leaf和MinCluster,把点(1,2,3)的id值放入Mincluster,更新MinCluster的CF值为(1,(1,2,3),(1,4,9)),把MinCluster作为Leaf的一个孩子,更新Leaf的CF值为(1,(1,2,3),(1,4,9))。实际上只要往树中放入一个CF(这里我们用CF作为Nonleaf、Leaf、MinCluster的统称),就要更新从Root到该叶子节点的路径上所有节点的CF值。

当又有一个数据点要插入树中时,把这个点封装为一个MinCluster(这样它就有了一个CF值),把新到的数据点记为CF_new,我们拿到树的根节点的各个孩子节点的CF值,根据D2来找到CF_new与哪个节点最近,就把CF_new加入那个子树上面去。这是一个递归的过程。递归的终止点是要把CF_new加入到一个MinCluster中,如果加入之后MinCluster的直径没有超过T,则直接加入,否则譔CF_new要单独作为一个簇,成为MinCluster的兄弟结点。插入之后注意更新该节点及其所有祖先节点的CF值。

插入新节点后,可能有些节点的孩子数大于了B(或L),此时该节点要分裂。对于Leaf,它现在有L+1个MinCluster,我们要新创建一个Leaf,使它作为原Leaf的兄弟结点,同时注意每新创建一个Leaf都要把它插入到双向链表中。L+1个MinCluster要分到这两个Leaf中,怎么分呢?找出这L+1个MinCluster中距离最远的两个Cluster(根据D2),剩下的Cluster看离哪个近就跟谁站在一起。分好后更新两个Leaf的CF值,其祖先节点的CF值没有变化,不需要更新。这可能导致祖先节点的递归分裂,因为Leaf分裂后恰好其父节点的孩子数超过了B。Nonleaf的分裂方法与Leaf的相似,只不过产生新的Nonleaf后不需要把它放入一个双向链表中。如果是树的根节点要分裂,则树的高度加1。

[java] view plaincopy
  1. //CF.java  
  2.   
  3. package birch;  
  4.     
  5. public class CF {  
  6.     
  7.     private int N;  
  8.     private double[] LS;  
  9.     private double[] SS;  
  10.     
  11.     public CF() {  
  12.         LS=new double[BIRCH.dimen];  
  13.         SS=new double[BIRCH.dimen];  
  14.     }  
  15.     
  16.     // 根据一个data point instance创建一个Clustering Feature  
  17.     public CF(double[] data) {  
  18.         int len = data.length;  
  19.         this.N = 1;  
  20.         this.LS = data;  
  21.         this.SS=new double[len];  
  22.         for (int i = 0; i < len; i++)  
  23.             this.SS[i] = Math.pow(data[i], 2);  
  24.     }  
  25.         
  26.     //复制构造函数(深复制)  
  27.     public CF(CF cf){  
  28.         this.N=cf.getN();  
  29.         int len=cf.getLS().length;  
  30.         this.LS=new double[len];  
  31.         this.SS=new double[len];  
  32.         for(int i=0;i<len;i++){  
  33.             this.LS[i]=cf.getLS()[i];  
  34.             this.SS[i]=cf.getSS()[i];  
  35.         }  
  36.     }  
  37.     
  38.     // 采用D2计算两个CF Entry之间的距离  
  39.     public double getDistanceTo(CF entry) {  
  40.         double dis = 0.0;  
  41.         int len = this.LS.length;  
  42.         // 采用D2  
  43.         for (int i = 0; i < len; i++) {  
  44.             dis += this.SS[i] / this.N + entry.getSS()[i] / entry.getN() - 2  
  45.                     * this.LS[i] * entry.getLS()[i] / (this.N * entry.getN());  
  46.         }  
  47.         return Math.sqrt(dis);  
  48.     }  
  49.         
  50.     //采用D0计算两个簇心之间的欧氏距离  
  51. //  public double getDistanceTo(CF entry) {  
  52. //      int len=entry.getLS().length;  
  53. //      double[] a=new double[len];  
  54. //      double[] b=new double[len];  
  55. //      for(int i=0;i<len;i++){  
  56. //          a[i]=this.getLS()[i]/this.N;  
  57. //          b[i]=this.getSS()[i]/this.N;  
  58. //      }  
  59. //      return calEuraDist(a,b,len);  
  60. //  }  
  61.     
  62.     // 加上或减去一个CF的值  
  63.     public void addCF(CF entry, boolean add) {  
  64.         int opt = 1// 默认为相加  
  65.         if (!add) // 如果add为false则为相减  
  66.             opt = -1;  
  67.         this.N = this.N + entry.getN() * opt;  
  68.         int len = this.LS.length;  
  69.         for (int i = 0; i < len; i++) {  
  70.             this.LS[i] = this.LS[i] + entry.getLS()[i] * opt;  
  71.             this.SS[i] = this.SS[i] + entry.getSS()[i] * opt;  
  72.         }  
  73.     }  
  74.     
  75.     //计算两个向量的欧氏距离  
  76.     public static double calEuraDist(double[] arr1,double[] arr2,int len){  
  77.         double result=0.0;  
  78.         for(int i=0;i<len;i++){  
  79.             result+=Math.pow(arr1[i]-arr2[i],2.0);  
  80.         }  
  81.         return Math.sqrt(result);  
  82.     }  
  83.     public int getN() {  
  84.         return N;  
  85.     }  
  86.     
  87.     public void setN(int n) {  
  88.         N = n;  
  89.     }  
  90.     
  91.     public double[] getLS() {  
  92.         return LS;  
  93.     }  
  94.     
  95.     public void setLS(double[] lS) {  
  96.         LS = lS;  
  97.     }  
  98.     
  99.     public double[] getSS() {  
  100.         return SS;  
  101.     }  
  102.     
  103.     public void setSS(double[] sS) {  
  104.         SS = sS;  
  105.     }  
  106.     
  107. }  

[java] view plaincopy
  1. //MinCluster.java  
  2.   
  3. package birch;  
  4.     
  5. import java.util.ArrayList;  
  6.     
  7. //最小簇  
  8. public class MinCluster {  
  9.     
  10.     private CF cf;  
  11.     private ArrayList<String> inst_marks;  
  12.         
  13.     public MinCluster(){  
  14.         cf=new CF();  
  15.         inst_marks=new ArrayList<String>();  
  16.     }  
  17.     
  18.     public CF getCf() {  
  19.         return cf;  
  20.     }  
  21.     
  22.     public void setCf(CF cf) {  
  23.         this.cf = cf;  
  24.     }  
  25.     
  26.     public ArrayList<String> getInst_marks() {  
  27.         return inst_marks;  
  28.     }  
  29.     
  30.     public void setInst_marks(ArrayList<String> inst_marks) {  
  31.         this.inst_marks = inst_marks;  
  32.     }  
  33.         
  34.     //计算簇的直径  
  35.     public static double getDiameter(CF cf){  
  36.         double diameter=0.0;  
  37.         int n=cf.getN();  
  38.         for(int i=0;i<cf.getLS().length;i++){  
  39.             double ls=cf.getLS()[i];  
  40.             double ss=cf.getSS()[i];  
  41.             diameter=diameter+(2*n*ss-2*ls*ls);  
  42.         }  
  43.         diameter=diameter/(n*n-n);  
  44.         return Math.sqrt(diameter);  
  45.     }  
  46.         
  47.     //计算和另外一个簇合并后的直径  
  48.     public static double getDiameter(MinCluster cluster1,MinCluster cluster2){  
  49.         CF cf=new CF(cluster1.getCf());  
  50.         cf.addCF(cluster2.getCf(), true);  
  51.         return getDiameter(cf);  
  52.     }  
  53.         
  54.     public void mergeCluster(MinCluster cluster){  
  55.         this.getCf().addCF(cluster.getCf(), true);  
  56.         for(int i=0;i<cluster.getInst_marks().size();i++){  
  57.             this.getInst_marks().add(cluster.getInst_marks().get(i));  
  58.         }  
  59.     }  
  60. }  

[java] view plaincopy
  1. //TreeNode.java  
  2.   
  3. package birch;  
  4.     
  5. public abstract class TreeNode extends CF {  
  6.     
  7.     private TreeNode parent;  
  8.     
  9.     public TreeNode() {  
  10.             
  11.     }  
  12.         
  13.     public TreeNode(double[] data) {  
  14.         super(data);  
  15.     }  
  16.     
  17.     public TreeNode getParent() {  
  18.         return parent;  
  19.     }  
  20.     
  21.     public void setParent(TreeNode parent) {  
  22.         this.parent = parent;  
  23.     }  
  24.         
  25.     public void addCFUpToRoot(CF cf){  
  26.         TreeNode node=this;  
  27.         while(node!=null){  
  28.             node.addCF(cf, true);  
  29.             node=node.getParent();  
  30.         }  
  31.     }  
  32.         
  33.     abstract void split();  
  34.         
  35.     abstract void absorbSubCluster(MinCluster cluster);  
  36. }  

[java] view plaincopy
  1. //NonleafNode.java  
  2.   
  3. package birch;  
  4.     
  5. import java.util.ArrayList;  
  6.     
  7. public class NonleafNode extends TreeNode {  
  8.     
  9.     private int B=5;  
  10.     private ArrayList<TreeNode> children;  
  11.     
  12.     public NonleafNode() {  
  13.         children=new ArrayList<TreeNode>();  
  14.     }  
  15.     
  16.     public NonleafNode(double[] data) {  
  17.         super(data);  
  18.     }  
  19.     
  20.     // 节点分裂  
  21.     public void split() {  
  22.         // 找到距离最远的两个孩子节点  
  23.         int c1 = 0;  
  24.         int c2 = 0;  
  25.         double maxDist = 0;  
  26.         int len = this.getChildren().size();  
  27.         for (int i = 0; i < len - 1; i++) {  
  28.             for (int j = i + 1; j < len; j++) {  
  29.                 double dist = this.getChildren().get(i)  
  30.                         .getDistanceTo(this.getChildren().get(j));  
  31.                 if (dist > maxDist) {  
  32.                     maxDist = dist;  
  33.                     c1 = i;  
  34.                     c2 = j;  
  35.                 }  
  36.             }  
  37.         }  
  38.         // 以距离最远的孩子节点为中心,把B+1个孩子分为两个大簇。其中一个簇仍留作本节点的孩子,另外一簇需要新创建一个节点来领养它们  
  39.         NonleafNode newNode = new NonleafNode();  
  40.         newNode.addChild(this.getChildren().get(c2));  
  41.         //如果本节点已经是Root节点,则需要创建一个新的Root节点  
  42.         if(this.getParent()==null){  
  43.             NonleafNode root= new NonleafNode();  
  44.             root.setN(this.getN());  
  45.             root.setLS(this.getLS());  
  46.             root.setSS(this.getSS());  
  47.             root.addChild(this);  
  48.             this.setParent(root);  
  49.         }  
  50.         newNode.setParent(this.getParent());  
  51.         ((NonleafNode)this.getParent()).addChild(newNode);  
  52.         for (int i = 0; i < len; i++) {  
  53.             if (i != c1 && i != c2) {  
  54.                 if (this.getChildren().get(i)  
  55.                         .getDistanceTo(this.getChildren().get(c2)) < this  
  56.                         .getChildren().get(i)  
  57.                         .getDistanceTo(this.getChildren().get(c1))) {  
  58.                     newNode.addChild(this.getChildren().get(i));  
  59.                 }  
  60.             }  
  61.         }  
  62.         for (TreeNode entry : newNode.getChildren()) {  
  63.             newNode.addCF(entry, true);  
  64.             this.deleteChild(entry);  
  65.             this.addCF(entry, false);  
  66.         }  
  67.         //如果本节点分裂导致父节点的孩子数超过了分枝因子,引发父节点分裂  
  68.         NonleafNode pn=(NonleafNode)this.getParent();  
  69.         if(pn.getChildren().size()>B){  
  70.             this.getParent().split();  
  71.         }  
  72.     }  
  73.     public void absorbSubCluster(MinCluster cluster){  
  74.         //从本节点的孩子中寻找与cluster最近的子节点  
  75.         CF cf=cluster.getCf();  
  76.         int nearIndex=0;  
  77.         double minDist=Double.MAX_VALUE;  
  78.         for(int i=0;i<this.getChildren().size();i++){  
  79.             double dist=cf.getDistanceTo(this.getChildren().get(i));  
  80.             if(dist<minDist){  
  81.                 nearIndex=i;  
  82.             }  
  83.         }  
  84.         //让那个最近的子节点absorb掉这个新到的cluster  
  85.         this.getChildren().get(nearIndex).absorbSubCluster(cluster);  
  86.     }  
  87.     
  88.     public ArrayList<TreeNode> getChildren() {  
  89.         return children;  
  90.     }  
  91.     
  92.     public void setChildren(ArrayList<TreeNode> children) {  
  93.         this.children = children;  
  94.     }  
  95.     
  96.     public void addChild(TreeNode child) {  
  97.         this.children.add(child);  
  98.     }  
  99.     
  100.     public void deleteChild(TreeNode child) {  
  101.         this.children.remove(children.indexOf(child));  
  102.     }  
  103.     
  104.     public int getB() {  
  105.         return B;  
  106.     }  
  107.     
  108.     public void setB(int b) {  
  109.         B = b;  
  110.     }  
  111. }  

[java] view plaincopy
  1. //LeafNode.java  
  2.   
  3. package birch;  
  4.     
  5. import java.util.ArrayList;  
  6.     
  7. public class LeafNode extends TreeNode {  
  8.     
  9.     private int L=10;  
  10.     private double T=2.8;  
  11.     private ArrayList<MinCluster> children;  
  12.     private LeafNode pre;  
  13.     private LeafNode next;  
  14.     
  15.     public LeafNode() {  
  16.         children=new ArrayList<MinCluster>();  
  17.     }  
  18.     
  19.     public LeafNode(double[] data) {  
  20.         super(data);  
  21.     }  
  22.     
  23.     // 节点分裂  
  24.     public void split() {  
  25.         // 找到距离最远的两个孩子节点  
  26.         int c1 = 0;  
  27.         int c2 = 0;  
  28.         double maxDist = 0;  
  29.         int len = this.getChildren().size();  
  30.         for (int i = 0; i < len - 1; i++) {  
  31.             for (int j = i + 1; j < len; j++) {  
  32.                 double dist = this.getChildren().get(i).getCf()  
  33.                         .getDistanceTo(this.getChildren().get(j).getCf());  
  34.                 if (dist > maxDist) {  
  35.                     maxDist = dist;  
  36.                     c1 = i;  
  37.                     c2 = j;  
  38.                 }  
  39.             }  
  40.         }  
  41.         // 以距离最远的孩子节点为中心,把B+1个孩子分为两个大簇。其中一个簇仍留作本节点的孩子,另外一簇需要新创建一个节点来领养它们  
  42.         LeafNode newNode = new LeafNode();  
  43.         newNode.addChild(this.getChildren().get(c2));  
  44.         // 如果本节点已经是Root节点,则需要创建一个新的Root节点  
  45.         if (this.getParent() == null) {  
  46.             NonleafNode root = new NonleafNode();  
  47.             root.setN(this.getN());  
  48.             root.setLS(this.getLS());  
  49.             root.setSS(this.getSS());  
  50.             this.setParent(root);  
  51.             root.addChild(this);  
  52.         }  
  53.         //建立新节点和本节点的父节点的父子关系  
  54.         newNode.setParent(this.getParent());  
  55.         ((NonleafNode)this.getParent()).addChild(newNode);  
  56.         //把离newNode近的孩子节点归到newNode这个簇里面  
  57.         for (int i = 0; i < len; i++) {  
  58.             if (i != c1 && i != c2) {  
  59.                 if (this.getChildren().get(i).getCf()  
  60.                         .getDistanceTo(this.getChildren().get(c2).getCf()) < this  
  61.                         .getChildren().get(i).getCf()  
  62.                         .getDistanceTo(this.getChildren().get(c1).getCf())) {  
  63.                     newNode.addChild(this.getChildren().get(i));  
  64.                 }  
  65.             }  
  66.         }  
  67.         //把离newNode近的孩子节点从本节点中删除  
  68.         for (MinCluster cluster : newNode.getChildren()) {  
  69.             newNode.addCF(cluster.getCf(), true);  
  70.             this.deleteChild(cluster);  
  71.             this.addCF(cluster.getCf(), false);  
  72.         }  
  73.         // 把新增加的LeafNode添加到LeafNode双向链表中  
  74.         if (this.getNext() != null) {  
  75.             newNode.setNext(this.getNext());  
  76.             this.getNext().setPre(newNode);  
  77.         }  
  78.         this.setNext(newNode);  
  79.         newNode.setPre(this);  
  80.         // 如果本节点分裂导致父节点的孩子数超过了分枝因子,引发父节点分裂  
  81.         NonleafNode pn = (NonleafNode) this.getParent();  
  82.         if (pn.getChildren().size() > pn.getB()) {  
  83.             this.getParent().split();  
  84.         }  
  85.     }  
  86.     
  87.     @Override  
  88.     public void absorbSubCluster(MinCluster cluster) {  
  89.         // 先试图找到叶子节点的孩子(一些subcluster)中与cluster最近的簇  
  90.         CF cf = cluster.getCf();  
  91.         int nearIndex = 0;  
  92.         double minDist = Double.MAX_VALUE;  
  93.         int len = this.getChildren().size();  
  94.         if (len > 0) {  
  95.             for (int i = 0; i < len; i++) {  
  96.                 double dist = cf.getDistanceTo(this.getChildren().get(i)  
  97.                         .getCf());  
  98.                 if (dist < minDist) {  
  99.                     nearIndex = i;  
  100.                 }  
  101.             }  
  102.             // 计算两个簇合并后的直径  
  103.             double mergeDiameter = MinCluster.getDiameter(cluster, this  
  104.                     .getChildren().get(nearIndex));  
  105.             // 如果合并后发现簇的直径超过了阈值,则把cluster作为一个单独的孩子插入本叶子节点下  
  106.             if (mergeDiameter > T) {  
  107.                 this.addChild(cluster);  
  108.                 if (this.getChildren().size() > L) {  
  109.                     this.split();  
  110.                 }  
  111.             }  
  112.             // 如果不超过阈值,则直接合并两个簇  
  113.             else {  
  114.                 this.getChildren().get(nearIndex).mergeCluster(cluster);  
  115.             }  
  116.         }  
  117.         // 创建B树之初,叶子节点还没有children  
  118.         else {  
  119.             this.addChild(cluster);  
  120.         }  
  121.         this.addCFUpToRoot(cluster.getCf());  
  122.     }  
  123.     
  124.     public ArrayList<MinCluster> getChildren() {  
  125.         return children;  
  126.     }  
  127.     
  128.     public void setChildren(ArrayList<MinCluster> children) {  
  129.         this.children = children;  
  130.     }  
  131.     
  132.     public void addChild(MinCluster child) {  
  133.         this.children.add(child);  
  134.     }  
  135.     
  136.     public void deleteChild(MinCluster child) {  
  137.         this.children.remove(children.indexOf(child));  
  138.     }  
  139.     
  140.     public LeafNode getPre() {  
  141.         return pre;  
  142.     }  
  143.     
  144.     public void setPre(LeafNode pre) {  
  145.         this.pre = pre;  
  146.     }  
  147.     
  148.     public LeafNode getNext() {  
  149.         return next;  
  150.     }  
  151.     
  152.     public void setNext(LeafNode next) {  
  153.         this.next = next;  
  154.     }  
  155.     
  156.     public int getL() {  
  157.         return L;  
  158.     }  
  159.     
  160.     public void setL(int l) {  
  161.         L = l;  
  162.     }  
  163.     
  164.     public double getT() {  
  165.         return T;  
  166.     }  
  167.     
  168.     public void setT(double t) {  
  169.         T = t;  
  170.     }  
  171. }  

[java] view plaincopy
  1. //BIRCH.java  
  2.   
  3. package birch;  
  4.     
  5. import java.io.BufferedReader;  
  6. import java.io.File;  
  7. import java.io.FileReader;  
  8. import java.io.IOException;  
  9.     
  10. public class BIRCH {  
  11.     
  12.     public static final int dimen=4;  
  13.     LeafNode leafNodeHead=new LeafNode();  
  14.     int point_num=0;        //point instance计数  
  15.         
  16.     //逐条扫描数据库,建立B-树  
  17.     public TreeNode buildBTree(String filename){  
  18.         //先建立一个叶子节点  
  19.         LeafNode leaf=new LeafNode();  
  20.         TreeNode root=leaf;  
  21.     
  22.         //把叶子节点加入存储叶子节点的双向链表  
  23.         leafNodeHead.setNext(leaf);  
  24.         leaf.setPre(leafNodeHead);  
  25.         //打开文件,从文件中读取原始数据  
  26.         File file = new File(filename);  
  27.         if(!file.exists()){  
  28.             System.out.println("Data File Not Exists.");  
  29.             System.exit(2);  
  30.         }  
  31.         try {  
  32.             FileReader fr = new FileReader(file);  
  33.             BufferedReader br=new BufferedReader(fr);  
  34.             String line=null;  
  35.             while((line=br.readLine())!=null && line.trim()!=""){  
  36.                 point_num++;  
  37.                 String[] cont=line.split("[,|\\s+]");  
  38.                 //读入point instance  
  39.                 double[] data=new double[dimen];  
  40.                 for(int i=0;i<data.length;i++){  
  41.                     data[i]=Double.parseDouble(cont[i]);  
  42.                 }  
  43.                 String mark=String.valueOf(point_num)+cont[data.length];  
  44.                 //根据一个point instance创建一个MinCluster  
  45.                 CF cf=new CF(data);  
  46.                 MinCluster subCluster=new MinCluster();  
  47.                 subCluster.setCf(cf);  
  48.                 subCluster.getInst_marks().add(mark);  
  49.                 //把新到的point instance插入树中  
  50.                 root.absorbSubCluster(subCluster);  
  51.                 //要始终保证root是树的根节点  
  52.                 while(root.getParent()!=null){  
  53.                     root=root.getParent();  
  54.                 }  
  55.             }  
  56.             br.close();  
  57.         } catch (IOException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.         return root;  
  61.     }  
  62.         
  63.     //打印B-树的所有叶子节点  
  64.     public void printLeaf(LeafNode header){  
  65.         //point_num清0  
  66.         point_num=0;  
  67.         while(header.getNext()!=null){  
  68.             System.out.println("\n一个叶子节点:");  
  69.             header=header.getNext();  
  70.             for(MinCluster cluster:header.getChildren()){  
  71.                 System.out.println("\n一个最小簇:");  
  72.                 for(String mark:cluster.getInst_marks()){  
  73.                     point_num++;  
  74.                     System.out.print(mark+"\t");  
  75.                 }  
  76.             }  
  77.         }  
  78.     }  
  79.         
  80.     //打印指定根节点的子树  
  81.     public void printTree(TreeNode root){  
  82.         if(!root.getClass().getName().equals("birch.LeafNode")){  
  83.             NonleafNode nonleaf=(NonleafNode)root;  
  84.             for(TreeNode child:nonleaf.getChildren()){  
  85.                 printTree(child);  
  86.             }  
  87.         }  
  88.         else{  
  89.             System.out.println("\n一个叶子节点:");  
  90.             LeafNode leaf=(LeafNode)root;  
  91.             for(MinCluster cluster:leaf.getChildren()){  
  92.                 System.out.println("\n一个最小簇:");  
  93.                 for(String mark:cluster.getInst_marks()){  
  94.                     System.out.print(mark+"\t");  
  95.                     point_num++;  
  96.                 }  
  97.             }  
  98.         }  
  99.     }  
  100.         
  101.     public static void main(String[] args) {  
  102.         BIRCH birch=new BIRCH();  
  103.         TreeNode root=birch.buildBTree("/home/orisun/test/iris.shuffled");  
  104.         birch.point_num=0;  
  105.         birch.printTree(root);  
  106.         System.out.println();  
  107.         //birch.printLeaf(birch.leafNodeHead);  
  108.         //确认被分类的point instance和扫描数据库时录入的point instance的个数是一致的  
  109.         System.out.println(birch.point_num);  
  110.     }  
  111. }  

最后我们来总结一BIRCH的优势和劣势。

优点:

  1. 节省内在。叶子节点放在磁盘分区上,非叶子节点仅仅是存储了一个CF值,外加指向父节点和孩子节点的指针。
  2. 快。合并两个两簇只需要两个CF算术相加即可;计算两个簇的距离只需要用到(N,LS,SS)这三个值足矣。
  3. 一遍扫描数据库即可建立B树。
  4. 可识别噪声点。建立好B树后把那些包含数据点少的MinCluster当作outlier。
  5. 由于B树是高度平衡的,所以在树上进行插入或查找操作很快。

缺点:

  1. 结果依赖于数据点的插入顺序。本属于同一个簇的点可能由于插入顺序相差很远而分到不同的簇中,即使同一个点在不同的时刻被插入,也会被分到不同的簇中。
  2. 对非球状的簇聚类效果不好。这取决于簇直径和簇间距离的计算方法。
  3. 对高维数据聚类效果不好。
  4. 由于每个节点只能包含一定数目的子节点,最后得出来的簇可能和自然簇相差很大。
  5. BIRCH适合于处理需要数十上百小时聚类的数据,但在整个过程中算法一旦中断,一切必须从头再来。
  6. 局部性也导致了BIRCH的聚类效果欠佳。当一个新点要插入B树时,它只跟很少一部分簇进行了相似性(通过计算簇间距离)比较,高的efficient导致低的effective。
from:http://www.kuqin.com/algorithm/20111016/312976.html
原创粉丝点击