Example usage for weka.core Instance setDataset

List of usage examples for weka.core Instance setDataset

Introduction

In this page you can find the example usage for weka.core Instance setDataset.

Prototype

public void setDataset(Instances instances);

Source Link

Document

Sets the reference to the dataset.

Usage

From source file:moa.streams.generators.STAGGERGenerator.java

License:Open Source License

@Override
public Instance nextInstance() {

    int size = 0, color = 0, shape = 0, group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        size = this.instanceRandom.nextInt(3);
        color = this.instanceRandom.nextInt(3);
        shape = this.instanceRandom.nextInt(3);

        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(size, color, shape);
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;/*from ww w .  jav a2  s .  co  m*/
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }

    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, size);
    inst.setValue(1, color);
    inst.setValue(2, shape);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:moa.streams.generators.STAGGERGenerator2.java

License:Open Source License

@Override
public Instance nextInstance() {

    int size = 0, color = 0, shape = 0, group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        size = this.instanceRandom.nextInt(3);
        color = this.instanceRandom.nextInt(3);
        shape = this.instanceRandom.nextInt(3);

        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(size, color, shape);
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;/*from  w  w w .  j ava 2 s.c  o m*/
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }

    //TODO refactor
    //sudden drift
    flag = flag + 1;
    if (flag > this.changeIntervalOption.getValue()) {
        this.functionOption.setValue(3);
    }
    if (flag > 2 * this.changeIntervalOption.getValue()) {
        this.functionOption.setValue(2);
    }

    if (flag > 3 * this.changeIntervalOption.getValue()) {
        this.functionOption.setValue(2);
    }

    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, size);
    inst.setValue(1, color);
    inst.setValue(2, shape);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:moa.streams.generators.WaveformGenerator.java

License:Open Source License

@Override
public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);
    int waveform = this.instanceRandom.nextInt(NUM_CLASSES);
    int choiceA = 0, choiceB = 0;
    switch (waveform) {
    case 0://w ww.j av  a  2 s.  co m
        choiceA = 0;
        choiceB = 1;
        break;
    case 1:
        choiceA = 0;
        choiceB = 2;
        break;
    case 2:
        choiceA = 1;
        choiceB = 2;
        break;

    }
    double multiplierA = this.instanceRandom.nextDouble();
    double multiplierB = 1.0 - multiplierA;
    for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) {
        inst.setValue(i, (multiplierA * hFunctions[choiceA][i]) + (multiplierB * hFunctions[choiceB][i])
                + this.instanceRandom.nextGaussian());
    }
    if (this.addNoiseOption.isSet()) {
        for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) {
            inst.setValue(i, this.instanceRandom.nextGaussian());
        }
    }
    inst.setClassValue(waveform);
    return inst;
}

From source file:moa.streams.generators.WaveformGeneratorDrift.java

License:Open Source License

@Override
public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);
    int waveform = this.instanceRandom.nextInt(NUM_CLASSES);
    int choiceA = 0, choiceB = 0;
    switch (waveform) {
    case 0:/*ww w.  j  a v a 2s .com*/
        choiceA = 0;
        choiceB = 1;
        break;
    case 1:
        choiceA = 0;
        choiceB = 2;
        break;
    case 2:
        choiceA = 1;
        choiceB = 2;
        break;

    }
    double multiplierA = this.instanceRandom.nextDouble();
    double multiplierB = 1.0 - multiplierA;
    for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) {
        inst.setValue(this.numberAttribute[i], (multiplierA * hFunctions[choiceA][i])
                + (multiplierB * hFunctions[choiceB][i]) + this.instanceRandom.nextGaussian());
    }
    if (this.addNoiseOption.isSet()) {
        for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) {
            inst.setValue(this.numberAttribute[i], this.instanceRandom.nextGaussian());
        }
    }
    inst.setClassValue(waveform);
    return inst;
}

From source file:mulan.classifier.lazy.IBLR_ML.java

License:Open Source License

