Example usage for weka.core Instance dataset

List of usage examples for weka.core Instance dataset

Introduction

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

Prototype

public Instances dataset();

Source Link

Document

Returns the dataset this instance has access to.

Usage

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

License:Open Source License

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

    int L = xy.classIndex();

    double y[] = new double[L];
    double conf[] = new double[L];
    double w = 0.0;

    /*/*from   w  ww . j a va  2 s  .  co  m*/
     * e.g. K = [3,3,5]
     * we push y_[] from [0,0,0] to [2,2,4] over all necessary iterations.
     */
    int K[] = getKs(xy.dataset());
    if (getDebug())
        System.out.println("K[] = " + Arrays.toString(K));
    double y_[] = new double[L];

    for (int i = 0; i < 1000000; i++) { // limit to 1m
        double conf_[] = super.probabilityForInstance(xy, y_);
        double w_ = A.product(conf_);
        //System.out.println(""+i+" "+Arrays.toString(y_)+" "+w_+" "+Arrays.toString(conf_)+"/"+Arrays.toString(convertConfidenceToProbability(y_,conf_)));
        if (w_ > w) {
            if (getDebug())
                System.out.println("y' = " + Arrays.toString(y_) + ", :" + w_);
            y = Arrays.copyOf(y_, y_.length);
            w = w_;
            conf = conf_;
        }
        if (push(y_, K, 0)) {
            // Done !
            if (getDebug())
                System.out.println("Tried all " + (i + 1) + " combinations.");
            break;
        }
    }

    // If it's multi-label (binary only), return the probabilistic output (else just the values).
    return (A.max(K) > 2) ? y : convertConfidenceToProbability(conf, y); //return p_y; //y;
}

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./*  w  w w . j  a va2s. c o 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.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 {//w ww .java 2 s.com
        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:milk.core.Exemplar.java

License:Open Source License

/**
 * Constructor using one instance to form an exemplar
 * /* www.  j  a  v  a2 s . c  o  m*/
 * @param instance the given instance
 * @param id the ID index
 */
public Exemplar(Instance inst, int id) {
    m_IdIndex = id;
    m_IdValue = inst.value(id);
    m_ClassIndex = inst.classIndex();
    m_ClassValue = inst.classValue();
    m_Instances = new Instances(inst.dataset(), 1);
    m_Instances.add(inst);
}

From source file:moa.classifiers.lazy.kNN.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classValue() > C)
        C = (int) inst.classValue();
    if (this.window == null) {
        this.window = new Instances(inst.dataset());
    }//from  w  ww  .j av  a 2s  .  co m
    if (this.limitOption.getValue() <= this.window.numInstances()) {
        this.window.delete(0);
    }
    this.window.add(inst);
}

From source file:moa.classifiers.lazy.kNNwithPAW.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classValue() > C) {
        C = (int) inst.classValue();
    }//from  w  w w . j a  v a 2s. c o m
    if (this.window == null) {
        this.window = new Instances(inst.dataset());
    }

    for (int i = 0; i < this.window.size(); i++) {
        if (this.classifierRandom.nextDouble() > this.prob) {
            this.window.delete(i);
        }
    }
    this.window.add(inst);

}

From source file:moa.classifiers.lazy.kNNwithPAWandADWIN.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    if (inst.classValue() > C) {
        C = (int) inst.classValue();
    }//from  w  w  w .  j av  a2s  .c o m
    // ADWIN
    if (this.window == null) {
        this.window = new Instances(inst.dataset());
    }

    if (this.timeStamp == null) {
        this.timeStamp = new ArrayList<Integer>(10);
    }
    for (int i = 0; i < this.window.size(); i++) {
        if (this.classifierRandom.nextDouble() > this.prob) {
            this.window.delete(i);
            this.timeStamp.remove(i);
        }
    }
    this.window.add(inst);
    this.timeStamp.add(this.time);
    this.time++;
    boolean correctlyClassifies = this.correctlyClassifies(inst);
    if (this.adwin.setInput(correctlyClassifies ? 0 : 1)) {
        //Change
        int size = (int) this.adwin.getWidth();
        for (int i = 0; i < this.window.size(); i++) {
            if (this.timeStamp.get(i) < this.time - size) {
                this.window.delete(i);
                this.timeStamp.remove(i);
            }
        }
    }

}

From source file:moa.classifiers.macros.TACNB.java

License:Open Source License

public Instance extendWithOldLabels(Instance instance) {
    if (this.header == null) {
        initHeader(instance.dataset());
    }/*from ww w. j a v  a  2  s.c  o  m*/
    int numLabels = this.oldLabels.length;
    if (numLabels == 0) {
        return instance;
    }
    double[] x = instance.toDoubleArray();
    double[] x2 = Arrays.copyOfRange(this.oldLabels, 0, numLabels + x.length);
    System.arraycopy(x, 0, x2, numLabels, x.length);
    Instance extendedInstance = new DenseInstance(instance.weight(), x2);
    extendedInstance.setDataset(this.header);
    //System.out.println( extendedInstance);
    return extendedInstance;
}

From source file:moa.classifiers.novelClass.AbstractNovelClassClassifier.java

License:Apache License

final public Instance augmentInstance(Instance x) {
    Instance ret = (Instance) x.copy();/*from www  .j a v a2  s  .  c o  m*/
    ret.setDataset(augmentInstances(x.dataset()));

    return ret;
}

From source file:moa.classifiers.novelClass.SluiceBox.SluiceBoxClassifier.java

License:Apache License

@Override
public double[] getVotesForInstance(Instance inst) {
    double[] ret = this.dynamicStreamClustering.getVotesForInstance(inst);
    double sslVotes[] = this.roughClassifier.getVotesForInstance(inst);
    Sieve.safeNormalize(ret, inst.dataset());
    for (int i = 0; i < ret.length && i < sslVotes.length; ++i) {
        ret[i] += sslVotes[i] * sslVoteWeightOption.getValue();
    }/*from   w  w  w.  j  a  v a  2s.co  m*/
    return ret;
}