Example usage for weka.core Instance weight

List of usage examples for weka.core Instance weight

Introduction

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

Prototype

public double weight();

Source Link

Document

Returns the instance's weight.

Usage

From source file:tr.gov.ulakbim.jDenetX.tasks.EvaluateModel.java

License:Open Source License

@Override
public Object doMainTask(TaskMonitor monitor, ObjectRepository repository) {
    Classifier model = (Classifier) getPreparedClassOption(this.modelOption);
    InstanceStream stream = (InstanceStream) getPreparedClassOption(this.streamOption);
    ClassificationPerformanceEvaluator evaluator = (ClassificationPerformanceEvaluator) getPreparedClassOption(
            this.evaluatorOption);
    int maxInstances = this.maxInstancesOption.getValue();
    long instancesProcessed = 0;
    monitor.setCurrentActivity("Evaluating model...", -1.0);
    while (stream.hasMoreInstances() && ((maxInstances < 0) || (instancesProcessed < maxInstances))) {
        Instance testInst = (Instance) stream.nextInstance().copy();
        int trueClass = (int) testInst.classValue();
        testInst.setClassMissing();//  w w  w.  j a  v  a  2s. c o  m
        double[] prediction = model.getVotesForInstance(testInst);
        evaluator.addClassificationAttempt(trueClass, prediction, testInst.weight());
        instancesProcessed++;
        if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
            if (monitor.taskShouldAbort()) {
                return null;
            }
            long estimatedRemainingInstances = stream.estimatedRemainingInstances();
            if (maxInstances > 0) {
                long maxRemaining = maxInstances - instancesProcessed;
                if ((estimatedRemainingInstances < 0) || (maxRemaining < estimatedRemainingInstances)) {
                    estimatedRemainingInstances = maxRemaining;
                }
            }
            monitor.setCurrentActivityFractionComplete(estimatedRemainingInstances < 0 ? -1.0
                    : (double) instancesProcessed
                            / (double) (instancesProcessed + estimatedRemainingInstances));
            if (monitor.resultPreviewRequested()) {
                monitor.setLatestResultPreview(new LearningEvaluation(evaluator.getPerformanceMeasurements()));
            }
        }
    }
    return new LearningEvaluation(evaluator.getPerformanceMeasurements());
}

From source file:tr.gov.ulakbim.jDenetX.tasks.EvaluatePrequential.java

License:Open Source License

