MOA 12.03
Real Time Analytics for Data Streams
MembershipMatrix.java
Go to the documentation of this file.
00001 /*
00002  *    Cluster.java
00003  *    Copyright (C) 2010 RWTH Aachen University, Germany
00004  *    @author Jansen (moa@cs.rwth-aachen.de)
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.evaluation;
00022 
00023 import java.util.ArrayList;
00024 import java.util.HashMap;
00025 import moa.cluster.Clustering;
00026 import moa.gui.visualization.DataPoint;
00027 
00028 
00029 public class MembershipMatrix {
00030 
00031     HashMap<Integer, Integer>  classmap;
00032     int cluster_class_weights[][];
00033     int cluster_sums[];
00034     int class_sums[];
00035     int total_entries;
00036     int class_distribution[];
00037     int total_class_entries;
00038     int initalBuildTimestamp = -1;
00039 
00040     public MembershipMatrix(Clustering foundClustering, ArrayList<DataPoint> points) {
00041         classmap = Clustering.classValues(points);
00042 //        int lastID  = classmap.size()-1;
00043 //        classmap.put(-1, lastID);
00044         int numClasses = classmap.size();
00045         int numCluster = foundClustering.size()+1;
00046 
00047         cluster_class_weights = new int[numCluster][numClasses];
00048         class_distribution = new int[numClasses];
00049         cluster_sums = new int[numCluster];
00050         class_sums = new int[numClasses];
00051         total_entries = 0;
00052         total_class_entries = points.size();
00053         for (int p = 0; p < points.size(); p++) {
00054             int worklabel = classmap.get((int)points.get(p).classValue());
00055             //real class distribution
00056             class_distribution[worklabel]++;
00057             boolean covered = false;
00058             for (int c = 0; c < numCluster-1; c++) {
00059                 double prob = foundClustering.get(c).getInclusionProbability(points.get(p));
00060                 if(prob >= 1){
00061                     cluster_class_weights[c][worklabel]++;
00062                     class_sums[worklabel]++;
00063                     cluster_sums[c]++;
00064                     total_entries++;
00065                     covered = true;
00066                 }
00067             }
00068             if(!covered){
00069                 cluster_class_weights[numCluster-1][worklabel]++;
00070                 class_sums[worklabel]++;
00071                 cluster_sums[numCluster-1]++;
00072                 total_entries++;
00073             }
00074 
00075         }
00076         
00077         initalBuildTimestamp = points.get(0).getTimestamp();
00078     }
00079 
00080     public int getClusterClassWeight(int i, int j){
00081         return cluster_class_weights[i][j];
00082     }
00083 
00084     public int getClusterSum(int i){
00085         return cluster_sums[i];
00086     }
00087 
00088     public int getClassSum(int j){
00089         return class_sums[j];
00090     }
00091 
00092     public int getClassDistribution(int j){
00093         return class_distribution[j];
00094     }
00095 
00096     public int getClusterClassWeightByLabel(int cluster, int classLabel){
00097         return cluster_class_weights[cluster][classmap.get(classLabel)];
00098     }
00099 
00100     public int getClassSumByLabel(int classLabel){
00101         return class_sums[classmap.get(classLabel)];
00102     }
00103 
00104     public int getClassDistributionByLabel(int classLabel){
00105         return class_distribution[classmap.get(classLabel)];
00106     }
00107 
00108     public int getTotalEntries(){
00109         return total_entries;
00110     }
00111 
00112     public int getNumClasses(){
00113         return classmap.size();
00114     }
00115 
00116     public boolean hasNoiseClass(){
00117         return classmap.containsKey(-1);
00118     }
00119 
00120     @Override
00121     public String toString() {
00122         StringBuffer sb = new StringBuffer();
00123         sb.append("Membership Matrix\n");
00124         for (int i = 0; i < cluster_class_weights.length; i++) {
00125             for (int j = 0; j < cluster_class_weights[i].length; j++) {
00126                 sb.append(cluster_class_weights[i][j]+"\t ");
00127             }
00128             sb.append("| "+cluster_sums[i]+"\n");
00129         }
00130         //sb.append("-----------\n");
00131         for (int i = 0; i < class_sums.length; i++) {
00132             sb.append(class_sums[i]+"\t ");
00133         }
00134         sb.append("| "+total_entries+"\n");
00135 
00136 
00137         sb.append("Real class distribution \n");
00138         for (int i = 0; i < class_distribution.length; i++) {
00139             sb.append(class_distribution[i]+"\t ");
00140         }
00141         sb.append("| "+total_class_entries+"\n");
00142 
00143         return sb.toString();
00144     }
00145 
00146 
00147     public int getInitalBuildTimestamp(){
00148         return initalBuildTimestamp;
00149     }
00150     
00151 
00152 }
 All Classes Namespaces Files Functions Variables Enumerations