Example usage for weka.core Instance value

List of usage examples for weka.core Instance value

Introduction

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

Prototype

public double value(Attribute att);

Source Link

Document

Returns an instance's attribute value in internal format.

Usage

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

License:Open Source License

/**
 * Executes the flow item./*from   w ww .  ja  v  a  2  s.c  o  m*/
 *
 * @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();

    try {
        if (m_AttributeName.length() > 0) {
            index = inst.dataset().attribute(m_AttributeName).index();
        } else {
            m_Index.setMax(inst.numAttributes());
            index = m_Index.getIntIndex();
        }
        if (inst.isMissing(index)) {
            m_OutputToken = new Token("?");
        } else {
            switch (inst.attribute(index).type()) {
            case Attribute.NUMERIC:
                m_OutputToken = new Token(inst.value(index));
                break;

            case Attribute.DATE:
            case Attribute.NOMINAL:
            case Attribute.STRING:
            case Attribute.RELATIONAL:
                m_OutputToken = new Token(inst.stringValue(index));
                break;

            default:
                result = "Unhandled attribute type: " + inst.attribute(index).type();
            }
        }
    } catch (Exception e) {
        result = handleException("Failed to obtain value from instance:\n" + inst, e);
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item./* ww  w.ja  v  a  2  s .  co  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instance inst;
    SequencePlotterContainer cont;
    int[] indices;
    int i;

    result = null;

    inst = (Instance) m_InputToken.getPayload();

    m_Counter++;
    m_Containers.clear();
    m_Attributes.setMax(inst.numAttributes());
    indices = m_Attributes.getIntIndices();

    for (i = 0; i < indices.length; i++) {
        if (inst.attribute(indices[i]).isNominal())
            cont = new SequencePlotterContainer(inst.dataset().attribute(indices[i]).name(),
                    new Double(m_Counter), inst.stringValue(indices[i]));
        else
            cont = new SequencePlotterContainer(inst.dataset().attribute(indices[i]).name(),
                    new Double(m_Counter), inst.value(indices[i]));
        m_Containers.add(cont);
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item./*from   w w  w  .  ja  v  a2  s.  c o  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Instances data;
    Double old;
    Double curr;
    int i;
    int index;
    Instance inst;

    result = null;

    m_Queue.clear();

    // copy and sort data
    data = new Instances((Instances) m_InputToken.getPayload());
    m_Index.setData(data);
    ;
    index = m_Index.getIntIndex();
    data.sort(index);

    // create subsets
    old = null;
    i = 0;
    while (i < data.numInstances()) {
        inst = data.instance(i);
        curr = inst.value(index);
        if ((old == null) || !curr.equals(old)) {
            m_Queue.add(new Instances(data, data.numInstances()));
            old = curr;
        }
        m_Queue.get(m_Queue.size() - 1).add(inst);
        i++;
    }

    // compact subsets
    for (Instances sub : m_Queue)
        sub.compactify();

    return result;
}

From source file:adams.opt.optimise.genetic.fitnessfunctions.AttributeSelection.java

License:Open Source License

public double evaluate(OptData opd) {
    init();/*from  w  w w .j av  a2 s .  co m*/
    int cnt = 0;
    int[] weights = getWeights(opd);
    Instances newInstances = new Instances(getInstances());
    for (int i = 0; i < getInstances().numInstances(); i++) {
        Instance in = newInstances.instance(i);
        cnt = 0;
        for (int a = 0; a < getInstances().numAttributes(); a++) {
            if (a == getInstances().classIndex())
                continue;
            if (weights[cnt++] == 0) {
                in.setValue(a, 0);
            } else {
                in.setValue(a, in.value(a));
            }
        }
    }
    Classifier newClassifier = null;

    try {
        newClassifier = (Classifier) OptionUtils.shallowCopy(getClassifier());
        // evaluate classifier on data
        Evaluation evaluation = new Evaluation(newInstances);
        evaluation.crossValidateModel(newClassifier, newInstances, getFolds(),
                new Random(getCrossValidationSeed()));

        // obtain measure
        double measure = 0;
        if (getMeasure() == Measure.ACC)
            measure = evaluation.pctCorrect();
        else if (getMeasure() == Measure.CC)
            measure = evaluation.correlationCoefficient();
        else if (getMeasure() == Measure.MAE)
            measure = evaluation.meanAbsoluteError();
        else if (getMeasure() == Measure.RAE)
            measure = evaluation.relativeAbsoluteError();
        else if (getMeasure() == Measure.RMSE)
            measure = evaluation.rootMeanSquaredError();
        else if (getMeasure() == Measure.RRSE)
            measure = evaluation.rootRelativeSquaredError();
        else
            throw new IllegalStateException("Unhandled measure '" + getMeasure() + "'!");
        measure = getMeasure().adjust(measure);

        return (measure);
        // process fitness

    } catch (Exception e) {
        getLogger().log(Level.SEVERE, "Error evaluating", e);
    }

    return 0;
}

