Example usage for weka.core Instance setClassValue

List of usage examples for weka.core Instance setClassValue

Introduction

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

Prototype

public void setClassValue(String value);

Source Link

Document

Sets the class value of an instance to the given value.

Usage

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  ww .java  2  s . co m
     * 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.transformation.MultiLabelStacking.java

License:Open Source License

/**
 * Builds the ensemble of meta-level classifiers.
 *
 * @throws Exception/*  w  w w .  j a  v  a2 s  . c o  m*/
 */
public void buildMetaLevel() throws Exception {
    debug("Building the ensemle of the meta level classifiers");

    for (int i = 0; i < numLabels; i++) { // creating meta-level data new
        ArrayList<Attribute> attributes = new ArrayList<Attribute>();

        if (includeAttrs) {// create an ArrayList with numAttributes size
            for (int j = 0; j < train.numAttributes(); j++) {
                attributes.add(train.attribute(j));
            }
        } else {// create a FastVector with numLabels size
            for (int j = 0; j < numLabels; j++) {
                attributes.add(train.attribute(labelIndices[j]));
            }
        }
        attributes.add(train.attribute(labelIndices[i]).copy("meta"));

        metaLevelData[i] = new Instances("Meta format", attributes, 0);
        metaLevelData[i].setClassIndex(metaLevelData[i].numAttributes() - 1);

        // add the meta instances new
        for (int l = 0; l < train.numInstances(); l++) {
            double[] values = new double[metaLevelData[i].numAttributes()];
            if (includeAttrs) {
                // Copy the original features
                for (int m = 0; m < featureIndices.length; m++) {
                    values[m] = train.instance(l).value(featureIndices[m]);
                }
                // Copy the label confidences as additional features
                for (int m = 0; m < numLabels; m++) {
                    values[train.numAttributes() - numLabels + m] = baseLevelPredictions[l][m];
                }
            } else {
                for (int m = 0; m < numLabels; m++) {
                    values[m] = baseLevelPredictions[l][m];
                }
            }

            values[values.length - 1] = Double.parseDouble(
                    train.attribute(labelIndices[i]).value((int) train.instance(l).value(labelIndices[i])));
            Instance metaInstance = DataUtils.createInstance(train.instance(l), 1, values);
            metaInstance.setDataset(metaLevelData[i]);
            if (values[values.length - 1] > 0.5) {
                metaInstance.setClassValue("1");
            } else {
                metaInstance.setClassValue("0");
            }
            metaLevelData[i].add(metaInstance);
        }

        // We utilize a filtered classifier to prune uncorrelated labels
        metaLevelFilteredEnsemble[i] = new FilteredClassifier();
        metaLevelFilteredEnsemble[i].setClassifier(metaLevelEnsemble[i]);

        Remove remove = new Remove();

        if (topkCorrelated < numLabels) {
            remove.setAttributeIndicesArray(selectedAttributes[i]);
        } else {
            remove.setAttributeIndices("first-last");
        }

        remove.setInvertSelection(true);
        remove.setInputFormat(metaLevelData[i]);
        metaLevelFilteredEnsemble[i].setFilter(remove);

        debug("Building classifier for meta training set" + i);
        metaLevelFilteredEnsemble[i].buildClassifier(metaLevelData[i]);
        metaLevelData[i].delete();
    }
}

From source file:mulan.transformations.PairwiseTransformation.java

License:Open Source License

/**
 * Prepares the training data for two labels. 
 *
 * @param label1 first label/*from  w w w.ja  va2 s  .com*/
 * @param label2 second label
 * @return transformed Instances object
 */
public Instances transformInstances(int label1, int label2) {
    Instances transformed = new Instances(shell, 0);
    int[] labelIndices = data.getLabelIndices();

    int indexOfTrueLabel1;
    if (data.getDataSet().attribute(labelIndices[label1]).value(0).equals("1")) {
        indexOfTrueLabel1 = 0;
    } else {
        indexOfTrueLabel1 = 1;
    }
    int indexOfTrueLabel2;
    if (data.getDataSet().attribute(labelIndices[label2]).value(0).equals("1")) {
        indexOfTrueLabel2 = 0;
    } else {
        indexOfTrueLabel2 = 1;
    }

    for (int j = 0; j < shell.numInstances(); j++) {
        boolean value1 = ((int) data.getDataSet().instance(j).value(labelIndices[label1]) == indexOfTrueLabel1);
        boolean value2 = ((int) data.getDataSet().instance(j).value(labelIndices[label2]) == indexOfTrueLabel2);
        if (value1 != value2) {
            Instance tempInstance;
            if (shell.instance(j) instanceof SparseInstance) {
                tempInstance = new SparseInstance(shell.instance(j));
            } else {
                tempInstance = new DenseInstance(shell.instance(j));
            }
            tempInstance.setDataset(transformed);
            if (value1 == true) {
                tempInstance.setClassValue(1);
            } else {
                tempInstance.setClassValue(0);
            }
            transformed.add(tempInstance);
        }
    }
    return transformed;
}