@Override
protected void buildInternal(MultiLabelInstances mltrain) throws Exception {
    super.buildInternal(mltrain);

    classifier = new Classifier[numLabels];

    /*/*  w w  w .j a  v a  2  s .c om*/
     * Create the new training data with label info as features.
     */
    Instances[] trainingDataForLabel = new Instances[numLabels];
    ArrayList<Attribute> attributes = new ArrayList<Attribute>();
    if (addFeatures == true) {// create an ArrayList with numAttributes size
        for (int i = 1; i <= train.numAttributes(); i++) {
            attributes.add(new Attribute("Attr." + i));
        }
    } else {// create a FastVector with numLabels size
        for (int i = 1; i <= numLabels; i++) {
            attributes.add(new Attribute("Attr." + i));
        }
    }
    ArrayList<String> classlabel = new ArrayList<String>();
    classlabel.add("0");
    classlabel.add("1");
    attributes.add(new Attribute("Class", classlabel));
    for (int i = 0; i < trainingDataForLabel.length; i++) {
        trainingDataForLabel[i] = new Instances("DataForLabel" + (i + 1), attributes, train.numInstances());
        trainingDataForLabel[i].setClassIndex(trainingDataForLabel[i].numAttributes() - 1);
    }

    for (int i = 0; i < train.numInstances(); i++) {

        Instances knn = new Instances(lnn.kNearestNeighbours(train.instance(i), numOfNeighbors));
        /*
         * Get the label confidence vector as the additional features.
         */
        double[] confidences = new double[numLabels];
        for (int j = 0; j < numLabels; j++) {
            // compute sum of counts for each label in KNN
            double count_for_label_j = 0;
            for (int k = 0; k < numOfNeighbors; k++) {
                double value = Double.parseDouble(
                        train.attribute(labelIndices[j]).value((int) knn.instance(k).value(labelIndices[j])));
                if (Utils.eq(value, 1.0)) {
                    count_for_label_j++;
                }
            }
            confidences[j] = count_for_label_j / numOfNeighbors;
        }

        double[] attvalue = new double[numLabels + 1];

        if (addFeatures == true) {
            attvalue = new double[train.numAttributes() + 1];

            // Copy the original features
            for (int m = 0; m < featureIndices.length; m++) {
                attvalue[m] = train.instance(i).value(featureIndices[m]);
            }
            // Copy the label confidences as additional features
            for (int m = 0; m < confidences.length; m++) {
                attvalue[train.numAttributes() - numLabels + m] = confidences[m];
            }
        } else {
            // Copy the label confidences as features
            for (int m = 0; m < confidences.length; m++) {
                attvalue[m] = confidences[m];
            }
        }

        // Add the class labels and finish the new training data
        for (int j = 0; j < numLabels; j++) {
            attvalue[attvalue.length - 1] = Double.parseDouble(
                    train.attribute(labelIndices[j]).value((int) train.instance(i).value(labelIndices[j])));
            Instance newInst = DataUtils.createInstance(train.instance(i), 1, attvalue);
            newInst.setDataset(trainingDataForLabel[j]);
            if (attvalue[attvalue.length - 1] > 0.5) {
                newInst.setClassValue("1");
            } else {
                newInst.setClassValue("0");
            }
            trainingDataForLabel[j].add(newInst);
        }

    }

    // for every label create a corresponding classifier.
    for (int i = 0; i < numLabels; i++) {
        classifier[i] = new Logistic();
        classifier[i].buildClassifier(trainingDataForLabel[i]);
    }

}

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

License:Open Source License

