Example usage for weka.core Instance classIndex

List of usage examples for weka.core Instance classIndex

Introduction

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

Prototype

public int classIndex();

Source Link

Document

Returns the class attribute's index.

Usage

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

License:Open Source License

@Override
public double[] distributionForInstance(Instance x) throws Exception {

    int L = x.classIndex();

    //if there is only one class (as for e.g. in some hier. mtds) predict it
    //if(L == 1) return new double[]{1.0};

    Instance x_sl = PSUtils.convertInstance(x, L, m_InstancesTemplate); // the sl instance
    //x_sl.setDataset(m_InstancesTemplate);                     // where y in {comb_1,comb_2,...,comb_k}

    double w[] = m_Classifier.distributionForInstance(x_sl); // w[j] = p(y_j) for each j = 1,...,L
    int max_j = Utils.maxIndex(w); // j of max w[j]
    //int max_j = (int)m_Classifier.classifyInstance(x_sl);         // where comb_i is selected
    String y_max = m_InstancesTemplate.classAttribute().value(max_j); // comb_i e.g. "0+3+0+0+1+2+0+0"

    double y[] = Arrays.copyOf(A.toDoubleArray(MLUtils.decodeValue(y_max)), L * 2); // "0+3+0+0+1+2+0+0" -> [0.0,3.0,0.0,...,0.0]

    HashMap<Double, Double> votes[] = new HashMap[L];
    for (int j = 0; j < L; j++) {
        votes[j] = new HashMap<Double, Double>();
    }/*from   w  w w .  j  a va  2s.c  o  m*/

    for (int i = 0; i < w.length; i++) {
        double y_i[] = A.toDoubleArray(MLUtils.decodeValue(m_InstancesTemplate.classAttribute().value(i)));
        for (int j = 0; j < y_i.length; j++) {
            votes[j].put(y_i[j], votes[j].containsKey(y_i[j]) ? votes[j].get(y_i[j]) + w[i] : w[i]);
        }
    }

    // some confidence information
    for (int j = 0; j < L; j++) {
        y[j + L] = votes[j].size() > 0 ? Collections.max(votes[j].values()) : 0.0;
    }

    return y;
}

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

License:Open Source License

@Override
public double[] distributionForInstance(Instance x) throws Exception {

    int L = x.classIndex();

    HashMap<Integer, Double> votes[] = new HashMap[L];
    for (int j = 0; j < L; j++) {
        votes[j] = new HashMap<Integer, Double>();
    }//from   www. j  ava2 s.c o m

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

        // Transform instance
        Instance x_m = PSUtils.convertInstance(x, L, m_InstancesTemplates[m]);
        x_m.setDataset(m_InstancesTemplates[m]);

        // Get a meta classification
        int yp_j = (int) m_Classifiers[m].classifyInstance(x_m); // e.g., 2

        int values[] = SuperLabelUtils.decodeValue(m_InstancesTemplates[m].classAttribute().value(yp_j));

        int k_indices[] = SuperLabelUtils.decodeClass(m_InstancesTemplates[m].classAttribute().name());

        // Vote with classification
        for (int j_k = 0; j_k < k_indices.length; j_k++) {
            //int i = k_indices[j_k];         // original indices
            int j = kMap[m][j_k]; // original indices
            Double score = votes[j].get(values[j_k]);
            votes[j].put(values[j_k], (score == null) ? 1. : score + 1.);
        }

    }

    double y[] = SuperLabelUtils.convertVotesToDistributionForInstance(votes);

    return y;
}

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

License:Open Source License

@Override
public double[] distributionForInstance(Instance x) throws Exception {

    //return mt.distributionForInstance(x);
    int L = x.classIndex();
    double y[] = new double[L * 2];

    // Convert (x,y) to (x_,y_)
    int L_ = m_InstancesTemplate.classIndex(); // == L-NUM
    Instance x_ = MLUtils.setTemplate(x, f.getTemplate(), m_InstancesTemplate);

    // Get a classification y_ = h(x_)
    double y_[] = null;
    try {/*from www  .ja v  a2  s  .  c  o  m*/
        y_ = ((ProblemTransformationMethod) m_Classifier).distributionForInstance(x_);
    } catch (Exception e) {
        System.err.println("EXCEPTION !!! setting to " + Arrays.toString(y_));
        return y;
        //e.printStackTrace();
        //System.exit(1);
    }

    // For each super node ...
    for (int j = 0; j < L_; j++) {

        int idxs[] = SuperNodeFilter.decodeClasses(m_InstancesTemplate.attribute(j).name()); // 3,4   (partition)
        String vals[] = SuperNodeFilter
                .decodeValue(m_InstancesTemplate.attribute(j).value((int) Math.round(y_[j]))); // 1,0   (clases)

        for (int i = 0; i < idxs.length; i++) {
            y[idxs[i]] = x.dataset().attribute(idxs[i]).indexOfValue(vals[i]); // y_j = v
            y[idxs[i] + L] = y_[j + L_]; // P(Y_j = v), hence, MUST be a multi-target classifier
        }
    }

    return y;
}

From source file:meka.core.CCUtils.java

License:Open Source License

/**
 * SetPath - set 'path[]' into the first L attributes of Instance 'xy'.
 * @param   xy      an Example (x,y)//  w w  w. j a  v a2s  .  c  om
 * @param   path   a label vector
 */
public static void setPath(Instance xy, double path[]) {
    int L = xy.classIndex(); // = path.length
    for (int j = 0; j < L; j++) {
        xy.setValue(j, (int) Math.round(path[j])); // x = x + path_j
    }
}

From source file:meka.core.MLUtils.java

License:Open Source License

/**
 * Instance with L labels to double[] of length L, where L = x.classIndex().
 * Rounds to the nearest whole number./*from   w w  w .j a  v a  2 s .  c o m*/
 */
public static final double[] toDoubleArray(Instance x) {
    int L = x.classIndex();
    return toDoubleArray(x, L);
}

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  w ww .  jav  a2 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)
 *///from www . jav a  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 x to missing.
 *//*from   w  w  w  . java  2  s.c om*/
public static Instance setLabelsMissing(Instance x) {
    return setLabelsMissing(x, x.classIndex());
}

From source file:meka.core.MLUtils.java

License:Open Source License

public static final String toDebugString(Instance x) {
    int L = x.classIndex();
    StringBuilder sb = new StringBuilder();
    sb.append("y = [");
    for (int j = 0; j < L; j++) {
        sb.append(x.value(j) + " ");
    }/*from  www . ja  v a  2  s.  com*/
    sb.append("], x = [");
    for (int j = L; j < L + 10; j++) {
        sb.append(x.value(j) + " ");
    }
    sb.append(" ... ]");
    return sb.toString();
}

From source file:meka.core.MLUtils.java

License:Open Source License

/** Clear Labels -- set the value of all label attributes to 0.0 */
public static void clearLabels(Instance x) {
    int L = x.classIndex();
    for (int j = 0; j < L; j++)
        x.setValue(j, 0.0);//w w  w .ja  va2  s. c  o m
}