Example usage for weka.core Instances setClassIndex

List of usage examples for weka.core Instances setClassIndex

Introduction

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

Prototype

public void setClassIndex(int classIndex) 

Source Link

Document

Sets the class index of the set.

Usage

From source file:meka.classifiers.multilabel.PLST.java

License:Open Source License

/**
 * Transforms the instance in the prediction process before given to the internal multi-label
 * or multi-target classifier. The instance is passed having the original set of labels, these
 * must be replaced with the transformed labels (attributes) so that the internla classifier
 * can predict them.// www  .  j a v a  2  s. co m
 *
 * @param x The instance to transform. Consists of features and labels.
 * @return The transformed instance. Consists of features and transformed labels.
 */
@Override
public Instance transformInstance(Instance x) throws Exception {
    Instances tmpInst = new Instances(x.dataset());

    tmpInst.delete();
    tmpInst.add(x);

    Instances features = this.extractPart(tmpInst, false);

    Instances labels = new Instances(this.m_PatternInstances);

    labels.add(new DenseInstance(labels.numAttributes()));

    Instances result = Instances.mergeInstances(labels, features);

    result.setClassIndex(labels.numAttributes());

    return result.instance(0);
}

From source file:meka.classifiers.multitarget.NSR.java

License:Open Source License

public Instances convertInstances(Instances D, int L) throws Exception {

    //Gather combinations
    HashMap<String, Integer> distinctCombinations = MLUtils.classCombinationCounts(D);
    if (getDebug())
        System.out.println("Found " + distinctCombinations.size() + " unique combinations");

    //Prune combinations
    MLUtils.pruneCountHashMap(distinctCombinations, m_P);
    if (getDebug())
        System.out.println("Pruned to " + distinctCombinations.size() + " with P=" + m_P);

    // Remove all class attributes
    Instances D_ = MLUtils.deleteAttributesAt(new Instances(D), MLUtils.gen_indices(L));
    // Add a new class attribute
    D_.insertAttributeAt(new Attribute("CLASS", new ArrayList(distinctCombinations.keySet())), 0); // create the class attribute
    D_.setClassIndex(0);

    //Add class values
    for (int i = 0; i < D.numInstances(); i++) {
        String y = MLUtils.encodeValue(MLUtils.toIntArray(D.instance(i), L));
        // add it
        if (distinctCombinations.containsKey(y)) //if its class value exists
            D_.instance(i).setClassValue(y);
        // decomp
        else if (m_N > 0) {
            String d_subsets[] = SuperLabelUtils.getTopNSubsets(y, distinctCombinations, m_N);
            for (String s : d_subsets) {
                int w = distinctCombinations.get(s);
                Instance copy = (Instance) (D_.instance(i)).copy();
                copy.setClassValue(s);//from w w w .  ja  v a2  s.c  om
                copy.setWeight(1.0 / d_subsets.length);
                D_.add(copy);
            }
        }
    }

    // remove with missing class
    D_.deleteWithMissingClass();

    // keep the header of new dataset for classification
    m_InstancesTemplate = new Instances(D_, 0);

    if (getDebug())
        System.out.println("" + D_);

    return D_;
}

From source file:meka.classifiers.multitarget.RAkELd.java

License:Open Source License

@Override
public void buildClassifier(Instances D) throws Exception {
    /*/*from   www.  ja  va2  s.  c o m*/
    This is a slow way of doing things at the moment, making use of multitarget.SCC functionality,
    even though multilabel.RAkELd is not a meta multi-label classifier.
     */

    int L = D.classIndex();
    int N = D.numInstances();
    Random r = new Random(m_S);

    // Note: a slightly round-about way of doing it:
    int num = (int) Math.ceil(L / m_K);
    kMap = SuperLabelUtils.generatePartition(A.make_sequence(L), num, r, true);
    m_M = kMap.length;
    vMap = new int[m_M][][];
    m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_M);
    m_InstancesTemplates = new Instances[m_M];

    if (getDebug())
        System.out.println("Building " + m_M + " models of " + m_K + " partitions:");

    D = SuperLabelUtils.SLTransformation(D, kMap, m_P, m_N);

    for (int i = 0; i < m_M; i++) {

        /*
        if (getDebug()) 
           System.out.println("\tpartitioning model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N);
                
        Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N);
        */

        Instances D_i = F.keepLabels(D, D.classIndex(), new int[] { i });
        D_i.setClassIndex(0);

        //vMap[i] = SuperLabelUtils.extractValues(D_i);

        if (getDebug())
            System.out.println("\tbuilding model " + (i + 1) + "/" + m_M + ": " + Arrays.toString(kMap[i]));

        m_Classifiers[i].buildClassifier(D_i);
        m_InstancesTemplates[i] = new Instances(D_i, 0);

    }

}