private void makePrediction(HMCNode currentNode, Instance instance, boolean[] predictedLabels,
        double[] confidences) throws Exception {
    //System.out.println("Node: " + currentNode.getName());

    double[] values = instance.toDoubleArray();

    Instance transformed = DataUtils.createInstance(instance, 1, values);

    // delete all labels apart from those of current node
    int[] currentNodeLabelIndices = currentNode.getLabelIndices();
    Set<Integer> indicesToKeep = new HashSet<Integer>();
    for (int i = 0; i < currentNodeLabelIndices.length; i++) {
        String labelToKeep = currentNode.getHeader().attribute(currentNodeLabelIndices[i]).name();
        indicesToKeep.add(labelIndices[labelsAndIndices.get(labelToKeep)]);
    }/* w w w.  j a  v  a  2 s. co m*/

    if (labelIndices.length - indicesToKeep.size() != 0) {
        int[] indicesToDelete = new int[labelIndices.length - indicesToKeep.size()];
        int counter = 0;
        for (int i = 0; i < labelIndices.length; i++) {
            if (indicesToKeep.contains(labelIndices[i])) {
                continue;
            }
            indicesToDelete[counter] = labelIndices[i];
            counter++;
        }
        transformed = RemoveAllLabels.transformInstance(transformed, indicesToDelete);
    }

    transformed.setDataset(currentNode.getHeader());
    // add as many attributes as the children    
    //        System.out.println("header:" + currentNode.getHeader());
    //System.out.println(transformed.toString());

    //debug("working at node " + currentNode.getName());
    //debug(Arrays.toString(predictedLabels));        
    NoClassifierEvals++;
    MultiLabelOutput pred = currentNode.makePrediction(transformed);
    int[] indices = currentNode.getLabelIndices();
    boolean[] temp = pred.getBipartition();

    for (int i = 0; i < temp.length; i++) {
        String childName = currentNode.getHeader().attribute(indices[i]).name();
        //System.out.println("childName:" + childName);
        int idx = labelsAndIndices.get(childName);
        if (pred.getBipartition()[i] == true) {
            predictedLabels[idx] = true;
            confidences[idx] = pred.getConfidences()[i];
            if (currentNode.hasChildren()) {
                for (HMCNode child : currentNode.getChildren()) {
                    if (child.getName().equals(childName)) {
                        makePrediction(child, instance, predictedLabels, confidences);
                    }
                }
            }
        } else {
            predictedLabels[idx] = false;
            Set<String> descendantLabels = originalMetaData.getLabelNode(childName).getDescendantLabels();
            if (descendantLabels != null) {
                for (String label : descendantLabels) {
                    int idx2 = labelsAndIndices.get(label);
                    predictedLabels[idx2] = false;
                    confidences[idx2] = pred.getConfidences()[i];
                }
            }
        }
    }
}

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

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    Instance transformed = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray());
    for (int i = 0; i < numMetaLabels; i++) {
        transformed.insertAttributeAt(transformed.numAttributes());
    }//from   ww w.ja  v  a 2  s .  c om

    transformed.setDataset(header);
    MultiLabelOutput mlo = hmc.makePrediction(transformed);
    boolean[] oldBipartition = mlo.getBipartition();
    //System.out.println("old:" + Arrays.toString(oldBipartition));
    boolean[] newBipartition = new boolean[numLabels];
    System.arraycopy(oldBipartition, 0, newBipartition, 0, numLabels);
    //System.out.println("new:" + Arrays.toString(newBipartition));
    double[] oldConfidences = mlo.getConfidences();
    double[] newConfidences = new double[numLabels];
    System.arraycopy(oldConfidences, 0, newConfidences, 0, numLabels);
    MultiLabelOutput newMLO = new MultiLabelOutput(newBipartition, newConfidences);
    return newMLO;
}

From source file:mulan.classifier.meta.thresholding.MetaLabeler.java

License:Open Source License

@Override
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    //System.out.println(instance);
    MultiLabelOutput mlo = baseLearner.makePrediction(instance);
    int[] arrayOfRankink = new int[numLabels];
    boolean[] predictedLabels = new boolean[numLabels];
    Instance modifiedIns = modifiedInstanceX(instance, metaDatasetChoice);
    //System.out.println(modifiedIns);
    modifiedIns.insertAttributeAt(modifiedIns.numAttributes());
    // set dataset to instance
    modifiedIns.setDataset(classifierInstances);
    //get the bipartition_key after classify the instance
    int bipartition_key;
    if (classChoice.compareTo("Nominal-Class") == 0) {
        double classify_key = classifier.classifyInstance(modifiedIns);
        String s = classifierInstances.attribute(classifierInstances.numAttributes() - 1)
                .value((int) classify_key);
        bipartition_key = Integer.valueOf(s);
    } else { //Numeric-Class
        double classify_key = classifier.classifyInstance(modifiedIns);
        bipartition_key = (int) Math.round(classify_key);
    }/*from   ww  w.  j  a va  2 s  . co  m*/
    if (mlo.hasRanking()) {
        arrayOfRankink = mlo.getRanking();
        for (int i = 0; i < numLabels; i++) {
            if (arrayOfRankink[i] <= bipartition_key) {
                predictedLabels[i] = true;
            } else {
                predictedLabels[i] = false;
            }
        }
    }
    MultiLabelOutput final_mlo = new MultiLabelOutput(predictedLabels, mlo.getConfidences());
    return final_mlo;
}