From source file:myclassifier.wekaCode.java

public static void classifyUnseenData(String[] attributes, Classifier classifiers, Instances data)
        throws Exception {
    Instance newInstance = new Instance(data.numAttributes());
    newInstance.setDataset(data);/*  w  ww .  j a v  a2  s .  co  m*/
    for (int i = 0; i < data.numAttributes() - 1; i++) {
        if (Attribute.NUMERIC == data.attribute(i).type()) {
            Double value = Double.valueOf(attributes[i]);
            newInstance.setValue(i, value);
        } else {
            newInstance.setValue(i, attributes[i]);
        }
    }

    double clsLabel = classifiers.classifyInstance(newInstance);
    newInstance.setClassValue(clsLabel);

    String result = data.classAttribute().value((int) clsLabel);

    System.out.println("Hasil Classify Unseen Data Adalah: " + result);
}

From source file:myclusterer.WekaCode.java

public static void classifyUnseenData(String[] attributes, Clusterer clusterer, Instances data)
        throws Exception {
    Instance newInstance = new Instance(data.numAttributes());
    newInstance.setDataset(data);/*  www .  j  a  v a 2s  .c  o m*/
    for (int i = 0; i < data.numAttributes() - 1; i++) {
        if (Attribute.NUMERIC == data.attribute(i).type()) {
            Double value = Double.valueOf(attributes[i]);
            newInstance.setValue(i, value);
        } else {
            newInstance.setValue(i, attributes[i]);
        }
    }

    double clsLabel = clusterer.clusterInstance(newInstance);
    newInstance.setClassValue(clsLabel);

    String result = data.classAttribute().value((int) clsLabel);

    System.out.println("Hasil Classify Unseen Data Adalah: " + result);
}

From source file:net.sf.jclal.activelearning.oracle.ConsoleHumanOracle.java

License:Open Source License

/**
 * Method for the specific case of a single-label dataset.
 *
 * @param queryStrategy The query strategy to use
 *///from   w  w  w.j a  v a2 s.  c o m
private void labelSingleLabelInstances(IQueryStrategy queryStrategy) {

    //Object to read from the console
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    ArrayList<Integer> selected = ((AbstractQueryStrategy) queryStrategy).getSelectedInstances();

    //In the labeled dataset must be defined all the possible classes
    Instances labeled = queryStrategy.getLabelledData().getDataset();

    String[] valueClass = valueClasses(labeled);

    //For each selected instance
    for (int i : selected) {

        //Ask to the oracle about the class of the instance
        Instance instance = queryStrategy.getUnlabelledData().instance(i);

        System.out.println("\n What is the class of this instance?");

        System.out.println("Instance:" + instance.toString() + "\n");

        int classSelected = 0;

        do {

            System.out.println("Type a number in the range. There are the possible classes:");

            for (int index = 0; index < valueClass.length; index++) {
                System.out.println(index + ": " + valueClass[index]);
            }

            classSelected = scanner.nextInt();

        } while (classSelected >= valueClass.length || classSelected < 0);

        instance.setClassValue(classSelected);
        System.out.println();
    }

}

From source file:net.sf.jclal.activelearning.singlelabel.querystrategy.ExpectedCeroOneLossQueryStrategy.java

License:Open Source License

/**
 * Returns the expected cero one loss, the lower the value the greater the information
 *
 * @param instanceToAdd The index of the instance to add
 * @param classValue The class value//from  ww  w .  ja v  a2  s  . c om
 * @return The expected cero one loss
 */
