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:org.openml.webapplication.evaluate.EvaluateStreamPredictions.java

License:Open Source License

private void doEvaluation() throws Exception {
    // set global evaluation
    Evaluation globalEvaluator = new Evaluation(datasetStructure);

    // we go through all the instances in one loop. 
    Instance currentInstance;/* www.  ja  v a  2 s  .c o m*/
    for (int iInstanceNr = 0; ((currentInstance = datasetLoader
            .getNextInstance(datasetStructure)) != null); ++iInstanceNr) {
        Instance currentPrediction = predictionsLoader.getNextInstance(predictionsStructure);
        if (currentPrediction == null)
            throw new Exception("Could not find prediction for instance #" + iInstanceNr);

        int rowid = (int) currentPrediction.value(ATT_PREDICTION_ROWID);

        if (rowid != iInstanceNr) {
            throw new Exception("Predictions need to be done in the same order as the dataset. "
                    + "Could not find prediction for instance #" + iInstanceNr
                    + ". Found prediction for instance #" + rowid + " instead.");
        }

        double[] confidences = InstancesHelper.predictionToConfidences(datasetStructure, currentPrediction,
                ATT_PREDICTION_CONFIDENCE, ATT_PREDICTION_PREDICTION);
        // TODO: we might want to throw an error if the sum of confidences is not 1.0. Not now though. 
        confidences = InstancesHelper.toProbDist(confidences); // TODO: security, we might be more picky later on and requiring real prob distributions.
        try {
            globalEvaluator.evaluateModelOnceAndRecordPrediction(confidences, currentInstance);
        } catch (ArrayIndexOutOfBoundsException aiobe) {
            throw new Exception(
                    "ArrayIndexOutOfBoundsException: This is an error that occurs when the classifier returns negative values. ");
        }
    }

    globalMeasures = Output.evaluatorToMap(globalEvaluator, nrOfClasses, TaskType.TESTTHENTRAIN);
}

From source file:org.openml.webapplication.evaluate.EvaluateSurvivalAnalysisPredictions.java

License:Open Source License

private void doEvaluation() throws Exception {
    for (int i = 0; i < predictions.numInstances(); i++) {
        Instance prediction = predictions.instance(i);
        int repeat = ATT_PREDICTION_REPEAT < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_REPEAT);
        int fold = ATT_PREDICTION_FOLD < 0 ? 0 : (int) prediction.value(ATT_PREDICTION_FOLD);
        int rowid = (int) prediction.value(ATT_PREDICTION_ROWID);

        predictionCounter.addPrediction(repeat, fold, 0, rowid);
        if (dataset.numInstances() <= rowid) {
            throw new RuntimeException("Making a prediction for row_id" + rowid
                    + " (0-based) while dataset has only " + dataset.numInstances() + " instances. ");
        }/*from ww w.j  a  v a 2  s .  c om*/
    }

    if (predictionCounter.check() == false) {
        throw new RuntimeException("Prediction count does not match: " + predictionCounter.getErrorMessage());
    }

    List<EvaluationScore> evaluationMeasuresList = new ArrayList<EvaluationScore>();

    evaluationScores = evaluationMeasuresList.toArray(new EvaluationScore[evaluationMeasuresList.size()]);
}

From source file:org.openml.webapplication.fantail.dc.DCUntils.java

License:Open Source License

public static double[] computeAttributeEntropy(Instances data) {
    List<Double> attributeEntropy = new ArrayList<Double>();
    for (int attIndex = 0; attIndex < data.numAttributes(); attIndex++) {

        if (data.attribute(attIndex).isNominal() && (data.classIndex() != attIndex)) {
            double[] attValueCounts = new double[data.numDistinctValues(attIndex)];

            for (int i = 0; i < data.numInstances(); i++) {
                Instance inst = data.instance(i);
                attValueCounts[(int) inst.value(attIndex)]++;
            }//  w ww  .ja  va  2  s  .  c o  m
            double attEntropy = 0;
            for (int c = 0; c < data.attribute(attIndex).numValues(); c++) {
                if (attValueCounts[c] > 0) {
                    double prob_c = attValueCounts[c] / data.numInstances();
                    attEntropy += prob_c * (Utils.log2(prob_c));
                }
            }
            attEntropy = attEntropy * -1.0;
            attributeEntropy.add(attEntropy);
        }
    }
    return ArrayUtils.toPrimitive(attributeEntropy.toArray(new Double[attributeEntropy.size()]));
}

From source file:org.openml.webapplication.fantail.dc.DCUntils.java

License:Open Source License

private static Instances[] splitData(Instances data, Attribute att) {

    Instances[] splitData = new Instances[att.numValues()];
    for (int j = 0; j < att.numValues(); j++) {
        splitData[j] = new Instances(data, data.numInstances());
    }/*from www.j a v a 2 s .c  o  m*/
    Enumeration<?> instEnum = data.enumerateInstances();
    while (instEnum.hasMoreElements()) {
        Instance inst = (Instance) instEnum.nextElement();
        splitData[(int) inst.value(att)].add(inst);
    }
    for (Instances splitData1 : splitData) {
        splitData1.compactify();
    }
    return splitData;
}