@Override
protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) {
    Classifier learner = (Classifier) getPreparedClassOption(this.learnerOption);
    InstanceStream stream = (InstanceStream) getPreparedClassOption(this.streamOption);
    ClassificationPerformanceEvaluator evaluator = (ClassificationPerformanceEvaluator) getPreparedClassOption(
            this.evaluatorOption);
    //New for prequential methods
    if (evaluator instanceof WindowClassificationPerformanceEvaluator) {
        ((WindowClassificationPerformanceEvaluator) evaluator).setWindowWidth(widthOption.getValue());
    }/*from ww w  .ja  v  a 2s  .c om*/
    if (evaluator instanceof EWMAClassificationPerformanceEvaluator) {
        ((EWMAClassificationPerformanceEvaluator) evaluator).setalpha(alphaOption.getValue());
    }
    if (evaluator instanceof FadingFactorClassificationPerformanceEvaluator) {
        ((FadingFactorClassificationPerformanceEvaluator) evaluator).setalpha(alphaOption.getValue());
    }
    //End New for prequential methods

    learner.setModelContext(stream.getHeader());
    int maxInstances = this.instanceLimitOption.getValue();
    long instancesProcessed = 0;
    int maxSeconds = this.timeLimitOption.getValue();
    int secondsElapsed = 0;
    monitor.setCurrentActivity("Evaluating learner...", -1.0);
    LearningCurve learningCurve = new LearningCurve("learning evaluation instances");
    File dumpFile = this.dumpFileOption.getFile();
    PrintStream immediateResultStream = null;
    if (dumpFile != null) {
        try {
            if (dumpFile.exists()) {
                immediateResultStream = new PrintStream(new FileOutputStream(dumpFile, true), true);
            } else {
                immediateResultStream = new PrintStream(new FileOutputStream(dumpFile), true);
            }
        } catch (Exception ex) {
            throw new RuntimeException("Unable to open immediate result file: " + dumpFile, ex);
        }
    }
    //File for output predictions
    File outputPredictionFile = this.outputPredictionFileOption.getFile();
    PrintStream outputPredictionResultStream = null;
    if (outputPredictionFile != null) {
        try {
            if (outputPredictionFile.exists()) {
                outputPredictionResultStream = new PrintStream(new FileOutputStream(outputPredictionFile, true),
                        true);
            } else {
                outputPredictionResultStream = new PrintStream(new FileOutputStream(outputPredictionFile),
                        true);
            }
        } catch (Exception ex) {
            throw new RuntimeException("Unable to open prediction result file: " + outputPredictionFile, ex);
        }
    }
    boolean firstDump = true;
    boolean preciseCPUTiming = TimingUtils.enablePreciseTiming();
    long evaluateStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
    long lastEvaluateStartTime = evaluateStartTime;
    double RAMHours = 0.0;
    while (stream.hasMoreInstances() && ((maxInstances < 0) || (instancesProcessed < maxInstances))
            && ((maxSeconds < 0) || (secondsElapsed < maxSeconds))) {
        Instance trainInst = stream.nextInstance();
        Instance testInst = (Instance) trainInst.copy();
        int trueClass = (int) trainInst.classValue();
        testInst.setClassMissing();
        double[] prediction = learner.getVotesForInstance(testInst);
        // Output prediction
        if (outputPredictionFile != null) {
            outputPredictionResultStream.println(Utils.maxIndex(prediction) + "," + trueClass);
        }

        evaluator.addClassificationAttempt(trueClass, prediction, testInst.weight());
        learner.trainOnInstance(trainInst);
        instancesProcessed++;
        if (instancesProcessed % this.sampleFrequencyOption.getValue() == 0) {
            long evaluateTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
            double time = TimingUtils.nanoTimeToSeconds(evaluateTime - evaluateStartTime);
            double timeIncrement = TimingUtils.nanoTimeToSeconds(evaluateTime - lastEvaluateStartTime);
            double RAMHoursIncrement = learner.measureByteSize() / (1024.0 * 1024.0 * 1024.0); //GBs
            RAMHoursIncrement *= (timeIncrement / 3600.0); //Hours
            RAMHours += RAMHoursIncrement;
            lastEvaluateStartTime = evaluateTime;
            learningCurve.insertEntry(new LearningEvaluation(
                    new Measurement[] { new Measurement("learning evaluation instances", instancesProcessed),
                            new Measurement("evaluation time (" + (preciseCPUTiming ? "cpu " : "") + "seconds)",
                                    time),
                            new Measurement("model cost (RAM-Hours)", RAMHours) },
                    evaluator, learner));

            if (immediateResultStream != null) {
                if (firstDump) {
                    immediateResultStream.println(learningCurve.headerToString());
                    firstDump = false;
                }
                immediateResultStream.println(learningCurve.entryToString(learningCurve.numEntries() - 1));
                immediateResultStream.flush();
            }
        }
        if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
            if (monitor.taskShouldAbort()) {
                return null;
            }
            long estimatedRemainingInstances = stream.estimatedRemainingInstances();
            if (maxInstances > 0) {
                long maxRemaining = maxInstances - instancesProcessed;
                if ((estimatedRemainingInstances < 0) || (maxRemaining < estimatedRemainingInstances)) {
                    estimatedRemainingInstances = maxRemaining;
                }
            }
            monitor.setCurrentActivityFractionComplete(estimatedRemainingInstances < 0 ? -1.0
                    : (double) instancesProcessed
                            / (double) (instancesProcessed + estimatedRemainingInstances));
            if (monitor.resultPreviewRequested()) {
                monitor.setLatestResultPreview(learningCurve.copy());
            }
            secondsElapsed = (int) TimingUtils
                    .nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread() - evaluateStartTime);
        }
    }
    if (immediateResultStream != null) {
        immediateResultStream.close();
    }
    if (outputPredictionResultStream != null) {
        outputPredictionResultStream.close();
    }
    return learningCurve;
}

From source file:wekimini.learning.LinearRegressionAttributeTransformer.java

@Override
public Instance convertInstance(Instance instance) throws Exception {
    double[] newVals = new double[numInputs * exponent + 1];
    int next = 0;
    for (int i = 0; i < instance.numAttributes() - 1; i++) {
        for (int j = 1; j <= exponent; j++) {
            newVals[next] = Math.pow(instance.value(i), j);
            next++;/*from w ww.  j  a  v  a  2  s.c  o m*/
        }
    }

    //Now add class:
    newVals[newVals.length - 1] = instance.classValue();

    //Convert:
    return new Instance(instance.weight(), newVals);
}

