Example usage for weka.core Instance setValue

List of usage examples for weka.core Instance setValue

Introduction

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

Prototype

public void setValue(Attribute att, String value);

Source Link

Document

Sets a value of an nominal or string attribute to the given value.

Usage

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

License:Open Source License

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

    Instance newInstance = pt6Trans.transformInstance(instance);
    //calculate confidences
    //debug(instance.toString());
    for (int i = 0; i < numLabels; i++) {
        newInstance.setDataset(transformed);
        newInstance.setValue(newInstance.numAttributes() - 2,
                instance.dataset().attribute(labelIndices[i]).name());
        //debug(newInstance.toString());
        double[] temp = baseClassifier.distributionForInstance(newInstance);
        //debug(temp.toString());
        confidences[i] = temp[transformed.classAttribute().indexOfValue("1")];
        //debug("" + confidences[i]);
        bipartition[i] = temp[transformed.classAttribute().indexOfValue("1")] >= temp[transformed
                .classAttribute().indexOfValue("0")] ? true : false;
        //debug("" + bipartition[i]);
    }/*  ww  w .  ja v a  2s  .co  m*/

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

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

License:Open Source License

/**
 * Attaches an index attribute at the beginning of each instance
 *
 * @param original//  www .  j av a2  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;
}

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

License:Open Source License

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

    //Generate the chain: Test the same dataset
    MultiLabelInstances tempTrainingSet = GenerateChain(trainingSet);

    labelIndices = tempTrainingSet.getLabelIndices();
    featureIndices = tempTrainingSet.getFeatureIndices();

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

    Instances trainingData = tempTrainingSet.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);
            // 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;
                if (trainingData.instance(i) instanceof SparseInstance) {
                    tempInstance = new SparseInstance(trainingData.instance(i));
                } else {
                    tempInstance = new DenseInstance(trainingData.instance(i));
                }/*from www  . j ava2s  .c  om*/

                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];

            System.arraycopy(featureIndices, 0, reorderedIndices, 0, numPredictors);
            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.TwoStageClassifierChainArchitecture.java

License:Open Source License

private Instance modifyDenseInstance(Instance instance, double[] confidences) {
    Instance modifiedIns = new DenseInstance(instance);
    for (int i = confidences.length - 1; i >= 0; i--) {
        modifiedIns.insertAttributeAt(0);
        modifiedIns.setValue(0, confidences[i]);
    }/*  w  w w .  j  av a2s .c o m*/
    return modifiedIns;
}

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

License:Open Source License