From source file:adams.opt.optimise.genetic.fitnessfunctions.AttributeSelection.java

License:Open Source License

/**
 * Callback for best measure so far//from w w  w  . j  a v a2 s .  co  m
 */
@Override
public void newBest(double val, OptData opd) {
    int cnt = 0;
    int[] weights = getWeights(opd);
    Instances newInstances = new Instances(getInstances());
    for (int i = 0; i < getInstances().numInstances(); i++) {
        Instance in = newInstances.instance(i);
        cnt = 0;
        for (int a = 0; a < getInstances().numAttributes(); a++) {
            if (a == getInstances().classIndex())
                continue;
            if (weights[cnt++] == 0) {
                in.setValue(a, 0);
            } else {
                in.setValue(a, in.value(a));
            }
        }
    }
    try {
        File file = new File(getOutputDirectory().getAbsolutePath() + File.separator
                + Double.toString(getMeasure().adjust(val)) + ".arff");
        file.createNewFile();
        Writer writer = new BufferedWriter(new FileWriter(file));
        Instances header = new Instances(newInstances, 0);

        // remove filter setup
        Remove remove = new Remove();
        remove.setAttributeIndices(getRemoveAsString(weights));
        remove.setInvertSelection(true);

        header.setRelationName(OptionUtils.getCommandLine(remove));

        writer.write(header.toString());
        writer.write("\n");
        for (int i = 0; i < newInstances.numInstances(); i++) {
            writer.write(newInstances.instance(i).toString());
            writer.write("\n");
        }
        writer.flush();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:adams.tools.CompareDatasets.java

License:Open Source License

/**
 * Returns the correlation between the two rows.
 *
 * @param first   the first row//from   ww w .  j av a 2s.  c  o  m
 * @param second   the second row
 * @return      the correlation
 */
protected double getCorrelation(Instance first, Instance second) {
    double[] val1;
    double[] val2;
    int i;

    val1 = new double[m_Indices1.length];
    val2 = new double[m_Indices2.length];

    for (i = 0; i < val1.length; i++) {
        if (first.attribute(m_Indices1[i]).isNumeric())
            val1[i] = first.value(m_Indices1[i]);
        if (second.attribute(m_Indices2[i]).isNumeric())
            val2[i] = second.value(m_Indices2[i]);
    }

    return StatUtils.correlationCoefficient(val1, val2);
}

From source file:adaptedClusteringAlgorithms.MyFarthestFirst.java

License:Open Source License

/**
 * Updates the minimum and maximum values for all the attributes
 * based on a new instance./* w  w  w .  j a va 2  s.  co  m*/
 *
 * @param instance the new instance
 */
private void updateMinMax(Instance instance) {

    for (int j = 0; j < instance.numAttributes(); j++) {
        if (Double.isNaN(m_Min[j])) {
            m_Min[j] = instance.value(j);
            m_Max[j] = instance.value(j);
        } else {
            if (instance.value(j) < m_Min[j]) {
                m_Min[j] = instance.value(j);
            } else {
                if (instance.value(j) > m_Max[j]) {
                    m_Max[j] = instance.value(j);
                }
            }
        }
    }
}

From source file:affective.core.ArffLexiconEvaluator.java

License:Open Source License

/**
 * Processes  all the dictionary files.//w w w  .j  ava2s  .  c  o m
 * @throws IOException  an IOException will be raised if an invalid file is supplied
 */
public void processDict() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(this.m_lexiconFile));
    Instances lexInstances = new Instances(reader);

    // set upper value for word index
    lexiconWordIndex.setUpper(lexInstances.numAttributes() - 1);

    List<Attribute> numericAttributes = new ArrayList<Attribute>();
    List<Attribute> nominalAttributes = new ArrayList<Attribute>();

    // checks all numeric and nominal attributes and discards the word attribute
    for (int i = 0; i < lexInstances.numAttributes(); i++) {

        if (i != this.lexiconWordIndex.getIndex()) {
            if (lexInstances.attribute(i).isNumeric()) {
                numericAttributes.add(lexInstances.attribute(i));
                // adds the attribute name to the message-level features to be calculated
                this.featureNames.add(this.lexiconName + "-" + lexInstances.attribute(i).name());
            }

            else if (lexInstances.attribute(i).isNominal()) {
                nominalAttributes.add(lexInstances.attribute(i));
                // adds the attribute name together with the nominal value to the message-level features to be calculated
                int numValues = lexInstances.attribute(i).numValues();
                for (int j = 0; j < numValues; j++)
                    this.featureNames.add(this.lexiconName + "-" + lexInstances.attribute(i).name() + "-"
                            + lexInstances.attribute(i).value(j));

            }

        }

    }

    // Maps all words with their affective scores discarding missing values
    for (Instance inst : lexInstances) {
        if (inst.attribute(this.lexiconWordIndex.getIndex()).isString()) {
            String word = inst.stringValue(this.lexiconWordIndex.getIndex());
            // stems the word
            word = this.m_stemmer.stem(word);

            // map numeric scores
            if (!numericAttributes.isEmpty()) {
                Map<String, Double> wordVals = new HashMap<String, Double>();
                for (Attribute na : numericAttributes) {
                    if (!weka.core.Utils.isMissingValue(inst.value(na)))
                        wordVals.put(na.name(), inst.value(na));
                }
                this.numDict.put(word, wordVals);
            }

            // map nominal associations
            if (!nominalAttributes.isEmpty()) {
                Map<String, String> wordCounts = new HashMap<String, String>();
                for (Attribute no : nominalAttributes) {
                    if (!weka.core.Utils.isMissingValue(inst.value(no))) {
                        wordCounts.put(no.name(), no.value((int) inst.value(no)));
                    }

                    this.nomDict.put(word, wordCounts);

                }

            }

        }

    }

}

