Example usage for weka.classifiers.meta FilteredClassifier distributionForInstance

List of usage examples for weka.classifiers.meta FilteredClassifier distributionForInstance

Introduction

In this page you can find the example usage for weka.classifiers.meta FilteredClassifier distributionForInstance.

Prototype

public double[] distributionForInstance(Instance instance) throws Exception 

Source Link

Document

Classifies a given instance after filtering.

Usage

From source file:mulan.classifier.transformation.MultiLabelStacking.java

License:Open Source License

/**
 * Builds the base-level classifiers./*from  ww  w.  j  a  va2  s . co m*/
 * Their predictions are gathered in the baseLevelPredictions member
 * @param trainingSet 
 * @throws Exception
 */
public void buildBaseLevel(MultiLabelInstances trainingSet) throws Exception {
    train = new Instances(trainingSet.getDataSet());
    baseLevelData = new Instances[numLabels];
    baseLevelEnsemble = AbstractClassifier.makeCopies(baseClassifier, numLabels);
    if (normalize) {
        maxProb = new double[numLabels];
        minProb = new double[numLabels];
        Arrays.fill(minProb, 1);
    }
    // initialize the table holding the predictions of the first level
    // classifiers for each label for every instance of the training set
    baseLevelPredictions = new double[train.numInstances()][numLabels];

    for (int labelIndex = 0; labelIndex < numLabels; labelIndex++) {
        debug("Label: " + labelIndex);
        // transform the dataset according to the BR method
        baseLevelData[labelIndex] = BinaryRelevanceTransformation.transformInstances(train, labelIndices,
                labelIndices[labelIndex]);
        // attach indexes in order to keep track of the original positions
        baseLevelData[labelIndex] = new Instances(attachIndexes(baseLevelData[labelIndex]));
        // prepare the transformed dataset for stratified x-fold cv
        Random random = new Random(1);
        baseLevelData[labelIndex].randomize(random);
        baseLevelData[labelIndex].stratify(numFolds);
        debug("Creating meta-data");
        for (int j = 0; j < numFolds; j++) {
            debug("Label=" + labelIndex + ", Fold=" + j);
            Instances subtrain = baseLevelData[labelIndex].trainCV(numFolds, j, random);
            // create a filtered meta classifier, used to ignore
            // the index attribute in the build process
            // perform stratified x-fold cv and get predictions
            // for each class for every instance
            FilteredClassifier fil = new FilteredClassifier();
            fil.setClassifier(baseLevelEnsemble[labelIndex]);
            Remove remove = new Remove();
            remove.setAttributeIndices("first");
            remove.setInputFormat(subtrain);
            fil.setFilter(remove);
            fil.buildClassifier(subtrain);

            // Classify test instance
            Instances subtest = baseLevelData[labelIndex].testCV(numFolds, j);
            for (int i = 0; i < subtest.numInstances(); i++) {
                double distribution[] = new double[2];
                distribution = fil.distributionForInstance(subtest.instance(i));
                // Ensure correct predictions both for class values {0,1}
                // and {1,0}
                Attribute classAttribute = baseLevelData[labelIndex].classAttribute();
                baseLevelPredictions[(int) subtest.instance(i)
                        .value(0)][labelIndex] = distribution[classAttribute.indexOfValue("1")];
                if (normalize) {
                    if (distribution[classAttribute.indexOfValue("1")] > maxProb[labelIndex]) {
                        maxProb[labelIndex] = distribution[classAttribute.indexOfValue("1")];
                    }
                    if (distribution[classAttribute.indexOfValue("1")] < minProb[labelIndex]) {
                        minProb[labelIndex] = distribution[classAttribute.indexOfValue("1")];
                    }
                }
            }
        }
        // now we can detach the indexes from the first level datasets
        baseLevelData[labelIndex] = detachIndexes(baseLevelData[labelIndex]);

        debug("Building base classifier on full data");
        // build base classifier on the full training data
        baseLevelEnsemble[labelIndex].buildClassifier(baseLevelData[labelIndex]);
        baseLevelData[labelIndex].delete();
    }

    if (normalize) {
        normalizePredictions();
    }

}