From source file:zhaop.textmining.proj.MyStringToWordVector.java

License:Open Source License

/**
 * Converts the instance w/o normalization.
 * /*from  w ww .j  av  a  2  s . co m*/
 * @oaram instance the instance to convert
 * @param v
 * @return the conerted instance
 */
private int convertInstancewoDocNorm(Instance instance, FastVector v) {

    // Convert the instance into a sorted set of indexes
    TreeMap contained = new TreeMap();

    // Copy all non-converted attributes from input to output
    int firstCopy = 0;
    for (int i = 0; i < getInputFormat().numAttributes(); i++) {
        if (!m_SelectedRange.isInRange(i)) {
            if (getInputFormat().attribute(i).type() != Attribute.STRING) {
                // Add simple nominal and numeric attributes directly
                if (instance.value(i) != 0.0) {
                    contained.put(new Integer(firstCopy), new Double(instance.value(i)));
                }
            } else {
                if (instance.isMissing(i)) {
                    contained.put(new Integer(firstCopy), new Double(Instance.missingValue()));
                } else {

                    // If this is a string attribute, we have to first add
                    // this value to the range of possible values, then add
                    // its new internal index.
                    if (outputFormatPeek().attribute(firstCopy).numValues() == 0) {
                        // Note that the first string value in a
                        // SparseInstance doesn't get printed.
                        outputFormatPeek().attribute(firstCopy)
                                .addStringValue("Hack to defeat SparseInstance bug");
                    }
                    int newIndex = outputFormatPeek().attribute(firstCopy)
                            .addStringValue(instance.stringValue(i));
                    contained.put(new Integer(firstCopy), new Double(newIndex));
                }
            }
            firstCopy++;
        }
    }

    for (int j = 0; j < instance.numAttributes(); j++) {
        // if ((getInputFormat().attribute(j).type() == Attribute.STRING)
        if (m_SelectedRange.isInRange(j) && (instance.isMissing(j) == false)) {

            m_Tokenizer.tokenize(instance.stringValue(j));

            while (m_Tokenizer.hasMoreElements()) {
                String word = (String) m_Tokenizer.nextElement();
                if (this.m_lowerCaseTokens == true)
                    word = word.toLowerCase();
                word = m_Stemmer.stem(word);
                Integer index = (Integer) m_Dictionary.get(word);
                if (index != null) {
                    if (m_OutputCounts) { // Separate if here rather than two lines down
                                          // to avoid hashtable lookup
                        Double count = (Double) contained.get(index);
                        if (count != null) {
                            contained.put(index, new Double(count.doubleValue() + 1.0));
                        } else {
                            contained.put(index, new Double(1));
                        }
                    } else {
                        contained.put(index, new Double(1));
                    }
                }
            }
        }
    }

    // Doing TFTransform
    if (m_TFTransform == true) {
        Iterator it = contained.keySet().iterator();
        for (int i = 0; it.hasNext(); i++) {
            Integer index = (Integer) it.next();
            if (index.intValue() >= firstCopy) {
                double val = ((Double) contained.get(index)).doubleValue();
                val = Math.log(val + 1);
                contained.put(index, new Double(val));
            }
        }
    }

    // Doing IDFTransform
    if (m_IDFTransform == true) {
        Iterator it = contained.keySet().iterator();
        for (int i = 0; it.hasNext(); i++) {
            Integer index = (Integer) it.next();
            if (index.intValue() >= firstCopy) {
                double val = ((Double) contained.get(index)).doubleValue();
                val = val * Math.log(m_NumInstances / (double) m_DocsCounts[index.intValue()]);
                contained.put(index, new Double(val));
            }
        }
    }

    // Convert the set to structures needed to create a sparse instance.
    double[] values = new double[contained.size()];
    int[] indices = new int[contained.size()];
    Iterator it = contained.keySet().iterator();
    for (int i = 0; it.hasNext(); i++) {
        Integer index = (Integer) it.next();
        Double value = (Double) contained.get(index);
        values[i] = value.doubleValue();
        indices[i] = index.intValue();
    }

    Instance inst = new SparseInstance(instance.weight(), values, indices, outputFormatPeek().numAttributes());
    inst.setDataset(outputFormatPeek());

    v.addElement(inst);

    return firstCopy;
}