public double expectedCeroOneLoss(int instanceToAdd, int classValue) {

    double sum = 0;

    try {
        //Make a copy of the labeled and unlabeled sets
        IDataset labeledCopy = new WekaDataset(getLabelledData());

        IDataset unlabeledCopy = new WekaDataset(getUnlabelledData());

        //the class settles down according to the classifier
        Instance copy = unlabeledCopy.instance(instanceToAdd);

        //The current instance is removed from unlabeled set
        unlabeledCopy.remove(instanceToAdd);

        copy.setClassValue(classValue);

        //The current instance is added to labeled set
        labeledCopy.add(copy);

        //it trains the classifier with the new labeled set
        IClassifier clasificadorTemp = getClassifier().makeCopy();
        clasificadorTemp.buildClassifier(labeledCopy);

        Iterator<Instance> instanceIterator = unlabeledCopy.getDataset().iterator();

        Instance current;
        while (instanceIterator.hasNext()) {

            current = instanceIterator.next();

            double[] probabilities;
            probabilities = clasificadorTemp.distributionForInstance(current);

            double currConf = probabilities[Utils.maxIndex(probabilities)];

            currConf = 1.0 - currConf;

            sum += currConf;
        }

        labeledCopy.getDataset().clear();
        labeledCopy = null;
        unlabeledCopy.getDataset().clear();
        unlabeledCopy = null;
        instanceIterator = null;
        clasificadorTemp = null;
        copy = null;
        System.gc();

    } catch (Exception e) {
        Logger.getLogger(ExpectedCeroOneLossQueryStrategy.class.getName()).log(Level.SEVERE, null, e);
    }

    return sum;
}

From source file:net.sf.jclal.activelearning.singlelabel.querystrategy.ExpectedLogLossQueryStrategy.java

License:Open Source License

/**
 * Returns the expected log loss, the lower the value the greater the information
 *
 * @param instanceToAdd The instance to add
 * @param classValue The class value//www. j a va2s  . c o m
 * @return the expected log loss
 */
public double expectedLogLoss(int instanceToAdd, int classValue) {

    double sum = 0;

    try {
        //Make a copy of the labeled and unlabeled sets
        IDataset labeledCopy = new WekaDataset(getLabelledData());

        IDataset unlabeledCopy = new WekaDataset(getUnlabelledData());

        //the class settles down according to the classifier
        Instance copy = unlabeledCopy.instance(instanceToAdd);

        unlabeledCopy.remove(instanceToAdd);

        copy.setClassValue(classValue);

        labeledCopy.add(copy);

        //To train the classifier with the new labeled set
        IClassifier clasificadorTemp = getClassifier().makeCopy();
        clasificadorTemp.buildClassifier(labeledCopy);

        Iterator<Instance> instanceIterator = unlabeledCopy.getDataset().iterator();

        Instance current;
        while (instanceIterator.hasNext()) {

            current = instanceIterator.next();

            double[] probabilities = clasificadorTemp.distributionForInstance(current);

            for (int i = 0; i < probabilities.length; i++) {

                if (probabilities[i] != 0) {

                    double tempValue = probabilities[i] * logbase2(probabilities[i]);

                    sum += tempValue;
                }
            }
        }

        labeledCopy.getDataset().clear();
        labeledCopy = null;
        unlabeledCopy.getDataset().clear();
        unlabeledCopy = null;
        instanceIterator = null;
        clasificadorTemp = null;
        copy = null;
        System.gc();

    } catch (Exception e) {
        Logger.getLogger(ExpectedLogLossQueryStrategy.class.getName()).log(Level.SEVERE, null, e);
    }

    return -sum;
}

From source file:Neural_Network.NuralN.java

