Example usage for weka.core Instance copy

List of usage examples for weka.core Instance copy

Introduction

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

Prototype

Object copy();

Source Link

Document

This method produces a shallow copy of an object.

Usage

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

License:Open Source License

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

    for (int i = 0; i < m_NumIterations; i++) {
        // Oza-Bag style
        int k = poisson(1.0, random);
        if (m_BagSizePercent == 100) {
            // Train on all instances
            k = 1;/*from  www  . j av  a 2  s. c  o  m*/
        }
        if (k > 0) {
            // Train on this instance only if k > 0
            Instance x_weighted = (Instance) x.copy();
            x_weighted.setWeight(x.weight() * (double) k);
            ((UpdateableClassifier) m_Classifiers[i]).updateClassifier(x_weighted);
        }
    }
}

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);// ww  w .  ja  v a 2  s  .  co m
    double y[] = m_MULAN.makePrediction(x).getConfidences();
    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 ava2  s  .  c  om
 * @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();
    x.setDataset(null);/*from   www. j  a va2 s  .  com*/
    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)
 *//*  www.  ja v a  2s  . 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.PSUtils.java

License:Open Source License

/**
 * Convert a multi-label instance into a multi-class instance, according to a template.
 *//*from  w ww . j av a2s .  co  m*/
public static Instance convertInstance(Instance x, int L, Instances template) {
    Instance x_ = (Instance) x.copy();
    x_.setDataset(null);
    for (int i = 0; i < L; i++)
        x_.deleteAttributeAt(0);
    x_.insertAttributeAt(0);
    x_.setDataset(template);
    return x_;
}

From source file:milk.classifiers.MINND.java

License:Open Source License

/**
 * Scale the given exemplar so that the returned exemplar
 * has the value of 0 to 1 for each dimension
 * //w ww .  jav a 2s. co m
 * @param before the given exemplar
 * @return the resultant exemplar after scaling
 * @exception if given exampler cannot be scaled properly
 */
private Exemplar scale(Exemplar before) throws Exception {
    Instances data = before.getInstances();
    Exemplar after = new Exemplar(before, 0);
    for (int i = 0; i < data.numInstances(); i++) {
        Instance datum = data.instance(i);
        Instance inst = (Instance) datum.copy();
        int k = 0;
        for (int j = 0; j < data.numAttributes(); j++) {
            if ((j != before.idIndex()) && (j != before.classIndex())) {
                if (data.attribute(j).isNumeric())
                    inst.setValue(j, (datum.value(j) - m_MinArray[k]) / (m_MaxArray[k] - m_MinArray[k]));
                k++;
            }
        }
        after.add(inst);
    }
    return after;
}

From source file:milk.core.Exemplar.java

License:Open Source License

/**
 * Adds one instance to the end of the set. 
 * Shallow copies instance before it is added. Increases the
 * size of the dataset if it is not large enough. Does not
 * check if the instance is compatible with the dataset.
 *
 * @param instance the instance to be added
 *///from w ww .  j  a  va  2s .c  om
public final void add(Instance instance) {
    Instance inst = (Instance) instance.copy();

    if (!checkInstance(instance))
        throw new IllegalArgumentException(
                "The Id value and/or class value " + "is not compatible: add failed.");
    else
        m_Instances.add(inst);
}

From source file:milk.core.Exemplars.java

License:Open Source License

/**
 * Adds one instance to one of the exemplars 
 *
 * @param instance the instance to be added
 * @exception Exception if the instance cannot be added properly
 *//*ww w .j a  v a  2 s  .  c o  m*/
public final void add(Instance instance) {
    Instance ins = (Instance) instance.copy();

    int idv = (int) ins.value(m_IdIndex);
    int x = 0;
    for (; x < m_Exemplars.size(); x++) {
        Exemplar ex = (Exemplar) m_Exemplars.elementAt(x);
        if (ex != null) {
            if ((int) (ex.idValue()) == idv) {
                if (!ex.checkInstance(instance))
                    throw new IllegalArgumentException("Instance not compatible " + "with the data");
                ex.add(ins);
                break;
            }
        }
    }
    if (x == m_Exemplars.size()) {
        Exemplar ex = new Exemplar(ins, m_IdIndex);
        ex.setWeight(1.0);
        m_Exemplars.addElement(ex);
    }
}

From source file:ml.ann.BackPropagation.java

License:Open Source License

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

    Instance currentInstance;/* www.  ja v  a  2s . c om*/
    // default model?
    if (useDefaultModel) {
        return zeroR.distributionForInstance(instance);
    }

    // Make a copy of the instance so that it isn't modified
    currentInstance = (Instance) instance.copy();

    for (int noa = 0; noa < currentInstance.numAttributes(); noa++) {
        if (noa != neuronTopology.instances.classIndex()) {
            if (attributeRanges[noa] != 0) {
                currentInstance.setValue(noa,
                        (currentInstance.value(noa) - attributeBases[noa]) / attributeRanges[noa]);
            } else {
                currentInstance.setValue(noa, currentInstance.value(noa) - attributeBases[noa]);
            }
        }
    }
    //        resetNetwork();

    //        initInputAndTarget(currentInstance);

    int lastLayerIdx = neuronTopology.numLayers - 1;
    for (int layers = 0; layers < neuronTopology.numLayers; layers++) {
        for (int neu = 0; neu < neuronTopology.numNeuronEachLayer[layers]; neu++) {
            neuronTopology.output[layers][neu] = sigmoidFunction(layers, neu);
        }
    }

    double[] jancuk = new double[neuronTopology.numNeuronEachLayer[lastLayerIdx]];

    for (int i = 0; i < neuronTopology.numNeuronEachLayer[lastLayerIdx]; i++) {
        jancuk[i] = neuronTopology.output[lastLayerIdx][i];
    }

    // now normalize the array
    double count = 0;
    for (int noa = 0; noa < neuronTopology.numClasses; noa++) {
        count += jancuk[noa];
    }
    if (count <= 0) {
        //            return zeroR.distributionForInstance(i);
    }
    for (int noa = 0; noa < neuronTopology.numClasses; noa++) {
        jancuk[noa] /= count;
    }
    return jancuk;
}