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:PrincipalComponents.java

License:Open Source License

/**
 * Transform an instance in original (unormalized) format. Convert back to
 * the original space if requested./*from   w  ww.ja  v  a  2s  .c o m*/
 *
 * @param instance an instance in the original (unormalized) format
 * @return a transformed instance
 * @throws Exception if instance cant be transformed
 */
@Override
public Instance convertInstance(Instance instance) throws Exception {

    if (m_eigenvalues == null) {
        throw new Exception("convertInstance: Principal components not " + "built yet");
    }

    double[] newVals = new double[m_outputNumAtts];
    Instance tempInst = (Instance) instance.copy();
    if (!instance.dataset().equalHeaders(m_trainHeader)) {
        throw new Exception("Can't convert instance: header's don't match: " + "PrincipalComponents\n"
                + instance.dataset().equalHeadersMsg(m_trainHeader));
    }

    m_replaceMissingFilter.input(tempInst);
    m_replaceMissingFilter.batchFinished();
    tempInst = m_replaceMissingFilter.output();

    /*
     * if (m_normalize) { m_normalizeFilter.input(tempInst);
     * m_normalizeFilter.batchFinished(); tempInst =
     * m_normalizeFilter.output(); }
     */

    m_nominalToBinFilter.input(tempInst);
    m_nominalToBinFilter.batchFinished();
    tempInst = m_nominalToBinFilter.output();

    if (m_attributeFilter != null) {
        m_attributeFilter.input(tempInst);
        m_attributeFilter.batchFinished();
        tempInst = m_attributeFilter.output();
    }

    if (!m_center) {
        m_standardizeFilter.input(tempInst);
        m_standardizeFilter.batchFinished();
        tempInst = m_standardizeFilter.output();
    } else {
        m_centerFilter.input(tempInst);
        m_centerFilter.batchFinished();
        tempInst = m_centerFilter.output();
    }

    if (m_hasClass) {
        newVals[m_outputNumAtts - 1] = instance.value(instance.classIndex());
    }

    double cumulative = 0;
    int numAttAdded = 0;
    for (int i = m_numAttribs - 1; i >= 0; i--) {
        double tempval = 0.0;
        for (int j = 0; j < m_numAttribs; j++) {
            tempval += (m_eigenvectors[j][m_sortedEigens[i]] * tempInst.value(j));
        }
        newVals[m_numAttribs - i - 1] = tempval;
        cumulative += m_eigenvalues[m_sortedEigens[i]];
        if ((cumulative / m_sumOfEigenValues) >= m_coverVariance) {
            break;
        }
        if (numAttAdded > m_maxNumAttr) {
            break;
        }
        numAttAdded++;
    }

    if (!m_transBackToOriginal) {
        if (instance instanceof SparseInstance) {
            return new SparseInstance(instance.weight(), newVals);
        } else {
            return new DenseInstance(instance.weight(), newVals);
        }
    } else {
        if (instance instanceof SparseInstance) {
            return convertInstanceToOriginal(new SparseInstance(instance.weight(), newVals));
        } else {
            return convertInstanceToOriginal(new DenseInstance(instance.weight(), newVals));
        }
    }
}

From source file:MultiClassClassifier.java

License:Open Source License

/**
 * Returns the individual predictions of the base classifiers
 * for an instance. Used by StackedMultiClassClassifier.
 * Returns the probability for the second "class" predicted
 * by each base classifier.//  w  w w . j av a  2  s.  c  o m
 *
 * @param inst the instance to get the prediction for
 * @return the individual predictions
 * @throws Exception if the predictions can't be computed successfully
 */
public double[] individualPredictions(Instance inst) throws Exception {

    double[] result = null;

    if (m_Classifiers.length == 1) {
        result = new double[1];
        result[0] = m_Classifiers[0].distributionForInstance(inst)[1];
    } else {
        result = new double[m_ClassFilters.length];
        for (int i = 0; i < m_ClassFilters.length; i++) {
            if (m_Classifiers[i] != null) {
                if (m_Method == METHOD_1_AGAINST_1) {
                    Instance tempInst = (Instance) inst.copy();
                    tempInst.setDataset(m_TwoClassDataset);
                    result[i] = m_Classifiers[i].distributionForInstance(tempInst)[1];
                } else {
                    m_ClassFilters[i].input(inst);
                    m_ClassFilters[i].batchFinished();
                    result[i] = m_Classifiers[i].distributionForInstance(m_ClassFilters[i].output())[1];
                }
            }
        }
    }
    return result;
}

