Example usage for weka.core Instance setDataset

List of usage examples for weka.core Instance setDataset

Introduction

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

Prototype

public void setDataset(Instances instances);

Source Link

Document

Sets the reference to the dataset.

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 w  ww . j  ava2 s  .  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.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);
        x_j = MLUtils.keepAttributesAt(x_j, new int[] { j }, L);
        x_j.setDataset(m_InstancesTemplates[j]);
        ((UpdateableClassifier) m_MultiClassifiers[j]).updateClassifier(x_j);
    }//from w  w w  .j a  v a2  s  .  com

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

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);/* www .j  a  va2 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);

    for (int i = 0; i < c; i++) {
        instance.insertAttributeAt(c);//from w ww .ja  va 2  s.  c o m
    }

    instance.setDataset(m_InstancesTemplate);

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

    return m_META.distributionForInstance(instance);
}

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

License:Open Source License

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

    int L = x.classIndex();
    double p[] = new double[L];

    for (int i = 0; i < m_NumIterations; i++) {
        // Use a template Instance from training, and copy values over
        // (this is faster than copying x and cutting it to shape)
        Instance x_ = (Instance) m_InstanceTemplates[i];
        MLUtils.copyValues(x_, x, m_IndicesCut[i]);
        x_.setDataset(m_InstancesTemplates[i]);

        // TODO, use generic voting scheme somewhere?
        double d[] = ((ProblemTransformationMethod) m_Classifiers[i]).distributionForInstance(x_);
        for (int j = 0; j < d.length; j++) {
            p[j] += d[j];/* w ww .j  a va2  s  .  co m*/
        }
    }

    return p;
}

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

License:Open Source License

@Override
public double[] distributionForInstance(Instance instance) throws Exception {
    int L = instance.classIndex();
    Instance x = F.meka2mulan((Instance) instance.copy(), L);
    x.setDataset(m_InstancesTemplate);
    double y[] = m_MULAN.makePrediction(x).getConfidences();
    return y;//from www .  ja v  a  2  s .  c o  m
}

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

License:Open Source License

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

    int L = x.classIndex();

    // If there is only one label, predict it
    //if(L == 1) return new double[]{1.0};

    double y[] = new double[L];
    //int c[] = new int[L]; // to scale it between 0 and 1

    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 i_m = (int) m_Classifiers[m].classifyInstance(x_m); // e.g., 2
        int k_indices[] = mapBack(m_InstancesTemplates[m], i_m); // e.g., [3,8]

        // Vote with classification
        for (int i : k_indices) {
            int index = kMap[m][i];
            y[index] += 1.;/*from  w w w.ja  v  a2s . c o m*/
        }

    }

    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>();
    }/*ww w . j av  a 2  s  .  com*/

    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.core.CCUtils.java

License:Open Source License

/**
 * LinkTransform - prepare 'x' for testing at a node 'j' of the chain, by excluding 'exl'.
 * @param   x      instance//from ww w.j a va 2s  .com
 * @param   excl   indices of labels which are NOT parents of j
 * @param   _D      the dataset template to use
 * @return   the transformed instance
 */
public static Instance linkTransformation(Instance x, int excl[], Instances _D) {
    // copy
    Instance copy = (Instance) x.copy();
    copy.setDataset(null);

    // delete attributes we don't need
    for (int i = excl.length - 1; i >= 0; i--) {
        copy.deleteAttributeAt(excl[i]);
    }

    //set template
    copy.setDataset(_D);

    return copy;
}

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 w  w .  j ava2s  . c  o m
    x.setDataset(null);
    for (int i = L_t; i < L; i++)
        x.deleteAttributeAt(0);
    x.setDataset(instancesTemplate);
    return x;
}