Example usage for weka.core Instance toDoubleArray

List of usage examples for weka.core Instance toDoubleArray

Introduction

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

Prototype

public double[] toDoubleArray();

Source Link

Document

Returns the values of each attribute as an array of doubles.

Usage

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)]);
    }//from   ww w. ja va  2s  .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());
    }// w ww . j  a  va 2  s . c o  m

    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.Meta.java

License:Open Source License

/**
 * A method that modify an instance/*from   w ww  . ja v  a  2 s.  co m*/
 *
 * @param instance to modified
 * @param xBased the type for constructing the meta dataset
 * @return a transformed instance for the predictor of labels/threshold
 */
protected Instance modifiedInstanceX(Instance instance, String xBased) {
    Instance modifiedIns = null;
    MultiLabelOutput mlo = null;
    if (xBased.compareTo("Content-Based") == 0) {
        Instance tempInstance = RemoveAllLabels.transformInstance(instance, labelIndices);
        modifiedIns = DataUtils.createInstance(tempInstance, tempInstance.weight(),
                tempInstance.toDoubleArray());
    } else if (xBased.compareTo("Score-Based") == 0) {
        double[] arrayOfScores = new double[numLabels];
        try {
            mlo = baseLearner.makePrediction(instance);
        } catch (InvalidDataException ex) {
            Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ModelInitializationException ex) {
            Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex);
        }
        arrayOfScores = mlo.getConfidences();
        modifiedIns = DataUtils.createInstance(instance, numLabels);
        for (int i = 0; i < numLabels; i++) {
            modifiedIns.setValue(i, arrayOfScores[i]);
        }
    } else { //Rank-Based
        try {
            //Rank-Based
            double[] arrayOfScores = new double[numLabels];
            mlo = baseLearner.makePrediction(instance);
            arrayOfScores = mlo.getConfidences();
            ArrayList<Double> list = new ArrayList();
            for (int i = 0; i < numLabels; i++) {
                list.add(arrayOfScores[i]);
            }
            Collections.sort(list);
            modifiedIns = DataUtils.createInstance(instance, numLabels);
            int j = numLabels - 1;
            for (Double x : list) {
                modifiedIns.setValue(j, x);
                j--;
            }
        } catch (InvalidDataException ex) {
            Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ModelInitializationException ex) {
            Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Meta.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return modifiedIns;
}

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

License:Open Source License

/**
 * A method that fill the array "newValues"
 *
 * @param learner the multi-label learner
 * @param instance the training instances
 * @param newValues the array to fill//www.j  a va2  s .c  om
 * @param xBased the type for constructing the meta dataset
 * @throws Exception
 */
protected void valuesX(MultiLabelLearner learner, Instance instance, double[] newValues, String xBased)
        throws Exception {
    MultiLabelOutput mlo = null;
    if (metaDatasetChoice.compareTo("Content-Based") == 0) {
        double[] values = instance.toDoubleArray();
        for (int i = 0; i < featureIndices.length; i++)
            newValues[i] = values[featureIndices[i]];
    } else if (metaDatasetChoice.compareTo("Score-Based") == 0) {
        mlo = learner.makePrediction(instance);
        double[] values = mlo.getConfidences();
        System.arraycopy(values, 0, newValues, 0, values.length);
    } else if (metaDatasetChoice.compareTo("Rank-Based") == 0) {
        mlo = learner.makePrediction(instance);
        double[] values = mlo.getConfidences();
        ArrayList<Double> list = new ArrayList();
        for (int i = 0; i < numLabels; i++) {
            list.add(values[i]);
        }
        Collections.sort(list);
        int j = numLabels - 1;
        for (Double x : list) {
            newValues[j] = x;
            j--;
        }
    }
}

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

License:Open Source License

protected Instances transformData(MultiLabelInstances trainingData) throws Exception {
    // initialize  classifier instances
    classifierInstances = RemoveAllLabels.transformInstances(trainingData);
    classifierInstances = new Instances(classifierInstances, 0);
    Attribute target = null;/*from w w  w. j  av  a  2s . c o  m*/
    if (classChoice.equals("Nominal-Class")) {
        int countTrueLabels = 0;
        Set<Integer> treeSet = new TreeSet();
        for (int instanceIndex = 0; instanceIndex < trainingData.getDataSet().numInstances(); instanceIndex++) {
            countTrueLabels = 0;
            for (int i = 0; i < numLabels; i++) {
                int labelIndice = labelIndices[i];
                if (trainingData.getDataSet().attribute(labelIndice)
                        .value((int) trainingData.getDataSet().instance(instanceIndex).value(labelIndice))
                        .equals("1")) {
                    countTrueLabels++;
                }
            }
            treeSet.add(countTrueLabels);
        }
        ArrayList<String> classlabel = new ArrayList<String>();
        for (Integer x : treeSet) {
            classlabel.add(x.toString());
        }
        target = new Attribute("Class", classlabel);
    } else if (classChoice.equals("Numeric-Class")) {
        target = new Attribute("Class");
    }
    classifierInstances.insertAttributeAt(target, classifierInstances.numAttributes());
    classifierInstances.setClassIndex(classifierInstances.numAttributes() - 1);

    // create instances
    if (metaDatasetChoice.equals("Content-Based")) {
        for (int instanceIndex = 0; instanceIndex < trainingData.getNumInstances(); instanceIndex++) {
            Instance instance = trainingData.getDataSet().instance(instanceIndex);
            double[] values = instance.toDoubleArray();
            double[] newValues = new double[classifierInstances.numAttributes()];
            for (int i = 0; i < featureIndices.length; i++) {
                newValues[i] = values[featureIndices[i]];
            }

            //set the number of true labels of an instance
            int numTrueLabels = countTrueLabels(instance);
            if (classChoice.compareTo("Nominal-Class") == 0) {
                newValues[newValues.length - 1] = classifierInstances
                        .attribute(classifierInstances.numAttributes() - 1).indexOfValue("" + numTrueLabels);
            } else if (classChoice.compareTo("Numeric-Class") == 0) {
                newValues[newValues.length - 1] = numTrueLabels;
            }
            Instance newInstance = DataUtils.createInstance(instance, instance.weight(), newValues);
            classifierInstances.add(newInstance);
        }
    } else {
        for (int k = 0; k < kFoldsCV; k++) {
            //Split data to train and test sets
            MultiLabelLearner tempLearner;
            MultiLabelInstances mlTest;
            if (kFoldsCV == 1) {
                tempLearner = baseLearner;
                mlTest = trainingData;
            } else {
                Instances train = trainingData.getDataSet().trainCV(kFoldsCV, k);
                Instances test = trainingData.getDataSet().testCV(kFoldsCV, k);
                MultiLabelInstances mlTrain = new MultiLabelInstances(train, trainingData.getLabelsMetaData());
                mlTest = new MultiLabelInstances(test, trainingData.getLabelsMetaData());
                tempLearner = foldLearner.makeCopy();
                tempLearner.build(mlTrain);
            }

            // copy features and labels, set metalabels
            for (int instanceIndex = 0; instanceIndex < mlTest.getDataSet().numInstances(); instanceIndex++) {
                Instance instance = mlTest.getDataSet().instance(instanceIndex);

                // initialize new class values
                double[] newValues = new double[classifierInstances.numAttributes()];

                // create features
                valuesX(tempLearner, instance, newValues, metaDatasetChoice);

                //set the number of true labels of an instance   
                int numTrueLabels = countTrueLabels(instance);
                if (classChoice.compareTo("Nominal-Class") == 0) {
                    newValues[newValues.length - 1] = classifierInstances
                            .attribute(classifierInstances.numAttributes() - 1)
                            .indexOfValue("" + numTrueLabels);
                } else if (classChoice.compareTo("Numeric-Class") == 0) {
                    newValues[newValues.length - 1] = numTrueLabels;
                }

                // add the new instance to  classifierInstances
                Instance newInstance = DataUtils.createInstance(mlTest.getDataSet().instance(instanceIndex),
                        mlTest.getDataSet().instance(instanceIndex).weight(), newValues);
                classifierInstances.add(newInstance);
            }
        }
    }

    return classifierInstances;
}

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

License:Open Source License

public MultiLabelOutput makePredictionInternal(Instance instance) throws InvalidDataException {

    Instance inputInstance = null;// w ww . ja  va  2 s .co  m
    if (nominalToBinaryFilter != null) {
        try {
            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;
}

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

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    boolean[] bipartition = new boolean[numLabels];
    double[] confidences = new double[numLabels];

    Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray());
    for (int counter = 0; counter < numLabels; counter++) {
        double distribution[] = new double[2];
        try {/*from   www  .  ja v a  2  s. c  o  m*/
            distribution = ensemble[counter].distributionForInstance(tempInstance);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        int maxIndex = (distribution[0] > distribution[1]) ? 0 : 1;

        // Ensure correct predictions both for class values {0,1} and {1,0}
        Attribute classAttribute = ensemble[counter].getFilter().getOutputFormat().classAttribute();
        bipartition[chain[counter]] = (classAttribute.value(maxIndex).equals("1")) ? true : false;

        // The confidence of the label being equal to 1
        confidences[chain[counter]] = distribution[classAttribute.indexOfValue("1")];

        tempInstance.setValue(labelIndices[chain[counter]], maxIndex);

    }

    MultiLabelOutput mlo = new MultiLabelOutput(bipartition, confidences);
    return mlo;
}

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

License:Open Source License

@Override
ArrayList<Instance> processRejected(LabelSet ls) {
    switch (strategy) {
    case INFORMATION_LOSS:
        return new ArrayList<Instance>();
    case NO_INFORMATION_LOSS:
        // split LabelSet into smaller ones
        //debug System.out.println("original:" + ls.toString());
        ArrayList<LabelSet> subsets = null;
        try {/*from  w w w  .  j  av  a 2 s . co  m*/
            subsets = ls.getSubsets();
        } catch (Exception ex) {
            Logger.getLogger(PPT.class.getName()).log(Level.SEVERE, null, ex);
        }
        // sort subsets based on size
        Collections.sort(subsets);
        //debug for (LabelSet l: subsets) System.out.println(l.toString());
        ArrayList<LabelSet> subsetsForInsertion = new ArrayList<LabelSet>();
        for (LabelSet l : subsets) {
            // check if it exists in the training set
            if (!ListInstancePerLabel.containsKey(l)) {
                continue;
            }
            // check if it occurs more than p times
            if (ListInstancePerLabel.get(l).size() <= p) {
                continue;
            }
            // check that it has no common elements with
            // previously selected subsets
            boolean foundCommon = false;
            for (LabelSet l2 : subsetsForInsertion) {
                LabelSet temp = LabelSet.intersection(l, l2);
                if (temp.size() != 0) {
                    foundCommon = true;
                    break;
                }
            }
            if (foundCommon) {
                continue;
            } else {
                subsetsForInsertion.add(l);
            }
        }

        // insert subsetsForInsertion with corresponding instances
        // from the original labelset
        ArrayList<Instance> instances = ListInstancePerLabel.get(ls);
        ArrayList<Instance> newInstances = new ArrayList<Instance>();
        for (Instance tempInstance : instances) {
            for (LabelSet l : subsetsForInsertion) {
                double[] temp = tempInstance.toDoubleArray();
                double[] tempLabels = l.toDoubleArray();
                for (int i = 0; i < numLabels; i++) {
                    if (format.attribute(labelIndices[i]).value(0).equals("0")) {
                        temp[labelIndices[i]] = tempLabels[i];
                    } else {
                        temp[labelIndices[i]] = 1 - tempLabels[i];
                    }
                }
                Instance newInstance = DataUtils.createInstance(tempInstance, 1, temp);
                newInstances.add(newInstance);
            }
        }
        return newInstances;
    default:
        return null;
    }
}

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

License:Open Source License

@Override
ArrayList<Instance> processRejected(LabelSet ls) {
    ArrayList<LabelSet> subsets = null;
    ArrayList<Instance> instances = null;
    ArrayList<Instance> newInstances = null;
    switch (strategy) {
    case A://from ww  w .  j a  va2  s  .  c o  m
        // split LabelSet into smaller ones
        //debug System.out.println("original:" + ls.toString());
        subsets = null;
        try {
            subsets = ls.getSubsets();
        } catch (Exception ex) {
            Logger.getLogger(PrunedSets.class.getName()).log(Level.SEVERE, null, ex);
        }
        //System.out.println("subsets: " + subsets.toString());
        ArrayList<LabelSet> sortedSubsets = new ArrayList<LabelSet>();
        for (LabelSet l : subsets) {
            //System.out.println(l.toString());
            // check if it exists in the training set
            if (!ListInstancePerLabel.containsKey(l)) {
                continue;
            }
            // check if it occurs more than p times 
            if (ListInstancePerLabel.get(l).size() <= p) {
                continue;
            }
            //
            boolean added = false;
            for (int i = 0; i < sortedSubsets.size(); i++) {
                LabelSet l2 = sortedSubsets.get(i);
                if (l.size() > l2.size()) {
                    sortedSubsets.add(i, l);
                    //System.out.println("adding " + l.toString());
                    added = true;
                    break;
                }
                if (l.size() == l2.size()
                        && ListInstancePerLabel.get(l).size() > ListInstancePerLabel.get(l2).size()) {
                    sortedSubsets.add(i, l);
                    //System.out.println("adding " + l.toString());
                    added = true;
                    break;
                }
            }
            if (added == false) {
                //System.out.println("adding " + l.toString());
                sortedSubsets.add(l);
            }
            //System.out.println("sorted: " + sortedSubsets.toString());
        }
        // take the top b
        newInstances = new ArrayList<Instance>();
        instances = ListInstancePerLabel.get(ls);
        for (Instance tempInstance : instances) {
            int counter = 0;
            for (LabelSet l : sortedSubsets) {
                double[] temp = tempInstance.toDoubleArray();
                double[] tempLabels = l.toDoubleArray();
                for (int i = 0; i < numLabels; i++) {
                    if (format.attribute(labelIndices[i]).value(0).equals("0"))
                        temp[labelIndices[i]] = tempLabels[i];
                    else
                        temp[labelIndices[i]] = 1 - tempLabels[i];
                }
                Instance newInstance = DataUtils.createInstance(tempInstance, 1, temp);
                newInstances.add(newInstance);
                counter++;
                if (counter == b) {
                    break;
                }
            }
        }
        return newInstances;
    case B:
        // split LabelSet into smaller ones
        //debug 
        System.out.println("original:" + ls.toString());
        subsets = null;
        try {
            subsets = ls.getSubsets();
        } catch (Exception ex) {
            Logger.getLogger(PrunedSets.class.getName()).log(Level.SEVERE, null, ex);
        }
        ArrayList<LabelSet> subsetsForInsertion = new ArrayList<LabelSet>();
        for (LabelSet l : subsets) {
            // check if it exists in the training set
            if (!ListInstancePerLabel.containsKey(l)) {
                continue;
            }
            // check if it occurs more than p times
            if (ListInstancePerLabel.get(l).size() <= p) {
                continue;
            }
            // check if it has more than b elements
            if (l.size() <= b) {
                continue;
            }
            subsetsForInsertion.add(l);
        }

        // insert subsetsForInsertion with corresponding instances
        // from the original labelset
        instances = ListInstancePerLabel.get(ls);
        newInstances = new ArrayList<Instance>();
        for (Instance tempInstance : instances) {
            for (LabelSet l : subsetsForInsertion) {
                double[] temp = tempInstance.toDoubleArray();
                double[] tempLabels = l.toDoubleArray();
                for (int i = 0; i < numLabels; i++) {
                    if (format.attribute(labelIndices[i]).value(0).equals("0"))
                        temp[labelIndices[i]] = tempLabels[i];
                    else
                        temp[labelIndices[i]] = 1 - tempLabels[i];
                }
                Instance newInstance = DataUtils.createInstance(tempInstance, 1, temp);
                newInstances.add(newInstance);
            }
        }
        System.out.println("finished");
        return newInstances;
    default:
        return null;
    }
}

From source file:mulan.regressor.transformation.RegressorChainSimple.java

License:Open Source License

protected MultiLabelOutput makePredictionInternal(Instance instance) throws Exception {
    double[] scores = new double[numLabels];

    // create a new temporary instance so that the passed instance is not altered
    Instances dataset = instance.dataset();
    Instance tempInstance = DataUtils.createInstance(instance, instance.weight(), instance.toDoubleArray());

    for (int counter = 0; counter < numLabels; counter++) {
        dataset.setClassIndex(chain[counter]);
        tempInstance.setDataset(dataset);
        // find the appropriate position for that score in the scores array
        // i.e. which is the corresponding target
        int pos = 0;
        for (int i = 0; i < numLabels; i++) {
            if (chain[counter] == labelIndices[i]) {
                pos = i;//from   www .ja  v a2 s .  c om
                break;
            }
        }
        scores[pos] = chainRegressors[counter].classifyInstance(tempInstance);
        tempInstance.setValue(chain[counter], scores[pos]);
    }

    MultiLabelOutput mlo = new MultiLabelOutput(scores, true);
    return mlo;
}