From source file:affective.core.ArffLexiconWordLabeller.java

License:Open Source License

/**
 * Processes  all the dictionary files./*w w w  . j  a v  a2  s  . c  o m*/
 * @throws IOException  an IOException will be raised if an invalid file is supplied
 */
public void processDict() throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(this.m_lexiconFile));
    Instances lexInstances = new Instances(reader);

    // set upper value for word index
    lexiconWordIndex.setUpper(lexInstances.numAttributes() - 1);

    // checks all numeric and nominal attributes and discards the word attribute
    for (int i = 0; i < lexInstances.numAttributes(); i++) {

        if (i != this.lexiconWordIndex.getIndex()) {
            if (lexInstances.attribute(i).isNumeric() || lexInstances.attribute(i).isNominal()) {
                this.attributes.add(lexInstances.attribute(i));
            }

        }

    }

    // Maps all words with their affective scores discarding missing values
    for (Instance inst : lexInstances) {
        if (inst.attribute(this.lexiconWordIndex.getIndex()).isString()) {
            String word = inst.stringValue(this.lexiconWordIndex.getIndex());
            // stems the word
            word = this.m_stemmer.stem(word);

            // map numeric scores
            if (!attributes.isEmpty()) {
                Map<Attribute, Double> wordVals = new HashMap<Attribute, Double>();
                for (Attribute na : attributes) {
                    wordVals.put(na, inst.value(na));
                }
                this.attValMap.put(word, wordVals);
            }

        }

    }

}

