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.multilabel.CDN.java

License:Open Source License

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

    int L = x.classIndex();
    //ArrayList<double[]> collection = new ArrayList<double[]>(100);

    double y[] = new double[L]; // for collectiing marginal
    int sequence[] = A.make_sequence(L);

    double likelihood[] = new double[L];

    for (int i = 0; i < I; i++) {
        Collections.shuffle(Arrays.asList(sequence));
        for (int j : sequence) {
            // x = [x,y[1],...,y[j-1],y[j+1],...,y[L]]
            x.setDataset(D_templates[j]);
            // q = h_j(x)    i.e. p(y_j | x)

            double dist[] = h[j].distributionForInstance(x);
            int k = A.samplePMF(dist, m_R);
            x.setValue(j, k);/*from  ww  w  .  j  av a  2s . c  o m*/
            likelihood[j] = dist[k];
            // likelihood
            double s = Utils.sum(likelihood);
            // collect  // and where is is good 
            if (i > (I - I_c)) {
                y[j] += x.value(j);
            }
            // else still burning in
        }
    }
    // finish, calculate marginals
    for (int j = 0; j < L; j++) {
        y[j] /= I_c;
    }

    return y;
}

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

License:Open Source License

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

    int L = x.classIndex();
    double y[] = new double[L]; // for sampling
    double y_marg[] = new double[L]; // for collectiing marginal

    int sequence[] = A.make_sequence(L);

    double likelihood[] = new double[L];

    for (int i = 0; i < I; i++) {
        Collections.shuffle(Arrays.asList(sequence));
        for (int j : sequence) {
            // sample
            y[j] = nodes[j].sample(x, y, m_R);
            // collect marginals
            if (i > (I - I_c)) {
                y_marg[j] += y[j];//from w ww .j a v a  2s  . c  o  m
            }
            // else still burning in
        }
    }
    // finish, calculate marginals
    for (int j = 0; j < L; j++) {
        y_marg[j] /= I_c;
    }

    return y_marg;
}

From source file:meka.classifiers.multilabel.incremental.BRUpdateable.java

License:Open Source License

@Override
public void updateClassifier(Instance x) throws Exception {

    int L = x.classIndex();

    if (getDebug())
        System.out.print("-: Updating " + L + " models");

    for (int j = 0; j < L; j++) {
        Instance x_j = (Instance) x.copy();
        x_j.setDataset(null);//from  ww w. j a  v a 2 s  .c om
        x_j = MLUtils.keepAttributesAt(x_j, new int[] { j }, L);
        x_j.setDataset(m_InstancesTemplates[j]);
        ((UpdateableClassifier) m_MultiClassifiers[j]).updateClassifier(x_j);
    }

    if (getDebug())
        System.out.println(":- ");
}

From source file:meka.classifiers.multilabel.incremental.CCUpdateable.java

License:Open Source License

@Override
public double[] distributionForInstance(Instance x) throws Exception {
    int L = x.classIndex();
    root.classify(x);/*  w ww  .ja  v a 2s .  c o m*/
    return MLUtils.toDoubleArray(x, L);
}

From source file:meka.classifiers.multilabel.incremental.MajorityLabelsetUpdateable.java

License:Open Source License

@Override
public void updateClassifier(Instance x) throws Exception {
    int L = x.classIndex();
    super.updateCount(x, L);
}

From source file:meka.classifiers.multilabel.incremental.meta.BaggingMLUpdateableADWIN.java

License:Open Source License

/**
 * DistributionForInstance - And Check for drift by measuring a type of error.
 *///  ww  w.j a  v  a2 s  . c  om
@Override
public double[] distributionForInstance(Instance x) throws Exception {

    // classification

    double y[] = new double[x.classIndex()];

    for (int i = 0; i < m_NumIterations; i++) {
        double y_i[] = m_Classifiers[i].distributionForInstance(x);
        for (int j = 0; j < y_i.length; j++) {
            y[j] += y_i[j];
        }

        accuracies[i] += error(y_i, MLUtils.toDoubleArray(x, y.length));
    }

    for (int j = 0; j < y.length; j++) {
        y[j] = y[j] / m_NumIterations;
    }

    double d = error(y, MLUtils.toDoubleArray(x, y.length));

    // ADWIN stuff

    double ErrEstim = this.adwin.getEstimation();
    if (this.adwin.setInput(1.0 - d))
        if (this.adwin.getEstimation() > ErrEstim) {
            // find worst classifier
            int index = Utils.minIndex(accuracies);
            if (getDebug())
                System.out.println("------- CHANGE DETECTED / Reset Model #" + index + " ------- ");
            // reset this classifier
            m_Classifiers[index] = (ProblemTransformationMethod) AbstractClassifier.makeCopy(m_Classifier);
            m_Classifiers[index].buildClassifier(new Instances(m_InstancesTemplate));
            // ... and reset ADWIN
            this.adwin = new ADWIN();
            accuracies = new double[m_NumIterations];
        }

    return y;
}

From source file:meka.classifiers.multilabel.incremental.PSUpdateable.java

License:Open Source License

@Override
public double[] distributionForInstance(Instance x) throws Exception {
    int L = x.classIndex();
    if (mlu != null) {
        // we're still using the majority-labelset classifier, use it to return the most common combination
        return mlu.distributionForInstance(x);
    } else {//from w w  w.java 2  s  .co m
        // we've built PS already, return a PS prediction!
        return super.distributionForInstance(x);
    }
}

From source file:meka.classifiers.multilabel.incremental.RTUpdateable.java

License:Open Source License

@Override
public void updateClassifier(Instance x) throws Exception {

    int L = x.classIndex();

    for (int j = 0; j < L; j++) {
        if (x.value(j) > 0.0) {
            Instance x_j = convertInstance(x);
            x_j.setClassValue(j);/*from  www  .  j av a2  s  .  com*/
            ((UpdateableClassifier) m_Classifier).updateClassifier(x_j);
        }
    }
}

From source file:meka.classifiers.multilabel.meta.DeepML.java

License:Open Source License

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

    int L = xy.classIndex();

    double z[] = dbm.prob_z(MLUtils.getxfromInstance(xy));

    Instance zy = (Instance) m_InstancesTemplate.firstInstance().copy();

    MLUtils.setValues(zy, z, L);/*from   w  ww .  java 2  s.c o m*/
    zy.setDataset(m_InstancesTemplate);

    return m_Classifier.distributionForInstance(zy);
}

From source file:meka.classifiers.multilabel.meta.MBR.java

License:Open Source License

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

    int c = instance.classIndex();

    double result[] = m_BASE.distributionForInstance(instance);

    instance.setDataset(null);/*w  w  w. java  2 s .  c  o  m*/

    for (int i = 0; i < c; i++) {
        instance.insertAttributeAt(c);
    }

    instance.setDataset(m_InstancesTemplate);

    for (int i = 0; i < c; i++) {
        instance.setValue(c + i, result[i]);
    }

    return m_META.distributionForInstance(instance);
}