From source file:MultiClassClassifier.java

License:Open Source License

/**
 * Returns the distribution for an instance.
 *
 * @param inst the instance to get the distribution for
 * @return the distribution/*from   ww w .j a  va 2s.c o m*/
 * @throws Exception if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance inst) throws Exception {

    if (m_Classifiers.length == 1) {
        return m_Classifiers[0].distributionForInstance(inst);
    }

    double[] probs = new double[inst.numClasses()];

    if (m_Method == METHOD_1_AGAINST_1) {
        double[][] r = new double[inst.numClasses()][inst.numClasses()];
        double[][] n = new double[inst.numClasses()][inst.numClasses()];

        for (int i = 0; i < m_ClassFilters.length; i++) {
            if (m_Classifiers[i] != null) {
                Instance tempInst = (Instance) inst.copy();
                tempInst.setDataset(m_TwoClassDataset);
                double[] current = m_Classifiers[i].distributionForInstance(tempInst);
                Range range = new Range(((RemoveWithValues) m_ClassFilters[i]).getNominalIndices());
                range.setUpper(m_ClassAttribute.numValues());
                int[] pair = range.getSelection();
                if (m_pairwiseCoupling && inst.numClasses() > 2) {
                    r[pair[0]][pair[1]] = current[0];
                    n[pair[0]][pair[1]] = m_SumOfWeights[i];
                } else {
                    if (current[0] > current[1]) {
                        probs[pair[0]] += 1.0;
                    } else {
                        probs[pair[1]] += 1.0;
                    }
                }
            }
        }
        if (m_pairwiseCoupling && inst.numClasses() > 2) {
            return pairwiseCoupling(n, r);
        }
    } else {
        // error correcting style methods
        for (int i = 0; i < m_ClassFilters.length; i++) {
            m_ClassFilters[i].input(inst);
            m_ClassFilters[i].batchFinished();
            double[] current = m_Classifiers[i].distributionForInstance(m_ClassFilters[i].output());
            //Calibrate the binary classifier scores

            for (int j = 0; j < m_ClassAttribute.numValues(); j++) {
                if (((MakeIndicator) m_ClassFilters[i]).getValueRange().isInRange(j)) {
                    probs[j] += current[1];
                } else {
                    probs[j] += current[0];
                }
            }
        }
    }

    if (Utils.gr(Utils.sum(probs), 0)) {
        Utils.normalize(probs);
        return probs;
    } else {
        return m_ZeroR.distributionForInstance(inst);
    }
}

From source file:adams.flow.container.WekaClusteringContainer.java

License:Open Source License

/**
 * Initializes the container./*ww w  . j a v a 2s .  co  m*/
 *
 * @param inst         the instance that was used for prediction
 * @param cluster         the chosen cluster
 * @param dist         the cluster distribution
 * @param logDensity         the log density
 * @param logDensityPerCluster   the log density per cluster
 * @param logJointDensities      the log joint densities
 */
public WekaClusteringContainer(Instance inst, int cluster, double[] dist, double logDensity,
        double[] logDensityPerCluster, double[] logJointDensities) {
    super();

    if (inst != null)
        store(VALUE_INSTANCE, (Instance) inst.copy());
    store(VALUE_CLUSTER, cluster);
    store(VALUE_DISTRIBUTION, dist.clone());
    if (logDensityPerCluster.length > 0) {
        store(VALUE_LOGDENSITY, logDensity);
        store(VALUE_LOGDENSITYPERCLUSTER, logDensityPerCluster.clone());
        store(VALUE_LOGJOINTDENSITIES, logJointDensities.clone());
    }
}

From source file:adams.flow.container.WekaPredictionContainer.java

License:Open Source License

/**
 * Initializes the container./*from   ww  w  . j  a va 2  s.c  om*/
 *
 * @param inst   the instance that was used for prediction
 * @param cls      the classification
 * @param dist   the class distribution
 * @param rangeCheck   the range check, null if not available
 */
public WekaPredictionContainer(Instance inst, double cls, double[] dist, String rangeCheck) {
    super();

    if (inst != null)
        store(VALUE_INSTANCE, inst.copy());
    store(VALUE_CLASSIFICATION, cls);
    store(VALUE_DISTRIBUTION, dist.clone());
    if ((inst != null) && inst.classAttribute().isNominal())
        store(VALUE_CLASSIFICATION_LABEL, inst.classAttribute().value((int) cls));
    if (rangeCheck != null)
        store(VALUE_RANGECHECK, rangeCheck);
}