From source file:ai.GiniFunction.java

License:GNU General Public License

/**
 * Create split function based on Gini coefficient
 * //  ww  w .ja v  a 2s .co  m
 * @param data original data
 * @param indices indices of the samples to use
 */
public void init(Instances data, ArrayList<Integer> indices) {
    if (indices.size() == 0) {
        this.index = 0;
        this.threshold = 0;
        this.allSame = true;
        return;
    }

    final int len = data.numAttributes();
    final int numElements = indices.size();
    final int numClasses = data.numClasses();
    final int classIndex = data.classIndex();

    /** Attribute-class pair comparator (by attribute value) */
    final Comparator<AttributeClassPair> comp = new Comparator<AttributeClassPair>() {
        public int compare(AttributeClassPair o1, AttributeClassPair o2) {
            final double diff = o2.attributeValue - o1.attributeValue;
            if (diff < 0)
                return 1;
            else if (diff == 0)
                return 0;
            else
                return -1;
        }

        public boolean equals(Object o) {
            return false;
        }
    };

    // Create and shuffle indices of features to use
    ArrayList<Integer> allIndices = new ArrayList<Integer>();
    for (int i = 0; i < len; i++)
        if (i != classIndex)
            allIndices.add(i);

    double minimumGini = Double.MAX_VALUE;

    for (int i = 0; i < numOfFeatures; i++) {
        // Select the random feature
        final int index = random.nextInt(allIndices.size());
        final int featureToUse = allIndices.get(index);
        allIndices.remove(index); // remove that element to prevent from repetitions

        // Get the smallest Gini coefficient

        // Create list with pairs attribute-class
        final ArrayList<AttributeClassPair> list = new ArrayList<AttributeClassPair>();
        for (int j = 0; j < numElements; j++) {
            final Instance ins = data.get(indices.get(j));
            list.add(new AttributeClassPair(ins.value(featureToUse), (int) ins.value(classIndex)));
        }

        // Sort pairs in increasing order
        Collections.sort(list, comp);

        final double[] probLeft = new double[numClasses];
        final double[] probRight = new double[numClasses];
        // initial probabilities (all samples on the right)
        for (int n = 0; n < list.size(); n++)
            probRight[list.get(n).classValue]++;

        // Try all splitting points, from position 0 to the end
        for (int splitPoint = 0; splitPoint < numElements; splitPoint++) {
            // Calculate Gini coefficient
            double giniLeft = 0;
            double giniRight = 0;
            final int rightNumElements = numElements - splitPoint;

            for (int nClass = 0; nClass < numClasses; nClass++) {
                // left set
                double prob = probLeft[nClass];
                // Divide by the number of elements to get probabilities
                if (splitPoint != 0)
                    prob /= (double) splitPoint;
                giniLeft += prob * prob;

                // right set
                prob = probRight[nClass];
                // Divide by the number of elements to get probabilities
                if (rightNumElements != 0)
                    prob /= (double) rightNumElements;
                giniRight += prob * prob;
            }

            // Total Gini value
            final double gini = ((1.0 - giniLeft) * splitPoint + (1.0 - giniRight) * rightNumElements)
                    / (double) numElements;

            // Save values of minimum Gini coefficient
            if (gini < minimumGini) {
                minimumGini = gini;
                this.index = featureToUse;
                this.threshold = list.get(splitPoint).attributeValue;
            }

            // update probabilities for next iteration
            probLeft[list.get(splitPoint).classValue]++;
            probRight[list.get(splitPoint).classValue]--;
        }
    }

    // free list of possible indices to help garbage collector
    //allIndices.clear();
    //allIndices = null;
}