Example usage for weka.core Instances add

List of usage examples for weka.core Instances add

Introduction

In this page you can find the example usage for weka.core Instances add.

Prototype



@Override
public void add(int index, Instance instance) 

Source Link

Document

Adds one instance at the given position in the list.

Usage

From source file:cyber009.udal.functions.StatisticalAnalysis.java

/**
 * /*from   w ww .j  a va2 s .c  om*/
 * @param classifier
 * @param trainingDataSet
 * @param unLabelDataSets
 * @param unLabelSet
 * @param classTarget
 * @return 
 */
public double conditionalEntropy(Classifier classifier, Instances trainingDataSet, Instances unLabelDataSets,
        Instance unLabelSet, double classTarget) {
    double cEnt = 0.0D;
    double entropy = 0.0D;
    unLabelSet.setClassValue(classTarget);
    trainingDataSet.add(trainingDataSet.numInstances(), unLabelSet);
    AttributeStats classStats = trainingDataSet.attributeStats(trainingDataSet.classIndex());
    for (Instance set : unLabelDataSets) {
        if (instanceCMPWithoutClass(set, unLabelSet) == true)
            continue;
        for (int i = 0; i < classStats.nominalCounts.length; i++) {
            double target = new Double(trainingDataSet.attribute(trainingDataSet.classIndex()).value(i));
            set.setClassValue(target);
            entropy = posteriorDistribution(classifier, trainingDataSet, set, classTarget);
            //System.out.println("entropy:"+entropy);
            cEnt += -(entropy) * Math.log10(entropy);
            set.setClassMissing();
        }
    }
    trainingDataSet.remove(trainingDataSet.numInstances() - 1);
    return cEnt;
}

From source file:lu.lippmann.cdb.ext.hydviga.cbr.GapFillingKnowledgeDB.java

License:Open Source License

public static Instances findSimilarCases(final String attrname, final double x, final double y, final int year,
        final String season, final int gapSize, final int gapPosition, final boolean isDuringRising,
        final boolean hasDownstream, final boolean hasUpstream, final String flow) throws Exception {
    /* build the current case */
    final StringBuilder newsb = new StringBuilder(DATABASE_AS_STRINGBUILDER);
    newsb.append(attrname).append(",").append(x).append(",").append(y).append(",")

            .append(gapSize).append(",").append(gapPosition).append(",")

            .append(season).append(",").append(year).append(",")

            .append(isDuringRising).append(",").append(flow).append(",")

            .append(hasDownstream).append(",").append(hasUpstream).append(",")

            .append("?").append(",").append("?").append(",").append("?").append(",").append("?").append(",")
            .append("?").append(",").append("?").append(",").append(0) // MAE
            .append(",").append(0) // RMSE
            .append(",").append(0) // RSR
            .append(",").append(0) // PBIAS
            .append(",").append(1) // NS
            .append(",").append(1) // IOA
            .append(",").append(true) // BEST SOLUTION          
            .append("\n");
    final Instances tmpDB = WekaDataAccessUtil.loadInstancesFromCSVString(newsb.toString(), false);

    final Instance newcase = tmpDB.instance(tmpDB.numInstances() - 1);

    /* compute NN for the current case */
    final Instances knn = WekaMachineLearningUtil.computeNearestNeighbours(tmpDB, newcase, 10,
            "2,3,4,6,7,8,9,10,23");
    knn.add(0, newcase);

    System.out.println(knn.toSummaryString());

    return knn;//from  w w w  .  jav  a 2s . com

}