Example usage for weka.core Instances add

List of usage examples for weka.core Instances add

Introduction

In this page you can find the example usage for weka.core Instances add.

Prototype

@Override
public boolean add(Instance instance) 

Source Link

Document

Adds one instance to the end of the set.

Usage

From source file:moa.tud.ke.patching.InstanceStore.java

/**
 * Merges all the batches of instances.//from ww  w.jav  a 2  s.c om
 * Probably theres a way to speed this up?
 * @return
 */
private Instances mergeAllInstances() {

    //        System.out.println("Merging instances of "+this.batches.size()+" batches.");
    if (this.batches.size() == 0) {
        return null;
    }

    Instances merged = new Instances(this.batches.getFirst()); // deep copy necessary!

    if (this.batches.size() == 1) {
        return merged;
    }

    for (int i = 1; i < this.batches.size(); i++) {
        Instances inst = this.batches.get(i);
        Iterator it = inst.iterator();
        while (it.hasNext()) {
            Instance in = (Instance) it.next();
            merged.add(in);
        }
    }
    return merged;
}

From source file:moa.tud.ke.patching.Patching.java

/**
 * Classifies an instance by checking if it lies in an error region and then
 * using the respective patch, or just using the base classifier otherwise.
 *
 * @param inst//from w ww  .j  a  v  a 2  s.c  o  m
 * @return
 * @throws Exception
 */
public double classifyInstance(weka.core.Instance inst) throws Exception {

    int region = -1;
    int defaultDecider = -1;
    int amountDeciders = -1;
    double label;

    weka.core.Instance origInst = inst;

    try {
        if (this.regionDecider != null) {

            // Handling of optional usage of the base class as an additional attribute.
            if (this.useBaseClassAsAttribute.isSet()) {
                Instances tmp = new Instances(this.prototypeData); // deep copy of our empty prototypeData
                tmp.add(inst);
                tmp = addBaseClassToInstances(tmp);
                weka.core.Instance inst2 = tmp.get(0);
                inst = inst2;
                inst2 = null;
            }

            // Pre-classify instance and retrieve the used Decider
            double regClass = this.regionDecider.classifyInstance(inst);

            if (regClass == 0) { // only if its in a "wrong" region

                Boolean isMultiDecider = false;
                if (this.regionDecider.getAmountOfDeciders() > 1) {
                    isMultiDecider = true;
                }

                Classifier patch;

                if (isMultiDecider) { // a) if the classifier can disciminate different regions
                    region = this.regionDecider.getLastUsedDecider();

                    patch = (Classifier) regionPatches.elementAt(region);
                    if (patch != null) {
                        return patch.classifyInstance(inst);
                    }
                } else { // case b: we only have a 0/1 information about if its in the error region or not.
                    patch = (Classifier) regionPatches.elementAt(0);
                    if (patch != null) {
                        return patch.classifyInstance(inst);
                    }

                }
            } else { // if its not in a "wrong" region, return the class from the base classifier
                if (this.useBaseClassAsAttribute.isSet()) {
                    // this has maybe already been calculated into the first attribute, so we dont need to 
                    // classify this instance again.
                    return inst.value(0);
                }
            }
        }
    } catch (Exception e) {
        System.err.println("AdaptivePatching : Error in classifyInstance while using regionDecider.");
        System.out.println("Region: " + region + " DefaultDecider:" + defaultDecider + " amountDeciders:"
                + amountDeciders + " regionPatches#:" + regionPatches.size());
        e.printStackTrace();
    }

    return baseClassifier.classifyInstance(origInst);
}

From source file:motaz.CODB.java

License:Open Source License

public Instances dataObjectsClassLableSubset(int clIndx) {
    Instances insts = database.getInstances();
    insts.delete();//from  ww  w. j a  va2 s  . co m
    for (int i = 0; i < database.size(); i++) {
        DataObject dataObject = (DataObject) database.getDataObject(Integer.toString(i));
        int attIndex = dataObject.getInstance().numAttributes() - 1;
        if ((int) dataObject.getInstance().value(attIndex) == clIndx)
            insts.add(dataObject.getInstance());
    }
    return insts;
}

