Example usage for weka.core Instances classIndex

List of usage examples for weka.core Instances classIndex

Introduction

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

Prototype


publicint classIndex() 

Source Link

Document

Returns the class attribute's index.

Usage

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * MostCommonCombination -  Most common label combination in D.
 *//*from  w w w .  j a  v  a  2s  . c o m*/
public static final String mostCommonCombination(Instances D) {
    return mostCommonCombination(D, D.classIndex());
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * ClassCombinationCounts - multi-target version of countCombinations(...).
 * NOTE: uses the encodeValue(...) function which does NOT consider sparse data.
 * TODO: use LabelVector instead of Strings
 * @param   D   dataset // w  w w .j  a v  a2s .co m
 * @return   a HashMap where a String representation of each class combination is associated with an Integer count, e.g. [0,2,2,3,2],5
 */
public static final HashMap<String, Integer> classCombinationCounts(Instances D) {
    int L = D.classIndex();
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    for (int i = 0; i < D.numInstances(); i++) {
        String y = encodeValue(toIntArray(D.instance(i), L));
        Integer c = map.get(y);
        map.put(y, c == null ? 1 : c + 1);
    }
    return map;
}

From source file:meka.core.MLUtils.java

License:Open Source License

public static final Instance setTemplate(Instance x, Instances instancesTemplate) {
    int L = x.classIndex();
    int L_t = instancesTemplate.classIndex();
    x = (Instance) x.copy();//from  www.  j ava2 s  .  c  o m
    x.setDataset(null);
    for (int i = L_t; i < L; i++)
        x.deleteAttributeAt(0);
    x.setDataset(instancesTemplate);
    return x;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * SetTemplate - returns a copy of x_template, set with x's attributes, and set to dataset D_template (of which x_template) is a template of this.
 * This function is very useful when Weka throws a strange IndexOutOfBounds exception for setTemplate(x,Template)
 *//*w  ww  .  java  2 s.c  o m*/
public static final Instance setTemplate(Instance x, Instance x_template, Instances D_template) {
    Instance x_ = (Instance) x_template.copy();
    int L_y = x.classIndex();
    int L_z = D_template.classIndex();
    // copy over x space
    MLUtils.copyValues(x_, x, L_y, L_z);
    // set class values to missing
    MLUtils.setLabelsMissing(x_, L_z);
    // set dataset
    x_.setDataset(D_template);
    return x_;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * SetLabelsMissing - Set all labels in D to missing.
 *//*from w w  w .  j  a va2s.c  o  m*/
public static Instances setLabelsMissing(Instances D) {
    int L = D.classIndex();
    for (int i = 0; i < D.numInstances(); i++) {
        for (int j = 0; j < L; j++) {
            D.instance(i).setMissing(j);
        }
    }
    return D;
}

From source file:meka.core.MLUtils.java

License:Open Source License

public static final String toDebugString(Instances D) {
    int L = D.classIndex();
    StringBuilder sb = new StringBuilder();
    sb.append("D=" + D.numInstances());
    sb.append(" L=" + L + " {");
    for (int j = 0; j < L; j++) {
        sb.append(D.attribute(j).name() + " ");
    }/*w w w  . j  a  v  a2  s . c  o  m*/
    sb.append("}");
    return sb.toString();
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * GetXfromD - Extract attributes as a double X[][] from Instances D.
 * TODO: getXfromInstances would be a better name.
 *//*from   ww w.j  av  a  2 s .  c  o  m*/
public static double[][] getXfromD(Instances D) {
    int N = D.numInstances();
    int L = D.classIndex();
    int d = D.numAttributes() - L;
    //System.out.println("d="+d);
    double X[][] = new double[N][d];
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < d; k++) {
            X[i][k] = D.instance(i).value(k + L);
        }
    }
    return X;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * GetXfromD - Extract labels as a double Y[][] from Instances D.
 * TODO: getYfromInstances would be a better name.
 *//*from  w ww  .  j  a  v  a  2  s .com*/
public static double[][] getYfromD(Instances D) {
    int L = D.classIndex();
    int N = D.numInstances();
    double Y[][] = new double[N][L];
    for (int i = 0; i < N; i++) {
        for (int k = 0; k < L; k++) {
            Y[i][k] = D.instance(i).value(k);
        }
    }
    return Y;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * Get K - get the number of values associated with each label L.
 * @param   D    a dataset/*from  www. java 2 s .com*/
 * @return   a vector of size L: K_1,...,K_L
 */
public int[] getK(Instances D) {
    int L = D.classIndex();
    HashSet counts[] = new HashSet[L];
    int K[] = new int[L];
    for (int j = 0; j < L; j++) {
        counts[j] = new HashSet<Integer>();
        for (Instance x : D) {
            int k = (int) x.value(j);
            counts[j].add(k);
        }
        K[j] = counts[j].size();
        /*
           System.out.println(""+j+" = "+counts[j]);
           if (counts[j].size() < 2) {
           System.out.println("OK, this is a problem ...");
        //System.exit(1);
           }
           */
    }
    return K;
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * Attempts to determine the number of classes/class index from the 
 * specified file. In case of ARFF files, only the header will get loaded.
 * //from  w  ww.  j a  va2s  .  com
 * @param file      the file to inspect
 * @return         the class index of the file, Integer.MAX_VALUE in case of error
 */
public static int peekClassIndex(File file) {
    int result;
    DataSource source;
    Instances structure;

    result = Integer.MAX_VALUE;

    try {
        source = new DataSource(file.getAbsolutePath());
        structure = source.getStructure();
        prepareData(structure);
        result = structure.classIndex();
    } catch (Exception e) {
        // ignored
    }

    return result;
}