MOA 12.03
Real Time Analytics for Data Streams
CFCluster.java
Go to the documentation of this file.
00001 /*
00002  *    CFCluster.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen ([email protected])
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 
00021 package moa.cluster;
00022 import java.util.Arrays;
00023 import weka.core.Instance;
00024 
00025 public abstract class CFCluster extends SphereCluster {
00026 
00027         private static final long serialVersionUID = 1L;
00028 
00029         protected double radiusFactor = 1.8;
00030 
00034         protected double N;
00038         public double[] LS;
00042         public double[] SS;
00043 
00049         public CFCluster(Instance instance, int dimensions) {
00050                 this(instance.toDoubleArray(), dimensions);
00051         }
00052 
00053         protected CFCluster(int dimensions) {
00054                 this.N = 0;
00055                 this.LS = new double[dimensions];
00056                 this.SS = new double[dimensions];
00057                 Arrays.fill(this.LS, 0.0);
00058                 Arrays.fill(this.SS, 0.0);
00059         }
00060 
00061         public CFCluster(double [] center, int dimensions) {
00062                 this.N = 1;
00063                 this.LS = center;
00064                 this.SS = new double[dimensions];
00065                 for (int i = 0; i < SS.length; i++) {
00066                         SS[i]=Math.pow(center[i], 2);
00067                 }
00068         }
00069 
00070         public CFCluster(CFCluster cluster) {
00071                 this.N = cluster.N;
00072                 this.LS = Arrays.copyOf(cluster.LS, cluster.LS.length);
00073                 this.SS = Arrays.copyOf(cluster.SS, cluster.SS.length);
00074         }
00075 
00076         public void add(CFCluster cluster ) {
00077                 this.N += cluster.N;
00078                 addVectors( this.LS, cluster.LS );
00079                 addVectors( this.SS, cluster.SS );
00080         }
00081 
00082         public abstract CFCluster getCF();
00083 
00087          @Override
00088          public double[] getCenter() {
00089                  assert (this.N>0);
00090                  double res[] = new double[this.LS.length];
00091                  for ( int i = 0; i < res.length; i++ ) {
00092                          res[i] = this.LS[i] / N;
00093                  }
00094                  return res;
00095          }
00096 
00097 
00098          @Override
00099          public abstract double getInclusionProbability(Instance instance);
00100 
00105          @Override
00106          public abstract double getRadius();
00107 
00113          @Override
00114          public double getWeight() {
00115                  return N;
00116          }
00117 
00118          public void setN(double N){
00119                  this.N = N;
00120          }
00121 
00122          public double getN() {
00123                  return N;
00124          }
00125 
00132          public static void addVectors(double[] a1, double[] a2) {
00133                  assert (a1 != null);
00134                  assert (a2 != null);
00135                  assert (a1.length == a2.length) : "Adding two arrays of different "
00136                          + "length";
00137 
00138                  for (int i = 0; i < a1.length; i++) {
00139                          a1[i] += a2[i];
00140                  }
00141          }
00142 }
 All Classes Namespaces Files Functions Variables Enumerations