@Override
protected void buildInternal(MultiLabelInstances trainingSet) throws Exception {
    // Virtual label models
    debug("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];

    ArrayList<MultiLabelOutput> predictions;
    predictions = predictLabels(trainingSet);

    int counter = 0;
    // Creation of one-vs-one models
    for (int label1 = 0; label1 < numLabels - 1; label1++) {
        for (int label2 = label1 + 1; label2 < numLabels; label2++) {
            //Generate the chain: Test the same dataset
            MultiLabelInstances tempTrainingSet = GenerateChain(trainingSet, label1, label2, predictions);

            Instances trainingData = tempTrainingSet.getDataSet();

            labelIndices = tempTrainingSet.getLabelIndices();
            featureIndices = tempTrainingSet.getFeatureIndices();

            // Attribute of label 1
            Attribute attrLabel1 = trainingData.attribute(labelIndices[label1]);

            debug("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;
                if (trainingData.instance(i) instanceof SparseInstance) {
                    tempInstance = new SparseInstance(trainingData.instance(i));
                } else {
                    tempInstance = new DenseInstance(trainingData.instance(i));
                }//from  w w  w.jav a2s  .c  o  m

                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];
            System.arraycopy(featureIndices, 0, reorderedIndices, 0, numPredictors);
            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.TwoStagePrunedClassifierChainArchitecture.java

License:Open Source License

public Instance modifyDenseInstance(Instance ins, double value1, double value2) {
    Instance modifiedIns = new DenseInstance(ins);
    modifiedIns.insertAttributeAt(0);//from  ww w  .j  av  a2  s. c  o  m
    modifiedIns.setValue(0, value1);
    modifiedIns.insertAttributeAt(0);
    modifiedIns.setValue(0, value2);
    return modifiedIns;
}

From source file:mulan.data.ConverterLibSVM.java

License:Open Source License

/**
 * Converts a multi-label dataset from LibSVM format to the format
 * that is compatible with Mulan. It constructs one ARFF and one XML file. 
 *
 * @param path the directory that contains the source file and will contain 
 * the target files// w  ww . ja  v a2 s . c  om
 * @param sourceFilename the name of the source file
 * @param relationName the relation name of the arff file that will be 
 * constructed
 * @param targetFilestem the filestem for the target files (.arff and .xml)
 */
public static void convertFromLibSVM(String path, String sourceFilename, String targetFilestem,
        String relationName) {
    BufferedReader aReader = null;
    BufferedWriter aWriter = null;

    int numLabels = 0;
    int numAttributes = 0;
    int numInstances = 0;
    double meanParsedAttributes = 0;

    // Calculate number of labels and attributes

    String Line = null;
    try {
        aReader = new BufferedReader(new FileReader(path + sourceFilename));

        while ((Line = aReader.readLine()) != null) {
            numInstances++;

            StringTokenizer strTok = new StringTokenizer(Line, " ");
            while (strTok.hasMoreTokens()) {
                String token = strTok.nextToken();

                if (token.indexOf(":") == -1) {
                    // parse label info
                    StringTokenizer labelTok = new StringTokenizer(token, ",");
                    while (labelTok.hasMoreTokens()) {
                        String strLabel = labelTok.nextToken();
                        int intLabel = Integer.parseInt(strLabel);
                        if (intLabel > numLabels) {
                            numLabels = intLabel;
                        }
                    }
                } else {
                    // parse attribute info
                    meanParsedAttributes++;
                    StringTokenizer attrTok = new StringTokenizer(token, ":");
                    String strAttrIndex = attrTok.nextToken();
                    int intAttrIndex = Integer.parseInt(strAttrIndex);
                    if (intAttrIndex > numAttributes) {
                        numAttributes = intAttrIndex;
                    }
                }
            }
        }

        numLabels++;

        System.out.println("Number of attributes: " + numAttributes);
        System.out.println("Number of instances: " + numInstances);
        System.out.println("Number of classes: " + numLabels);

        System.out.println("Constructing XML file... ");
        LabelsMetaDataImpl meta = new LabelsMetaDataImpl();
        for (int label = 0; label < numLabels; label++) {
            meta.addRootNode(new LabelNodeImpl("Label" + (label + 1)));
        }

        String labelsFilePath = path + targetFilestem + ".xml";
        try {
            LabelsBuilder.dumpLabels(meta, labelsFilePath);
            System.out.println("Done!");
        } catch (LabelsBuilderException e) {
            File labelsFile = new File(labelsFilePath);
            if (labelsFile.exists()) {
                labelsFile.delete();
            }
            System.out.println("Construction of labels XML failed!");
        }

        meanParsedAttributes /= numInstances;
        boolean Sparse = false;
        if (meanParsedAttributes < numAttributes) {
            Sparse = true;
            System.out.println("Dataset is sparse.");
        }

        // Define Instances class to hold data
        ArrayList<Attribute> attInfo = new ArrayList<Attribute>(numAttributes + numLabels);
        Attribute[] att = new Attribute[numAttributes + numLabels];

        for (int i = 0; i < numAttributes; i++) {
            att[i] = new Attribute("Att" + (i + 1));
            attInfo.add(att[i]);
        }
        ArrayList<String> ClassValues = new ArrayList<String>(2);
        ClassValues.add("0");
        ClassValues.add("1");
        for (int i = 0; i < numLabels; i++) {
            att[numAttributes + i] = new Attribute("Label" + (i + 1), ClassValues);
            attInfo.add(att[numAttributes + i]);
        }

        // Re-read file and convert into multi-label arff
        int countInstances = 0;

        aWriter = new BufferedWriter(new FileWriter(path + targetFilestem + ".arff"));
        Instances data = new Instances(relationName, attInfo, 0);
        aWriter.write(data.toString());

        aReader = new BufferedReader(new FileReader(path + sourceFilename));

        while ((Line = aReader.readLine()) != null) {
            countInstances++;

            // set all  values to 0
            double[] attValues = new double[numAttributes + numLabels];
            Arrays.fill(attValues, 0);

            Instance tempInstance = new DenseInstance(1, attValues);
            tempInstance.setDataset(data);

            // separate class info from attribute info
            // ensure class info exists
            StringTokenizer strTok = new StringTokenizer(Line, " ");

            while (strTok.hasMoreTokens()) {
                String token = strTok.nextToken();

                if (token.indexOf(":") == -1) {
                    // parse label info
                    StringTokenizer labelTok = new StringTokenizer(token, ",");
                    while (labelTok.hasMoreTokens()) {
                        String strLabel = labelTok.nextToken();
                        int intLabel = Integer.parseInt(strLabel);
                        tempInstance.setValue(numAttributes + intLabel, 1);
                    }
                } else {
                    // parse attribute info
                    StringTokenizer AttrTok = new StringTokenizer(token, ":");
                    String strAttrIndex = AttrTok.nextToken();
                    String strAttrValue = AttrTok.nextToken();
                    tempInstance.setValue(Integer.parseInt(strAttrIndex) - 1, Double.parseDouble(strAttrValue));
                }
            }

            if (Sparse) {
                SparseInstance tempSparseInstance = new SparseInstance(tempInstance);
                aWriter.write(tempSparseInstance.toString() + "\n");
            } else {
                aWriter.write(tempInstance.toString() + "\n");
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (aReader != null) {
                aReader.close();
            }
            if (aWriter != null) {
                aWriter.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

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;//  w ww  .ja va2 s. c  o m
                break;
            }
        }
        scores[pos] = chainRegressors[counter].classifyInstance(tempInstance);
        tempInstance.setValue(chain[counter], scores[pos]);
    }

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

From source file:mulan.transformations.IncludeLabelsTransformation.java

License:Open Source License

/**
 *
 * @param mlData multi-label data/*from   w ww.j a  va  2s.  c  o  m*/
 * @return transformed instances
 * @throws Exception Potential exception thrown. To be handled in an upper level.
 */
public Instances transformInstances(MultiLabelInstances mlData) throws Exception {
    int numLabels = mlData.getNumLabels();
    labelIndices = mlData.getLabelIndices();

    // remove all labels
    Instances transformed = RemoveAllLabels.transformInstances(mlData);

    // add at the end an attribute with values the label names
    ArrayList<String> labelNames = new ArrayList<String>(numLabels);
    for (int counter = 0; counter < numLabels; counter++) {
        labelNames.add(mlData.getDataSet().attribute(labelIndices[counter]).name());
    }
    Attribute attrLabel = new Attribute("Label", labelNames);
    transformed.insertAttributeAt(attrLabel, transformed.numAttributes());

    // and at the end a binary attribute
    ArrayList<String> binaryValues = new ArrayList<String>(2);
    binaryValues.add("0");
    binaryValues.add("1");
    Attribute classAttr = new Attribute("Class", binaryValues);
    transformed.insertAttributeAt(classAttr, transformed.numAttributes());

    // add instances
    transformed = new Instances(transformed, 0);
    transformed.setClassIndex(transformed.numAttributes() - 1);
    Instances data = mlData.getDataSet();
    for (int instanceIndex = 0; instanceIndex < data.numInstances(); instanceIndex++) {
        for (int labelCounter = 0; labelCounter < numLabels; labelCounter++) {
            Instance temp;
            temp = RemoveAllLabels.transformInstance(data.instance(instanceIndex), labelIndices);
            temp.setDataset(null);
            temp.insertAttributeAt(temp.numAttributes());
            temp.insertAttributeAt(temp.numAttributes());
            temp.setDataset(transformed);
            temp.setValue(temp.numAttributes() - 2, (String) labelNames.get(labelCounter));
            if (data.attribute(labelIndices[labelCounter])
                    .value((int) data.instance(instanceIndex).value(labelIndices[labelCounter])).equals("1")) {
                temp.setValue(temp.numAttributes() - 1, "1");
            } else {
                temp.setValue(temp.numAttributes() - 1, "0");
            }
            transformed.add(temp);
        }
    }

    return transformed;
}

From source file:mulan.transformations.multiclass.Copy.java

License:Open Source License

/**
 * Transforms a multi-label instance to a list of single-label instances,
 * one for each of the labels that annotate the instance, by copying the
 * feature vector//w  w  w  .j  a  v  a  2  s. c om
 *
 * @param instance a multi-label instance
 * @return a list with the transformed single-label instances
 */
List<Instance> transformInstance(Instance instance) {
    List<Instance> result = new ArrayList<Instance>();
    for (int counter = 0; counter < numOfLabels; counter++) {
        if (instance.attribute(labelIndices[counter]).value((int) instance.value(labelIndices[counter]))
                .equals("1")) {
            Instance transformed = null;
            try {
                transformed = RemoveAllLabels.transformInstance(instance, labelIndices);
                transformed.setDataset(null);
                transformed.insertAttributeAt(transformed.numAttributes());
                transformed.setValue(transformed.numAttributes() - 1, counter);
            } catch (Exception ex) {
                Logger.getLogger(Copy.class.getName()).log(Level.SEVERE, null, ex);
            }
            result.add(transformed);
        }
    }
    return result;
}