Ignore:
Timestamp:
10/31/14 15:54:08 (10 years ago)
Author:
atrautsch
Message:

Mehr kommentiert.

Location:
trunk/CrossPare/src/de/ugoe/cs/cpdp/training
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaBaseTraining2.java

    r7 r20  
    55 
    66import de.ugoe.cs.util.console.Console; 
     7 
    78import weka.core.OptionHandler; 
    89import weka.classifiers.Classifier; 
    910import weka.classifiers.meta.CVParameterSelection; 
    1011 
     12/** 
     13 * WekaBaseTraining2 
     14 *  
     15 * Allows specification of the Weka classifier and its params in the XML experiment configuration. 
     16 *  
     17 * Important conventions of the XML format:  
     18 * Cross Validation params come always last and are prepended with -CVPARAM 
     19 * Example: <trainer name="WekaClusterTraining2" param="RandomForestLocal weka.classifiers.trees.RandomForest -CVPARAM I 5 25 5"/> 
     20 */ 
    1121public abstract class WekaBaseTraining2 implements WekaCompatibleTrainer { 
    1222         
     
    2030                String[] params = parameters.split(" "); 
    2131 
    22                 // first is classifierName 
     32                // first part of the params is the classifierName (e.g. SMORBF) 
    2333                classifierName = params[0]; 
    2434                 
    25                 // all following parameters can be copied from weka! 
     35                // the following parameters can be copied from weka! 
    2636                 
    27                 // second param is classifierClassName 
     37                // second param is classifierClassName (e.g. weka.classifiers.functions.SMO) 
    2838                classifierClassName = params[1]; 
    2939         
    30                 // rest are params to the specified classifier 
     40                // rest are params to the specified classifier (e.g. -K weka.classifiers.functions.supportVector.RBFKernel) 
    3141                classifierParams = Arrays.copyOfRange(params, 2, params.length); 
    3242                 
     
    4656                        Classifier obj = (Classifier) c.newInstance(); 
    4757                         
    48                         // Filter -CVPARAM 
     58                        // Filter out -CVPARAM, these are special because they do not belong to the Weka classifier class as parameters 
    4959                        String[] param = Arrays.copyOf(classifierParams, classifierParams.length); 
    5060                        String[] cvparam = {}; 
     
    6878                         
    6979                        // we have cross val params 
    70                         // cant check on cvparam.length may not be initialized                   
     80                        // cant check on cvparam.length here, it may not be initialized                  
    7181                        if(cv) { 
    7282                                final CVParameterSelection ps = new CVParameterSelection(); 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaClusterTraining2.java

    r19 r20  
    2323 * WekaClusterTraining2 
    2424 *  
    25  * 1. Cluster traindata 
    26  * 2. for each cluster train a classifier with traindata from cluster 
    27  * 3. match testdata instance to a cluster, then classify with classifier from the cluster 
     25 * Currently supports only EM Clustering. 
    2826 *  
    29  * XML config: 
     27 * 1. Cluster training data 
     28 * 2. for each cluster train a classifier with training data from cluster 
     29 * 3. match test data instance to a cluster, then classify with classifier from the cluster 
     30 *  
     31 * XML configuration: 
    3032 * <!-- because of clustering --> 
    3133 * <preprocessor name="Normalization" param=""/> 
     
    3335 * <!-- cluster trainer --> 
    3436 * <trainer name="WekaClusterTraining2" param="NaiveBayes weka.classifiers.bayes.NaiveBayes" /> 
    35  *  
    36  * Questions: 
    37  * - how do we configure the clustering params? 
    3837 */ 
    3938public class WekaClusterTraining2 extends WekaBaseTraining2 implements ITrainingStrategy { 
     
    4544                return classifier; 
    4645        } 
    47          
    4846         
    4947        @Override 
     
    7169                 
    7270                 
    73                  
     71                /** 
     72                 * Helper method that gives us a clean instance copy with  
     73                 * the values of the instancelist of the first parameter.  
     74                 *  
     75                 * @param instancelist with attributes 
     76                 * @param instance with only values 
     77                 * @return copy of the instance 
     78                 */ 
    7479                private Instance createInstance(Instances instances, Instance instance) { 
    7580                        // attributes for feeding instance to classifier 
     
    96101                } 
    97102                 
    98                  
    99103                @Override 
    100104                public double classifyInstance(Instance instance) { 
    101105                        double ret = 0; 
    102106                        try { 
     107                                // 1. copy the instance (keep the class attribute) 
    103108                                Instances traindata = ctraindata.get(0); 
    104109                                Instance classInstance = createInstance(traindata, instance); 
    105110                                 
    106                                 // remove class attribute before clustering 
     111                                // 2. remove class attribute before clustering 
    107112                                Remove filter = new Remove(); 
    108113                                filter.setAttributeIndices("" + (traindata.classIndex() + 1)); 
     
    110115                                traindata = Filter.useFilter(traindata, filter); 
    111116                                 
     117                                // 3. copy the instance (without the class attribute) for clustering 
    112118                                Instance clusterInstance = createInstance(traindata, instance); 
    113119                                 
    114                                 // 1. classify testdata instance to a cluster number 
     120                                // 4. match instance without class attribute to a cluster number 
    115121                                int cnum = clusterer.clusterInstance(clusterInstance); 
    116122                                 
    117                                 //Console.traceln(Level.INFO, String.format("instance is in cluster: " + cnum)); 
    118                                                  
    119                                 // 2. classify testata instance to the classifier 
     123                                // 5. classify instance with class attribute to the classifier of that cluster number 
    120124                                ret = cclassifier.get(cnum).classifyInstance(classInstance); 
    121125                                 
     
    127131                } 
    128132 
    129                  
    130                  
    131133                @Override 
    132134                public void buildClassifier(Instances traindata) throws Exception { 
    133135                         
    134                         // 1. copy traindata 
     136                        // 1. copy training data 
    135137                        Instances train = new Instances(traindata); 
    136138                         
     
    141143                        train = Filter.useFilter(train, filter); 
    142144                         
    143                         // 3. cluster data 
    144                         //Console.traceln(Level.INFO, String.format("starting clustering")); 
    145                          
     145                        // new objects 
    146146                        cclassifier = new HashMap<Integer, Classifier>(); 
    147147                        ctraindata = new HashMap<Integer, Instances>(); 
    148148                         
     149                        // 3. cluster data 
    149150                        // use standard params for now 
    150151                        clusterer = new EM(); 
     152                        // we can set options like so: 
    151153                        //String[] params = {"-N", "100"}; 
    152154                        //clusterer.setOptions(params); 
     155                         
     156                        // set max num of clusters to train data size (although we do not want that) 
     157                        clusterer.setMaximumNumberOfClusters(train.size()); 
     158                                                 
     159                        // build clusterer 
    153160                        clusterer.buildClusterer(train); 
    154                         // set max num to traindata size 
    155                         clusterer.setMaximumNumberOfClusters(train.size()); 
    156                          
    157                         // 4. get cluster membership of our traindata 
    158                         //AddCluster cfilter = new AddCluster(); 
    159                         //cfilter.setClusterer(clusterer); 
    160                         //cfilter.setInputFormat(train); 
    161                         //Instances ctrain = Filter.useFilter(train, cfilter); 
    162161                         
    163162                        Instances ctrain = new Instances(train); 
    164163                         
    165                         // get traindata per cluster 
     164                        // get train data per cluster 
    166165                        int cnumber; 
    167166                        for ( int j=0; j < ctrain.numInstances(); j++ ) { 
    168                                 // get the cluster number from the attributes, subract 1 because if we clusterInstance we get 0-n, and this is 1-n 
    169                                 //cnumber = Integer.parseInt(ctrain.get(j).stringValue(ctrain.get(j).numAttributes()-1).replace("cluster", "")) - 1; 
     167                                cnumber = clusterer.clusterInstance(ctrain.get(j)); 
    170168                                 
    171                                 cnumber = clusterer.clusterInstance(ctrain.get(j)); 
    172169                                // add training data to list of instances for this cluster number 
    173170                                if ( !ctraindata.containsKey(cnumber) ) { 
     
    178175                        } 
    179176                         
    180                         // Debug output 
    181                         //Console.traceln(Level.INFO, String.format("number of clusters: " + clusterer.numberOfClusters())); 
    182                          
    183                         // train one classifier per cluster, we get the clusternumber from the traindata 
     177                        // train one classifier per cluster, we get the cluster number from the training data 
    184178                        Iterator<Integer> clusternumber = ctraindata.keySet().iterator(); 
    185179                        while ( clusternumber.hasNext() ) { 
     
    188182                                cclassifier.get(cnumber).buildClassifier(ctraindata.get(cnumber)); 
    189183                                 
    190                                 //Console.traceln(Level.INFO, String.format("building classifier in cluster "+cnumber + " with " + ctraindata.get(cnumber).size() + " traindata instances")); 
     184                                //Console.traceln(Level.INFO, String.format("classifier in cluster "+cnumber)); 
    191185                        } 
    192186                } 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaLocalTraining2.java

    r19 r20  
    8484                private HashMap<Integer, ArrayList<Double[][]>> csize; 
    8585                 
     86                /* debug vars */ 
     87                @SuppressWarnings("unused") 
    8688                private boolean show_biggest = true; 
    8789                 
     90                @SuppressWarnings("unused") 
    8891                private int CFOUND = 0; 
     92                @SuppressWarnings("unused") 
    8993                private int CNOTFOUND = 0; 
    9094                 
     
    260264                                //} 
    261265 
    262                                 // now it can happen that we dont find a cluster because we deleted it previously (too few instances) 
     266                                // now it can happen that we do not find a cluster because we deleted it previously (too few instances) 
    263267                                // or we get bigger distance measures from weka so that we are completely outside of our clusters. 
    264268                                // in these cases we just find the nearest cluster to our instance and use it for classification. 
     
    280284                                } 
    281285                                 
    282                                 // here we have the cluster where an instance has the minimum distance between itself the 
     286                                // here we have the cluster where an instance has the minimum distance between itself and the 
    283287                                // instance we want to classify 
    284288                                // if we still have not found a cluster we exit because something is really wrong 
     
    436440                        */ 
    437441                         
    438                     // train one classifier per cluster, we get the clusternumber from the traindata 
     442                    // train one classifier per cluster, we get the cluster number from the traindata 
    439443                    int cnumber; 
    440444                        Iterator<Integer> clusternumber = ctraindata.keySet().iterator(); 
     
    444448                        while ( clusternumber.hasNext() ) { 
    445449                                cnumber = clusternumber.next(); 
    446                                 cclassifier.put(cnumber,setupClassifier()); // das hier ist der eigentliche trainer  
     450                                cclassifier.put(cnumber,setupClassifier());  // this is the classifier used for the cluster  
    447451                                cclassifier.get(cnumber).buildClassifier(ctraindata.get(cnumber)); 
    448452                                //Console.traceln(Level.INFO, String.format("classifier in cluster "+cnumber)); 
  • trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaTraining2.java

    r2 r20  
    1010 
    1111/** 
    12  * Programmatic WekaBaggingTraining 
     12 * Programmatic WekaTraining 
    1313 * 
    1414 * first parameter is Trainer Name. 
Note: See TracChangeset for help on using the changeset viewer.