From source file:org.openml.webapplication.fantail.dc.statistical.Statistical.java

License:Open Source License

private static double findKurtosis(Instances instances, double mean, double stddev, int attrib) {
    final double S4 = Math.pow(stddev, 4), YBAR = mean;
    double sum = 0.0;
    final int COUNT = instances.numInstances();
    int n = 0;//www.ja v  a2s .  co  m

    if (S4 == 0) {
        return 0;
    }

    for (int i = 0; i < COUNT; i++) {
        Instance instance = instances.instance(i);
        if (!instance.isMissing(attrib)) {
            n++;
            sum += Math.pow(instance.value(attrib) - YBAR, 4);
        }
    }

    return (sum / ((n - 1) * S4)) - 3;
}

From source file:org.openml.webapplication.fantail.dc.statistical.Statistical.java

License:Open Source License

private static double findSkewness(Instances instances, double mean, double stddev, int attrib) {
    final double S3 = Math.pow(stddev, 3), YBAR = mean;
    double sum = 0.0;
    final int COUNT = instances.numInstances();
    int n = 0;//w  w  w  .ja  v a 2s.co  m

    if (S3 == 0) {
        return 0;
    }

    for (int i = 0; i < COUNT; i++) {
        Instance instance = instances.instance(i);
        if (!instance.isMissing(attrib)) {
            n++;
            sum += Math.pow(instance.value(attrib) - YBAR, 3);
        }
    }

    return (sum / ((n - 1) * S3));
}

From source file:org.openml.webapplication.predictionCounter.FoldsPredictionCounter.java

License:Open Source License

@SuppressWarnings("unchecked")
public FoldsPredictionCounter(Instances splits, String type, String shadowType) throws Exception {
    ATT_SPLITS_TYPE = InstancesHelper.getRowIndex("type", splits);
    ATT_SPLITS_ROWID = InstancesHelper.getRowIndex(new String[] { "rowid", "row_id" }, splits);
    ATT_SPLITS_REPEAT = InstancesHelper.getRowIndex(new String[] { "repeat", "repeat_nr" }, splits);
    ATT_SPLITS_FOLD = InstancesHelper.getRowIndex(new String[] { "fold", "fold_nr" }, splits);
    int att_splits_sample;
    try {//w  w w. j  a  v a 2 s .  co  m
        att_splits_sample = InstancesHelper.getRowIndex(new String[] { "sample", "sample_nr" }, splits);
    } catch (Exception e) {
        att_splits_sample = -1;
    }
    ATT_SPLITS_SAMPLE = att_splits_sample;

    NR_OF_REPEATS = splits.attribute("repeat") == null ? 1
            : (int) splits.attributeStats(ATT_SPLITS_REPEAT).numericStats.max + 1;
    NR_OF_FOLDS = splits.attribute("fold") == null ? 1
            : (int) splits.attributeStats(ATT_SPLITS_FOLD).numericStats.max + 1;
    NR_OF_SAMPLES = splits.attribute("sample") == null ? 1
            : (int) splits.attributeStats(ATT_SPLITS_SAMPLE).numericStats.max + 1;

    expectedTotal = 0;
    expected = new ArrayList[NR_OF_REPEATS][NR_OF_FOLDS][NR_OF_SAMPLES];
    actual = new ArrayList[NR_OF_REPEATS][NR_OF_FOLDS][NR_OF_SAMPLES];
    shadowTypeSize = new int[NR_OF_REPEATS][NR_OF_FOLDS][NR_OF_SAMPLES];
    for (int i = 0; i < NR_OF_REPEATS; i++)
        for (int j = 0; j < NR_OF_FOLDS; j++)
            for (int k = 0; k < NR_OF_SAMPLES; k++) {
                expected[i][j][k] = new ArrayList<Integer>();
                actual[i][j][k] = new ArrayList<Integer>();
            }

    for (int i = 0; i < splits.numInstances(); i++) {
        Instance instance = splits.instance(i);
        if (instance.value(ATT_SPLITS_TYPE) == splits.attribute(ATT_SPLITS_TYPE).indexOfValue(type)) {
            int repeat = (int) instance.value(ATT_SPLITS_REPEAT);
            int fold = (int) instance.value(ATT_SPLITS_FOLD);
            int sample = ATT_SPLITS_SAMPLE < 0 ? 0 : (int) instance.value(ATT_SPLITS_SAMPLE);
            int rowid = (int) instance.value(ATT_SPLITS_ROWID); //TODO: maybe we need instance.stringValue() ...
            expected[repeat][fold][sample].add(rowid);
            expectedTotal++;
        } else if (instance.value(ATT_SPLITS_TYPE) == splits.attribute(ATT_SPLITS_TYPE)
                .indexOfValue(shadowType)) {
            int repeat = (int) instance.value(ATT_SPLITS_REPEAT);
            int fold = (int) instance.value(ATT_SPLITS_FOLD);
            int sample = ATT_SPLITS_SAMPLE < 0 ? 0 : (int) instance.value(ATT_SPLITS_SAMPLE);

            shadowTypeSize[repeat][fold][sample]++;
        }
    }

    for (int i = 0; i < NR_OF_REPEATS; i++) {
        for (int j = 0; j < NR_OF_FOLDS; j++) {
            for (int k = 0; k < NR_OF_SAMPLES; k++) {
                Collections.sort(expected[i][j][k]);
            }
        }
    }

    error_message = "";
}