public static ArrayList<Double> predictDisease(String[] instanceData) {

    ArrayList<Double> predictions = new ArrayList<>();
    if (!trained) {
        System.err.println("Neural netowrk is not trained....");
    } else {// w w  w . j  ava2  s.c  o m
        Instance temp = toInstance(instanceData);
        try {
            temp.setClassValue(nN.classifyInstance(temp));
            for (double d : nN.distributionForInstance(temp)) {
                // classify all the instance in array
                predictions.add(d);
            } // giving a class value to the instance of teh image 
              // listing all the index
            predictions.add(temp.classValue());
            // adding the closes value to last with its class value
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
    return predictions;
}

From source file:ocr.ARFFSymbolFilter.java

License:Apache License

public static void writeWeka(final String filenameout, final ArrayList<?> symbolData) {
    final int nsold = ARFFSymbolFilter.ns;
    ARFFSymbolFilter.tangent = (ARFFSymbolFilter.times > 1);
    try {//w  w w .j  a v a 2 s .co  m
        if (!ARFFSymbolFilter.strokenumber) {
            ARFFSymbolFilter.ns = 1;
        }
        final DataOutputStream[] fileout = new DataOutputStream[ARFFSymbolFilter.ns];
        final Instances[] instances = new Instances[ARFFSymbolFilter.ns];
        System.out.println("Writing file");
        for (int i = 0; i < ARFFSymbolFilter.ns; ++i) {
            final int k = ARFFSymbolFilter.strokenumber ? i : (nsold - 1);
            fileout[ARFFSymbolFilter.strokenumber ? i : 0] = new DataOutputStream(new FileOutputStream(
                    filenameout + (ARFFSymbolFilter.strokenumber ? ("" + (k + 1)) : "") + ".arff#"));
        }
        final int tot = symbolData.size();
        for (int j = 0; j < symbolData.size(); ++j) {
            final ArrayList<?> group = (ArrayList<?>) symbolData.get(j);
            for (int i = 0; i < group.size(); ++i) {
                final Symbol sym = (Symbol) group.get(i);
                final int k = ARFFSymbolFilter.strokenumber ? (sym.size() - 1) : 0;
                if (sym.name.equals("no_name") || sym.name.equals("empty_symbol")) {
                    System.out.print("#" + sym.name + "#");
                } else {
                    for (int t = 0; t < ARFFSymbolFilter.times; ++t) {
                        final String line = constructStringInstance(sym, ARFFSymbolFilter.alpha);
                        if (line == null) {
                            System.out.print("line=null!");
                        } else {
                            if (instances[k] == null) {
                                final StringTokenizer st = new StringTokenizer(line, " ");
                                final int nt = st.countTokens() / 2;
                                final FastVector att = new FastVector();
                                for (int kk = 0; kk < nt; ++kk) {
                                    final String token = st.nextToken();
                                    att.addElement(new Attribute(new String(token)));
                                    st.nextToken();
                                }
                                att.addElement(new Attribute("class", (FastVector) null));
                                (instances[k] = new Instances("Symbols of Size " + (k + 1), att, 1))
                                        .setClassIndex(att.size() - 1);
                            }
                            final StringTokenizer st = new StringTokenizer(line, " ");
                            final int nt = st.countTokens() / 2;
                            final Instance inst = new Instance(nt + 1);
                            for (int kk = 0; kk < nt; ++kk) {
                                st.nextToken();
                                final String token = new String(st.nextToken());
                                inst.setValue(kk, Double.parseDouble(token));
                            }
                            inst.setDataset(instances[k]);
                            inst.setClassValue(oldReplace(sym.name, "\\", ""));
                            instances[k].add(inst);
                        }
                    }
                }
            }
            if ((int) (100.0 * j) / tot % 10 == 0) {
                System.out.print((int) (100.0 * j) / tot + "%-");
            }
        }
        for (int k = 0; k < ARFFSymbolFilter.ns; ++k) {
            if (fileout[ARFFSymbolFilter.strokenumber ? k : 0] == null) {
                System.out.println("fo" + fileout[ARFFSymbolFilter.strokenumber ? k : 0]);
            }
            if (instances[ARFFSymbolFilter.strokenumber ? k : 0] == null) {
                System.out.println("in:" + instances[ARFFSymbolFilter.strokenumber ? k : 0]);
            }
            fileout[ARFFSymbolFilter.strokenumber ? k : 0]
                    .writeBytes(instances[ARFFSymbolFilter.strokenumber ? k : 0].toString());
            fileout[ARFFSymbolFilter.strokenumber ? k : 0].close();
        }
        final StringToNominal filter = new StringToNominal();
        final String[] args = new String[4];
        for (int k = 0; k < ARFFSymbolFilter.ns; ++k) {
            args[0] = "-i";
            args[1] = filenameout + (ARFFSymbolFilter.strokenumber ? ("" + (k + 1)) : "") + ".arff#";
            args[2] = "-o";
            args[3] = filenameout + (ARFFSymbolFilter.strokenumber ? ("" + (k + 1)) : "") + ".arff";
            Filter.filterFile(filter, args);
            new File(args[1]).delete();
        }
        System.out.println("100.0%");
    } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    } catch (Exception ioe) {
        ioe.printStackTrace();
    }
}