From source file:adams.flow.transformer.WekaDatasetsMerge.java

License:Open Source License

/**
 * Creates an Instances dataset, containing a copy of the single instance
 * provided./* ww w  .ja  v  a2  s.co m*/
 *
 * @param instance The instance to create a dataset for.
 * @return The created dataset.
 */
protected Instances datasetForSingleInstance(Instance instance) {
    // Create a copy of the instance's original dataset
    Instances dataset = new Instances(instance.dataset(), 1);

    // Add a copy of the provided instance
    dataset.add((Instance) instance.copy());

    // Return the dataset
    return dataset;
}

From source file:adams.flow.transformer.WekaInstanceBuffer.java

License:Open Source License

/**
 * Executes the flow item.//w  ww.j  a v  a  2s. c  o  m
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance[] insts;
    Instance inst;
    double[] values;
    int i;
    int n;
    boolean updated;

    result = null;

    if (m_Operation == Operation.INSTANCE_TO_INSTANCES) {
        if (m_InputToken.getPayload() instanceof Instance) {
            insts = new Instance[] { (Instance) m_InputToken.getPayload() };
        } else {
            insts = (Instance[]) m_InputToken.getPayload();
        }

        for (n = 0; n < insts.length; n++) {
            inst = insts[n];

            if ((m_Buffer != null) && m_CheckHeader) {
                if (!m_Buffer.equalHeaders(inst.dataset())) {
                    getLogger().info("Header changed, resetting buffer");
                    m_Buffer = null;
                }
            }

            // buffer instance
            if (m_Buffer == null)
                m_Buffer = new Instances(inst.dataset(), 0);

            // we need to make sure that string and relational values are in our
            // buffer header and update the current Instance accordingly before
            // buffering it
            values = inst.toDoubleArray();
            updated = false;
            for (i = 0; i < values.length; i++) {
                if (inst.isMissing(i))
                    continue;
                if (inst.attribute(i).isString()) {
                    values[i] = m_Buffer.attribute(i).addStringValue(inst.stringValue(i));
                    updated = true;
                } else if (inst.attribute(i).isRelationValued()) {
                    values[i] = m_Buffer.attribute(i).addRelation(inst.relationalValue(i));
                    updated = true;
                }
            }

            if (updated) {
                if (inst instanceof SparseInstance) {
                    inst = new SparseInstance(inst.weight(), values);
                } else if (inst instanceof BinarySparseInstance) {
                    inst = new BinarySparseInstance(inst.weight(), values);
                } else {
                    if (!(inst instanceof DenseInstance)) {
                        getLogger().severe("Unhandled instance class (" + inst.getClass().getName() + "), "
                                + "defaulting to " + DenseInstance.class.getName());
                    }
                    inst = new DenseInstance(inst.weight(), values);
                }
            } else {
                inst = (Instance) inst.copy();
            }

            m_Buffer.add(inst);
        }

        if (m_Buffer.numInstances() % m_Interval == 0) {
            m_OutputToken = new Token(m_Buffer);
            if (m_ClearBuffer)
                m_Buffer = null;
        }
    } else if (m_Operation == Operation.INSTANCES_TO_INSTANCE) {
        m_Buffer = (Instances) m_InputToken.getPayload();
        m_Iterator = m_Buffer.iterator();
    } else {
        throw new IllegalStateException("Unhandled operation: " + m_Operation);
    }

    return result;
}

From source file:adams.flow.transformer.WekaSetInstanceValue.java

License:Open Source License

/**
 * Executes the flow item./*ww w  .j  a  va  2s  .  c om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    int index;

    result = null;

    inst = (Instance) m_InputToken.getPayload();
    inst = (Instance) inst.copy();
    m_Index.setData(inst.dataset());
    index = m_Index.getIntIndex();

    try {
        if (m_Value.equals("?")) {
            inst.setMissing(index);
        } else {
            switch (inst.attribute(index).type()) {
            case Attribute.NUMERIC:
                inst.setValue(index, Utils.toDouble(m_Value));
                break;

            case Attribute.DATE:
                inst.setValue(index, inst.attribute(index).parseDate(m_Value));
                break;

            case Attribute.NOMINAL:
            case Attribute.STRING:
                inst.setValue(index, m_Value);
                break;

            case Attribute.RELATIONAL:
                result = "Relational attributes cannot be set!";
                break;

            default:
                result = "Unhandled attribute type: " + inst.attribute(index).type();
            }
        }
    } catch (Exception e) {
        result = handleException("Failed to set value: " + m_Index.getIndex() + " -> " + m_Value, e);
    }

    // broadcast data
    if (result == null)
        m_OutputToken = new Token(inst);

    return result;
}

From source file:adams.flow.transformer.WekaTrainClassifier.java

License:Open Source License

/**
 * Executes the flow item./*  w  w w  .j a va2  s. co  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances data;
    Instance inst;
    weka.classifiers.Classifier cls;

    result = null;

    try {
        cls = null;
        if ((m_InputToken != null) && (m_InputToken.getPayload() instanceof Instances)) {
            cls = getClassifierInstance();
            data = (Instances) m_InputToken.getPayload();
            cls.buildClassifier(data);
            m_OutputToken = new Token(new WekaModelContainer(cls, new Instances(data, 0), data));
        } else if ((m_InputToken != null) && (m_InputToken.getPayload() instanceof Instance)) {
            if (m_IncrementalClassifier == null) {
                cls = getClassifierInstance();
                if (!(cls instanceof UpdateableClassifier))
                    result = m_Classifier + "/" + cls.getClass().getName()
                            + " is not an incremental classifier!";
            }
            if (result == null) {
                inst = (Instance) m_InputToken.getPayload();
                if (m_IncrementalClassifier == null) {
                    m_IncrementalClassifier = cls;
                    if (m_SkipBuild) {
                        ((UpdateableClassifier) m_IncrementalClassifier).updateClassifier(inst);
                    } else {
                        data = new Instances(inst.dataset(), 1);
                        data.add((Instance) inst.copy());
                        m_IncrementalClassifier.buildClassifier(data);
                    }
                } else {
                    ((UpdateableClassifier) m_IncrementalClassifier).updateClassifier(inst);
                }
                m_OutputToken = new Token(
                        new WekaModelContainer(m_IncrementalClassifier, new Instances(inst.dataset(), 0)));
            }
        }
    } catch (Exception e) {
        m_OutputToken = null;
        result = handleException("Failed to process data:", e);
    }

    if (m_OutputToken != null)
        updateProvenance(m_OutputToken);

    return result;
}

From source file:adams.flow.transformer.WekaTrainClusterer.java

License:Open Source License

/**
 * Executes the flow item.//from  w  w  w  . j a  v a2 s.  c  om
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances data;
    Instance inst;
    weka.clusterers.Clusterer cls;
    WekaModelContainer cont;

    result = null;

    try {
        cls = null;
        if ((m_InputToken != null) && (m_InputToken.getPayload() instanceof Instances)) {
            cls = getClustererInstance();
            data = (Instances) m_InputToken.getPayload();
            cls.buildClusterer(data);
            cont = new WekaModelContainer(cls, new Instances(data, 0), data);
            cont = m_PostProcessor.postProcess(cont);
            m_OutputToken = new Token(cont);
        } else if ((m_InputToken != null) && (m_InputToken.getPayload() instanceof Instance)) {
            if (m_IncrementalClusterer == null) {
                cls = getClustererInstance();
                if (!(cls instanceof UpdateableClusterer))
                    result = m_Clusterer + "/" + cls.getClass().getName() + " is not an incremental clusterer!";
            }
            if (result == null) {
                inst = (Instance) m_InputToken.getPayload();
                if (m_IncrementalClusterer == null) {
                    m_IncrementalClusterer = cls;
                    data = new Instances(inst.dataset(), 1);
                    data.add((Instance) inst.copy());
                    m_IncrementalClusterer.buildClusterer(data);
                } else {
                    ((UpdateableClusterer) m_IncrementalClusterer).updateClusterer(inst);
                    ((UpdateableClusterer) m_IncrementalClusterer).updateFinished();
                }
                m_OutputToken = new Token(
                        new WekaModelContainer(m_IncrementalClusterer, new Instances(inst.dataset(), 0)));
            }
        }
    } catch (Exception e) {
        m_OutputToken = null;
        result = handleException("Failed to process input: " + m_InputToken.getPayload(), e);
    }

    if (m_OutputToken != null)
        updateProvenance(m_OutputToken);

    return result;
}