From source file:mulan.classifier.meta.thresholding.ThresholdPrediction.java

License:Open Source License

@Override
protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    boolean[] predictedLabels = new boolean[numLabels];
    Instance modifiedIns = modifiedInstanceX(instance, metaDatasetChoice);

    modifiedIns.insertAttributeAt(modifiedIns.numAttributes());
    // set dataset to instance
    modifiedIns.setDataset(classifierInstances);
    double bipartition_key = classifier.classifyInstance(modifiedIns);

    MultiLabelOutput mlo = baseLearner.makePrediction(instance);
    double[] arrayOfScores = new double[numLabels];
    arrayOfScores = mlo.getConfidences();
    for (int i = 0; i < numLabels; i++) {
        if (arrayOfScores[i] >= bipartition_key) {
            predictedLabels[i] = true;/*from  ww w .  j a va  2  s.c  o m*/
        } else {
            predictedLabels[i] = false;
        }
    }
    MultiLabelOutput final_mlo = new MultiLabelOutput(predictedLabels, mlo.getConfidences());
    return final_mlo;

}

From source file:mulan.classifier.neural.BPMLL.java

License:Open Source License

public MultiLabelOutput makePredictionInternal(Instance instance) throws InvalidDataException {

    Instance inputInstance = null;
    if (nominalToBinaryFilter != null) {
        try {/*from  w  ww  .j  av a  2  s  .c  om*/
            nominalToBinaryFilter.input(instance);
            inputInstance = nominalToBinaryFilter.output();
            inputInstance.setDataset(null);
        } catch (Exception ex) {
            throw new InvalidDataException("The input instance for prediction is invalid. "
                    + "Instance is not consistent with the data the model was built for.");
        }
    } else {
        inputInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray());
    }

    int numAttributes = inputInstance.numAttributes();
    if (numAttributes < model.getNetInputSize()) {
        throw new InvalidDataException("Input instance do not have enough attributes "
                + "to be processed by the model. Instance is not consistent with the data the model was built for.");
    }

    // if instance has more attributes than model input, we assume that true outputs
    // are there, so we remove them
    List<Integer> someLabelIndices = new ArrayList<Integer>();
    boolean labelsAreThere = false;
    if (numAttributes > model.getNetInputSize()) {
        for (int index : this.labelIndices) {
            someLabelIndices.add(index);
        }

        labelsAreThere = true;
    }

    if (normalizeAttributes) {
        normalizer.normalize(inputInstance);
    }

    int inputDim = model.getNetInputSize();
    double[] inputPattern = new double[inputDim];
    int indexCounter = 0;
    for (int attrIndex = 0; attrIndex < numAttributes; attrIndex++) {
        if (labelsAreThere && someLabelIndices.contains(attrIndex)) {
            continue;
        }
        inputPattern[indexCounter] = inputInstance.value(attrIndex);
        indexCounter++;
    }

    double[] labelConfidences = model.feedForward(inputPattern);
    double threshold = thresholdF.computeThreshold(labelConfidences);
    boolean[] labelPredictions = new boolean[numLabels];
    Arrays.fill(labelPredictions, false);

    for (int labelIndex = 0; labelIndex < numLabels; labelIndex++) {
        if (labelConfidences[labelIndex] > threshold) {
            labelPredictions[labelIndex] = true;
        }
        // translate from bipolar output to binary
        labelConfidences[labelIndex] = (labelConfidences[labelIndex] + 1) / 2;
    }

    MultiLabelOutput mlo = new MultiLabelOutput(labelPredictions, labelConfidences);
    return mlo;
}