From source file:meka.core.CCUtils.java

License:Open Source License

/**
 * LinkTransform - prepare 'D' for training at a node 'j' of the chain, by excluding 'exl'.
 * @param   D      dataset//from w ww  .  j a  va  2 s. c  o m
 * @param   j      index of the label of this node
 * @param   exl      indices of labels which are NOT parents of j
 * @return   the transformed dataset (which can be used as a template)
 */
public static Instances linkTransform(Instances D, int j, int exl[]) {
    Instances D_j = new Instances(D);
    D_j.setClassIndex(-1);
    // delete all the attributes (and track where our index ends up)
    int ndx = j;
    for (int i = exl.length - 1; i >= 0; i--) {
        D_j.deleteAttributeAt(exl[i]);
        if (exl[i] < ndx)
            ndx--;
    }
    D_j.setClassIndex(ndx);
    return D_j;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * ReplaceZasAttributes - data Z[][] will be the new attributes in D.
 * @param   D    dataset (of N instances)
 * @param   Z   attribute space (of N rows, H columns)
 * @param   L   number of classes / labels.
 *//*w ww.  ja  va  2s  . c  o m*/
public static Instances replaceZasAttributes(Instances D, double Z[][], int L) {
    D.setClassIndex(0);
    int m = D.numAttributes() - L;
    for (int j = 0; j < m; j++) {
        D.deleteAttributeAt(L);
    }
    return addZtoD(D, Z, L);
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * ReplaceZasClasses - data Z[][] will be the new class labels in D.
 * @param   D    dataset (of N instances)
 * @param   Z   attribute space (of N rows, H columns)
 * @param   L   column to add Z from in D
 *///from   www .j a v  a  2s . c  o  m
public static Instances replaceZasClasses(Instances D, double Z[][], int L) {

    D.setClassIndex(-1);

    for (int j = 0; j < L; j++) {
        D.deleteAttributeAt(0);
    }
    return insertZintoD(D, Z);
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * InsertZintoD - Insert data Z[][] to Instances D (e.g., as labels).
 * NOTE: Assumes binary labels!/*from ww  w. ja  va2  s.co m*/
 * @see #addZtoD(Instances, double[][], int)
 */
private static Instances insertZintoD(Instances D, double Z[][]) {

    int L = Z[0].length;

    // add attributes
    for (int j = 0; j < L; j++) {
        D.insertAttributeAt(new Attribute("c" + j, Arrays.asList(new String[] { "0", "1" })), j);
    }

    // add values Z[0]...Z[N] to D
    // (note that if D.numInstances() < Z.length, only some are added)
    for (int j = 0; j < L; j++) {
        for (int i = 0; i < D.numInstances(); i++) {
            D.instance(i).setValue(j, Z[i][j] > 0.5 ? 1.0 : 0.0);
        }
    }

    D.setClassIndex(L);
    return D;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * AddZtoD - Add attribute space Z[N][H] (N rows of H columns) to Instances D, which should have N rows also.
 * @param   D    dataset (of N instances)
 * @param   Z   attribute space (of N rows, H columns)
 * @param   L   column to add Z from in D
 *//*from   w  w w .java2s . com*/
private static Instances addZtoD(Instances D, double Z[][], int L) {

    int H = Z[0].length;
    int N = D.numInstances();

    // add attributes
    for (int a = 0; a < H; a++) {
        D.insertAttributeAt(new Attribute("A" + a), L + a);
    }

    // add values Z[0]...Z[N] to D
    for (int a = 0; a < H; a++) {
        for (int i = 0; i < N; i++) {
            D.instance(i).setValue(L + a, Z[i][a]);
        }
    }

    D.setClassIndex(L);
    return D;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * Prepares the class index of the data.
 * /*w  ww .j a va2 s . c  om*/
 * @param data the data to prepare
 * @throws Exception if preparation fails
 */
public static void prepareData(Instances data) throws Exception {
    String doptions[] = null;
    try {
        doptions = MLUtils.getDatasetOptions(data);
    } catch (Exception e) {
        throw new Exception("[Error] Failed to Get Options from @Relation Name", e);
    }

    try {
        int c = (Utils.getOptionPos('C', doptions) >= 0) ? Integer.parseInt(Utils.getOption('C', doptions))
                : Integer.parseInt(Utils.getOption('c', doptions));
        // if negative, then invert
        if (c < 0) {
            c = -c;
            data = F.mulan2meka(data, c);
        }
        // set c
        data.setClassIndex(c);
    } catch (Exception e) {
        throw new Exception(
                "Failed to parse options stored in relation name; expected format for relation name:\n"
                        + "  'name: options'\n" + "But found:\n" + "  '" + data.relationName() + "'\n"
                        + "Format example:\n" + "  'Example_Dataset: -C 3 -split-percentage 50'\n"
                        + "'-C 3' specifies the number of target attributes to be 3. See tutorial for more information.",
                e);
    }
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * For retrieving some dataset statistics on the command line.
 * Note: -L, -d does not work for Mulan format (labels at the end)
 *///from  ww  w  .  j  a v  a2 s . c o  m
public static final void main(String args[]) throws Exception {

    /*
     * If we are given an argument, load a file and extract some info and exit.
     */
    if (args.length > 0) {

        //System.out.println("loading ...");
        Instances D = new Instances(new BufferedReader(new FileReader(args[0])));
        int N = D.numInstances();

        int L = Integer.parseInt(Utils.getOption('C', MLUtils.getDatasetOptions(D)));
        D.setClassIndex(L);

        switch (args[1].charAt(0)) {

        case 'L':
            System.out.println(L); // return the number of labels of D
            break;
        case 'N':
            System.out.println(D.numInstances()); // return the number of Instances of D
            break;
        case 'd':
            System.out.println(D.numAttributes() - L); // reurns the number of (non-label) attributes of D
            break;
        case 'A':
            System.out.println(D.numAttributes()); // returns the number of ALL attributes of D
            break;
        case 'l':
            System.out.println(MLUtils.labelCardinality(D)); // reurns the label cardinalities
            break;
        case 'P':
            System.out.println(Arrays.toString(MLUtils.labelCardinalities(D))); // reurns the label cardinalities
            break;
        case 'C':
            System.out.println(hashMapToString(MLUtils.countCombinations(D, L))); // counts
            break;
        case 'p':
            System.out.println("collecting ...");
            HashMap<LabelSet, Integer> hm = PSUtils.countCombinationsSparse(D, L);
            System.out.println("pruning ...");
            //MLUtils.pruneCountHashMap(hm,1);
            //System.out.println(""+hm);
            System.out.println("writing ...");
            saveObject(hm, "hm-NEW.serialized");
            break;
        default:
            System.out.println(MLUtils.getDatasetName(D)); // returns the name of D
            break;
        }

        return;
    }
    /*
     * Else, just do some tests ...
     */
    else {

        // NEED THIS FOR SOME SCRIPTS
        /*
           String p[] = permute(args[0]);
           int i = 0;
           for(String s: p) {
           System.out.println(""+(i++)+" "+s);
           }
           */
        //System.out.println(""+Arrays.toString(invert(new int[]{1,2},6)));
        //System.out.println(""+Arrays.toString(invert(new int[]{0,2},6)));
        //System.out.println(""+Arrays.toString(invert(new int[]{5,2},6)));
        return;
    }
}