From source file:org.opentox.jaqpot3.qsar.InstancesUtil.java

License:Open Source License

/**
 * Accepts /*from w  ww  .  j a v  a2 s .co m*/
 * @param features
 * @param data
 * @param compoundURIposition
 *      Position where the compound URI should be placed. If set to <code>-1</code>
 *      the compound URI will not be included in the created dataset.
 * @return
 *      A subset of the provided dataset (parameter data in this method) with the
 *      features specified in the provided list with that exact order. The compound
 *      URI feature (string) is placed in the position specified by the parameter
 *      compoundURIposition.
 * @throws JaqpotException
 *      A JaqpotException is thrown with error code {@link ErrorCause#FeatureNotInDataset FeatureNotInDataset}
 *      in case you provide a feature that is not found in the sumbitted Instances.
 */
public static Instances sortByFeatureAttrList(List<String> features, final Instances data,
        int compoundURIposition) throws JaqpotException {
    int position = compoundURIposition > features.size() ? features.size() : compoundURIposition;
    if (compoundURIposition != -1) {
        features.add(position, "compound_uri");
    }
    FastVector vector = new FastVector(features.size());
    for (int i = 0; i < features.size(); i++) {
        String feature = features.get(i);
        Attribute attribute = data.attribute(feature);
        if (attribute == null) {
            throw new JaqpotException("The Dataset you provided does not contain feature:" + feature);
        }
        vector.addElement(attribute.copy());
    }
    Instances result = new Instances(data.relationName(), vector, 0);
    Enumeration instances = data.enumerateInstances();
    while (instances.hasMoreElements()) {
        Instance instance = (Instance) instances.nextElement();
        double[] vals = new double[features.size()];
        for (int i = 0; i < features.size(); i++) {
            vals[i] = instance.value(data.attribute(result.attribute(i).name()));
        }
        Instance in = new Instance(1.0, vals);
        result.add(in);
    }
    return result;
}

From source file:org.opentox.jaqpot3.qsar.InstancesUtil.java

License:Open Source License

public static Instances sortForPMMLModel(List<Feature> list, List<Integer> trFieldsAttrIndex,
        final Instances data, int compoundURIposition) throws JaqpotException {
    List<String> features = new ArrayList<String>();
    for (Feature feature : list) {
        features.add(feature.getUri().toString());
    }/* w w w .  ja v a 2s  . co  m*/

    int position = compoundURIposition > features.size() ? features.size() : compoundURIposition;
    if (compoundURIposition != -1) {
        features.add(position, "compound_uri");
    }
    FastVector vector = new FastVector(features.size());
    for (int i = 0; i < features.size(); i++) {
        String feature = features.get(i);
        Attribute attribute = data.attribute(feature);
        if (attribute == null) {
            throw new JaqpotException("The Dataset you provided does not contain feature:" + feature);
        }
        vector.addElement(attribute.copy());
    }
    int attributeSize = features.size();

    if (trFieldsAttrIndex.size() > 0) {
        for (int i = 0; i < trFieldsAttrIndex.size(); i++) {
            Attribute attribute = data.attribute(trFieldsAttrIndex.get(i));
            if (attribute == null) {
                throw new JaqpotException("The Dataset you provided does not contain this pmml feature");
            }
            vector.addElement(attribute.copy());
        }
        attributeSize += trFieldsAttrIndex.size();
    }

    Instances result = new Instances(data.relationName(), vector, 0);
    Enumeration instances = data.enumerateInstances();
    while (instances.hasMoreElements()) {
        Instance instance = (Instance) instances.nextElement();
        double[] vals = new double[attributeSize];
        for (int i = 0; i < attributeSize; i++) {
            vals[i] = instance.value(data.attribute(result.attribute(i).name()));
        }
        Instance in = new Instance(1.0, vals);
        result.add(in);
    }
    return result;
}

From source file:org.opentox.jaqpot3.qsar.predictor.FastRbfNnPredictor.java

License:Open Source License

private static double squaredNormDifference(Instance a, Instance b) {
    int numAttributes = a.numAttributes();
    if (numAttributes != b.numAttributes()) {
        throw new IllegalArgumentException("Provided instances of different length! "
                + "Squared Norm of the difference cannot be calculated!");
    }//w  ww. j  a  va 2 s . co m
    double sum = 0;
    for (int i = 0; i < numAttributes; i++) {
        sum += Math.pow(a.value(i) - b.value(i), 2);
    }
    return sum;
}