From source file:motaz.CODB.java

License:Open Source License

public Instances dataObjectsKnnSubset(DataObject dataObject) {
    Instances insts = database.getInstances();
    insts.delete();//  w w  w . j a v  a 2  s.  c om
    List knn = new ArrayList();

    knn = myknn(getK(), dataObject);

    for (int i = 0; i < knn.size(); i++) {
        PriorityQueueElement pqe = (PriorityQueueElement) knn.get(i);
        DataObject knndataObject = (DataObject) pqe.getObject();
        insts.add(knndataObject.getInstance());
    }
    return insts;
}

From source file:mulan.classifier.meta.HierarchyBuilder.java

License:Open Source License

private ArrayList<String>[] clustering(int clusters, List<String> labels, MultiLabelInstances mlData,
        boolean balanced) {
    ArrayList<String>[] childrenLabels = new ArrayList[clusters];
    for (int i = 0; i < clusters; i++) {
        childrenLabels[i] = new ArrayList<String>();
    }//  www.j  a  v  a2 s .c  o  m

    // transpose data and keep only labels in the parameter list
    int numInstances = mlData.getDataSet().numInstances();
    ArrayList<Attribute> attInfo = new ArrayList<Attribute>(numInstances);
    for (int i = 0; i < numInstances; i++) {
        Attribute att = new Attribute("instance" + (i + 1));
        attInfo.add(att);
    }
    System.out.println("constructing instances");
    Instances transposed = new Instances("transposed", attInfo, 0);
    for (int i = 0; i < labels.size(); i++) {
        double[] values = new double[numInstances];
        for (int j = 0; j < numInstances; j++) {
            values[j] = mlData.getDataSet().instance(j).value(mlData.getDataSet().attribute(labels.get(i)));
        }
        Instance newInstance = DataUtils.createInstance(mlData.getDataSet().instance(0), 1, values);
        transposed.add(newInstance);
    }

    if (!balanced) {
        EM clusterer = new EM();
        try {
            // cluster the labels
            clusterer.setNumClusters(clusters);
            System.out.println("clustering");
            clusterer.buildClusterer(transposed);
            // return the clustering
            for (int i = 0; i < labels.size(); i++) {
                childrenLabels[clusterer.clusterInstance(transposed.instance(i))].add(labels.get(i));
            }
        } catch (Exception ex) {
            Logger.getLogger(HierarchyBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else {
        ConstrainedKMeans clusterer = new ConstrainedKMeans();
        try {
            // cluster the labels
            clusterer.setMaxIterations(20);
            clusterer.setNumClusters(clusters);
            System.out.println("balanced clustering");
            clusterer.buildClusterer(transposed);
            // return the clustering
            for (int i = 0; i < labels.size(); i++) {
                childrenLabels[clusterer.clusterInstance(transposed.instance(i))].add(labels.get(i));
            }
        } catch (Exception ex) {
            Logger.getLogger(HierarchyBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //==================================================12-17
    for (int i = 0; i < childrenLabels.length; i++) {
        System.out.println(childrenLabels[i]);
    }
    //==================================================
    return childrenLabels;
}

From source file:mulan.classifier.meta.HierarchyBuilder.java

License:Open Source License

/**
 * Creates the hierarchical dataset according to the original multilabel
 * instances object and the constructed label hierarchy
 *
 * @param mlData the original multilabel instances
 * @param metaData the metadata of the constructed label hierarchy
 * @return the produced dataset/*from   www  .  j a v  a 2 s . c om*/
 * @throws InvalidDataFormatException 
 */
public static MultiLabelInstances createHierarchicalDataset(MultiLabelInstances mlData, LabelsMetaData metaData)
        throws InvalidDataFormatException {
    Set<String> leafLabels = mlData.getLabelsMetaData().getLabelNames();
    Set<String> metaLabels = metaData.getLabelNames();
    for (String string : leafLabels) {
        metaLabels.remove(string);
    }
    Instances dataSet = mlData.getDataSet();
    int numMetaLabels = metaLabels.size();

    // copy existing attributes
    ArrayList<Attribute> atts = new ArrayList<Attribute>(dataSet.numAttributes() + numMetaLabels);
    for (int i = 0; i < dataSet.numAttributes(); i++) {
        atts.add(dataSet.attribute(i));
    }

    ArrayList<String> labelValues = new ArrayList<String>();
    labelValues.add("0");
    labelValues.add("1");

    // add metalabel attributes
    for (String metaLabel : metaLabels) {
        atts.add(new Attribute(metaLabel, labelValues));
    }

    // initialize dataset
    Instances newDataSet = new Instances("hierarchical", atts, dataSet.numInstances());

    // copy features and labels, set metalabels
    for (int i = 0; i < dataSet.numInstances(); i++) {
        //System.out.println("Constructing instance " + (i+1) + "/"  + dataSet.numInstances());
        // initialize new values
        double[] newValues = new double[newDataSet.numAttributes()];
        Arrays.fill(newValues, 0);

        // copy features and labels
        double[] values = dataSet.instance(i).toDoubleArray();
        System.arraycopy(values, 0, newValues, 0, values.length);

        // set metalabels
        for (String label : leafLabels) {
            Attribute att = dataSet.attribute(label);
            if (att.value((int) dataSet.instance(i).value(att)).equals("1")) {
                //System.out.println(label);
                //System.out.println(Arrays.toString(metaData.getLabelNames().toArray()));
                LabelNode currentNode = metaData.getLabelNode(label);
                // put 1 all the way up to the root, unless you see a 1, in which case stop
                while (currentNode.hasParent()) {
                    currentNode = currentNode.getParent();
                    Attribute currentAtt = newDataSet.attribute(currentNode.getName());
                    // change the following to refer to the array
                    if (newValues[atts.indexOf(currentAtt)] == 1) // no need to go more up
                    {
                        break;
                    } else // put 1
                    {
                        newValues[atts.indexOf(currentAtt)] = 1;
                    }
                }
            }
        }
        Instance instance = dataSet.instance(i);
        newDataSet.add(DataUtils.createInstance(instance, instance.weight(), newValues));
    }
    return new MultiLabelInstances(newDataSet, metaData);
}

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

License:Open Source License

@Override
protected void buildInternal(MultiLabelInstances trainingSet) throws Exception {
    // Virtual label models
    debug("Building calibration label models");
    System.out.println("Building calibration label models");
    virtualLabelModels = new BinaryRelevance(getBaseClassifier());
    virtualLabelModels.setDebug(getDebug());
    virtualLabelModels.build(trainingSet);

    // One-vs-one models
    numModels = ((numLabels) * (numLabels - 1)) / 2;
    oneVsOneModels = AbstractClassifier.makeCopies(getBaseClassifier(), numModels);
    nodata = new boolean[numModels];
    metaDataTest = new Instances[numModels];

    Instances trainingData = trainingSet.getDataSet();

    int counter = 0;
    // Creation of one-vs-one models
    for (int label1 = 0; label1 < numLabels - 1; label1++) {
        // Attribute of label 1
        Attribute attrLabel1 = trainingData.attribute(labelIndices[label1]);
        for (int label2 = label1 + 1; label2 < numLabels; label2++) {
            debug("Building one-vs-one model " + (counter + 1) + "/" + numModels);
            System.out.println("Building one-vs-one model " + (counter + 1) + "/" + numModels);
            // Attribute of label 2
            Attribute attrLabel2 = trainingData.attribute(labelIndices[label2]);

            // initialize training set
            Instances dataOneVsOne = new Instances(trainingData, 0);
            // filter out examples with no preference
            for (int i = 0; i < trainingData.numInstances(); i++) {
                Instance tempInstance;//from w w w . ja va2 s.  c  om
                if (trainingData.instance(i) instanceof SparseInstance) {
                    tempInstance = new SparseInstance(trainingData.instance(i));
                } else {
                    tempInstance = new DenseInstance(trainingData.instance(i));
                }

                int nominalValueIndex;
                nominalValueIndex = (int) tempInstance.value(labelIndices[label1]);
                String value1 = attrLabel1.value(nominalValueIndex);
                nominalValueIndex = (int) tempInstance.value(labelIndices[label2]);
                String value2 = attrLabel2.value(nominalValueIndex);

                if (!value1.equals(value2)) {
                    tempInstance.setValue(attrLabel1, value1);
                    dataOneVsOne.add(tempInstance);
                }
            }

            // remove all labels apart from label1 and place it at the end
            Reorder filter = new Reorder();
            int numPredictors = trainingData.numAttributes() - numLabels;
            int[] reorderedIndices = new int[numPredictors + 1];
            for (int i = 0; i < numPredictors; i++) {
                reorderedIndices[i] = featureIndices[i];
            }
            reorderedIndices[numPredictors] = labelIndices[label1];
            filter.setAttributeIndicesArray(reorderedIndices);
            filter.setInputFormat(dataOneVsOne);
            dataOneVsOne = Filter.useFilter(dataOneVsOne, filter);
            //System.out.println(dataOneVsOne.toString());
            dataOneVsOne.setClassIndex(numPredictors);

            // build model label1 vs label2
            if (dataOneVsOne.size() > 0) {
                oneVsOneModels[counter].buildClassifier(dataOneVsOne);
            } else {
                nodata[counter] = true;
            }
            dataOneVsOne.delete();
            metaDataTest[counter] = dataOneVsOne;
            counter++;
        }
    }
}

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

License:Open Source License

@Override
protected void buildInternal(MultiLabelInstances mlDataSet) throws Exception {
    Instances data = mlDataSet.getDataSet();
    format = new Instances(data, 0);
    int numInstances = data.numInstances();

    ListInstancePerLabel = new HashMap<LabelSet, ArrayList<Instance>>();
    for (int i = 0; i < numInstances; i++) {
        double[] dblLabels = new double[numLabels];
        for (int j = 0; j < numLabels; j++) {
            int index = labelIndices[j];
            double value = Double.parseDouble(data.attribute(index).value((int) data.instance(i).value(index)));
            dblLabels[j] = value;/*w ww . ja v a 2  s .  co m*/
        }
        LabelSet labelSet = new LabelSet(dblLabels);
        if (ListInstancePerLabel.containsKey(labelSet)) {
            ListInstancePerLabel.get(labelSet).add(data.instance(i));
        } else {
            ArrayList<Instance> li = new ArrayList<Instance>();
            li.add(data.instance(i));
            ListInstancePerLabel.put(labelSet, li);
        }
    }

    // Iterates the structure and a) if occurences of a labelset are higher
    // than p parameter then add them to the training set, b) if occurences
    // are less, then depending on the strategy discard/reintroduce them
    Instances newData = new Instances(data, 0);
    Iterator<LabelSet> it = ListInstancePerLabel.keySet().iterator();
    while (it.hasNext()) {
        LabelSet ls = it.next();
        ArrayList<Instance> instances = ListInstancePerLabel.get(ls);
        if (instances.size() > p) {
            for (int i = 0; i < instances.size(); i++) {
                newData.add(instances.get(i));
            }
        } else {
            ArrayList<Instance> processed = processRejected(ls);
            newData.addAll(processed);
        }
    }

    super.buildInternal(new MultiLabelInstances(newData, mlDataSet.getLabelsMetaData()));
}

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

License:Open Source License

/**
 * Initializes all the parameters used in the meta-level.
 * Calculates the correlated labels if meta-level pruning is applied.
 *
 * @param dataSet/*from  w  ww . j a  v  a 2 s. co  m*/
 * @param metaClassifier
 * @param includeAttrs
 * @param metaPercentage
 * @param eval
 * @throws Exception
 */
public void initializeMetaLevel(MultiLabelInstances dataSet, Classifier metaClassifier, boolean includeAttrs,
        double metaPercentage, ASEvaluation eval) throws Exception {
    this.metaClassifier = metaClassifier;
    metaLevelEnsemble = AbstractClassifier.makeCopies(metaClassifier, numLabels);
    metaLevelData = new Instances[numLabels];
    metaLevelFilteredEnsemble = new FilteredClassifier[numLabels];
    this.includeAttrs = includeAttrs;
    // calculate the number of correlated labels that corresponds to the
    // given percentage
    topkCorrelated = (int) Math.floor(metaPercentage * numLabels);
    if (topkCorrelated < 1) {
        debug("Too small percentage, selecting k=1");
        topkCorrelated = 1;
    }
    if (topkCorrelated < numLabels) {// pruning should be applied
        selectedAttributes = new int[numLabels][];
        if (eval == null) {// calculate the PhiCoefficient
            Statistics phi = new Statistics();
            phi.calculatePhi(dataSet);
            for (int i = 0; i < numLabels; i++) {
                selectedAttributes[i] = phi.topPhiCorrelatedLabels(i, topkCorrelated);
            }
        } else {// apply feature selection
            AttributeSelection attsel = new AttributeSelection();
            Ranker rankingMethod = new Ranker();
            rankingMethod.setNumToSelect(topkCorrelated);
            attsel.setEvaluator(eval);
            attsel.setSearch(rankingMethod);
            // create a dataset consisting of all the classes of each
            // instance plus the class we want to select attributes from
            for (int i = 0; i < numLabels; i++) {
                ArrayList<Attribute> attributes = new ArrayList<Attribute>();

                for (int j = 0; j < numLabels; j++) {
                    attributes.add(train.attribute(labelIndices[j]));
                }
                attributes.add(train.attribute(labelIndices[i]).copy("meta"));

                Instances iporesult = new Instances("Meta format", attributes, 0);
                iporesult.setClassIndex(numLabels);
                for (int k = 0; k < train.numInstances(); k++) {
                    double[] values = new double[numLabels + 1];
                    for (int m = 0; m < numLabels; m++) {
                        values[m] = Double.parseDouble(train.attribute(labelIndices[m])
                                .value((int) train.instance(k).value(labelIndices[m])));
                    }
                    values[numLabels] = Double.parseDouble(train.attribute(labelIndices[i])
                            .value((int) train.instance(k).value(labelIndices[i])));
                    Instance metaInstance = DataUtils.createInstance(train.instance(k), 1, values);
                    metaInstance.setDataset(iporesult);
                    iporesult.add(metaInstance);
                }
                attsel.SelectAttributes(iporesult);
                selectedAttributes[i] = attsel.selectedAttributes();
                iporesult.delete();
            }
        }
    }
}

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

License:Open Source License

/**
 * Attaches an index attribute at the beginning of each instance
 *
 * @param original//from w ww  .ja va  2  s  . c om
 * @return
 */
protected Instances attachIndexes(Instances original) {

    ArrayList<Attribute> attributes = new ArrayList<Attribute>(original.numAttributes() + 1);

    for (int i = 0; i < original.numAttributes(); i++) {
        attributes.add(original.attribute(i));
    }
    // Add attribute for holding the index at the beginning.
    attributes.add(0, new Attribute("Index"));
    Instances transformed = new Instances("Meta format", attributes, 0);
    for (int i = 0; i < original.numInstances(); i++) {
        Instance newInstance;
        newInstance = (Instance) original.instance(i).copy();
        newInstance.setDataset(null);
        newInstance.insertAttributeAt(0);
        newInstance.setValue(0, i);

        transformed.add(newInstance);
    }

    transformed.setClassIndex(original.classIndex() + 1);
    return transformed;
}