Example usage for weka.core Instances instance

List of usage examples for weka.core Instances instance

Introduction

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

Prototype



publicInstance instance(int index) 

Source Link

Document

Returns the instance at the given position.

Usage

From source file:decisiontree.MyC45.java

private double computeThreshold(Instances instances, Attribute attr) throws Exception {
    double[] threshold = new double[instances.numInstances()];
    double[] gainRatio = new double[instances.numInstances()];
    for (int i = 0; i < instances.numInstances() - 1; i++) {
        if (instances.instance(i).classValue() != instances.instance(i + 1).classValue()) {
            threshold[i] = (instances.instance(i).value(attr) + instances.instance(i + 1).value(attr)) / 2;
            gainRatio[i] = computeGainRatio(instances, attr, threshold[i]);
        }/*  w w w. ja va  2 s .co m*/
    }
    return (double) threshold[Utils.maxIndex(gainRatio)];
}

From source file:decisiontreeclassifier.ITree2.java

/********************************************************************
 * Changes the missing data to 0.0. For the voting data set, this 
 * should be sufficient seeing as 0.00 is more or less random.
 ********************************************************************/
public Instances fixMissingData(Instances iToFix) {
    for (int i = 0; i < iToFix.numInstances(); i++) {
        for (int j = 0; j < iToFix.numAttributes(); j++) {
            if (iToFix.instance(i).isMissing(j)) {
                iToFix.instance(i).setValue(j, 0.0);
            }/* www . ja v a2  s. c o  m*/
        }
    }
    return iToFix;
}

From source file:DetectorCreation.DetectorCreator.java

private static void ClassifyInstances(WekaTrainedClassifier trainedClassifier, Instances testset) {
    String classification;/*from   w ww  . j av a2s  .c om*/
    double classificationIndex;
    double[] classificationDist;
    Instance instance;
    for (int i = 0; i < testset.numInstances(); i++) {
        instance = testset.instance(i);
        //Print classification
        classification = trainedClassifier.GetClassification(instance);
        classificationIndex = trainedClassifier.GetClassificationIndex(instance);
        Console.PrintLine(String.format("Instance %s classification: %s", i + 1, classification));
        //Print classification distribution
        classificationDist = trainedClassifier.GetDistribution(instance);
        Console.PrintLine(String.format("Instance %s distribution: %s , %s", i + 1, classificationDist[0],
                classificationDist[1]));
        Console.PrintLine("");
    }
}

From source file:development.CrossValidateShapelets.java

public static void splitTrainData(Instances train, Instances[] trainFolds, Instances[] testFolds, int folds) {
    int size = train.numInstances();
    int foldSize = size / folds;
    int[] foldCV = new int[folds];
    for (int i = 0; i < foldCV.length; i++)
        foldCV[i] = foldSize;/*from ww w.j  a v a  2  s.com*/
    if (size % folds != 0) //Adjust the last fold size accordingly
        foldCV[folds - 1] = size - foldSize * (folds - 1);
    int diff = foldCV[folds - 1] - foldSize;
    int c = 0;
    while (diff > 0) { //Reassign elements to other folds

        foldCV[c % (folds - 1)]++;
        foldCV[folds - 1]--;
        diff = foldCV[folds - 1] - foldCV[c % (folds - 1)];
        c++;
    }
    Instances copy = new Instances(train);
    int start = 0;
    for (int i = 0; i < folds; i++) {
        trainFolds[i] = new Instances(copy, 0);
        testFolds[i] = new Instances(copy, 0);
        for (int j = 0; j < train.numInstances(); j++) {
            if (j < start || j >= start + foldCV[i])
                trainFolds[i].add(train.instance(j));
            else
                testFolds[i].add(train.instance(j));
        }
        start += foldCV[i];
    }
}

From source file:development.CrossValidateShapelets.java

public static Instances randomise(Instances train, int[] pos) {
    //Generate a random permutation into pos
    Random r = new Random();
    for (int i = 0; i < pos.length; i++)
        pos[i] = i;/*from w ww . j a  va  2s  . c o  m*/
    for (int i = 0; i < pos.length; i++) {
        int p1 = r.nextInt(pos.length);
        int p2 = r.nextInt(pos.length);
        int temp = pos[p1];
        pos[p1] = pos[p2];
        pos[p2] = temp;
    }
    Instances newD = new Instances(train, 0);
    for (int i = 0; i < pos.length; i++)
        newD.add(train.instance(pos[i]));
    return newD;
}

From source file:development.CrossValidateShapelets.java

public static int[][] classifyFold(String file, int fold) {

    String clusterPath = "/gpfs/sys/ajb/TSC Problems/" + file + "/";
    String desktopPath = "C:/Users/ajb/Dropbox/TSC Problems/" + file + "/";
    String path = desktopPath;/* w w  w .  jav a 2s.  c om*/
    if (useCluster)
        path = clusterPath;

    String filePath = path + "ShapeletCV/";

    //Check training and test files exist, terminate if not
    File tr = new File(filePath + file + "_TRAIN" + fold + ".arff");
    File ts = new File(filePath + file + "_TEST" + fold + ".arff");
    if (!tr.exists() || !ts.exists()) {
        System.err.println(" ERROR CLASSIFYING " + file + " fold " + fold + " file does not exist");
        return null;
    }
    //Check whether predictions exist, terminate if not.
    File r = new File(filePath + file + "Predictions" + fold + ".csv");
    if (r.exists()) {
        System.err.println(file + " fold " + fold + " Classificastion already done");
        return null;
    }

    Instances train = ClassifierTools.loadData(filePath + file + "_TRAIN" + fold);
    Instances test = ClassifierTools.loadData(filePath + file + "_TEST" + fold);
    ArrayList<String> names = new ArrayList<>();
    ArrayList<Classifier> c = setSingleClassifiers(names);
    HeterogeneousEnsemble hc = new HeterogeneousEnsemble(c);
    hc.useCVWeighting(true);

    int[][] preds = new int[2][test.numInstances()];
    try {
        hc.buildClassifier(train);
        for (int i = 0; i < test.numInstances(); i++) {
            preds[0][i] = (int) test.instance(i).classValue();
            preds[1][i] = (int) hc.classifyInstance(test.instance(i));
        }
    } catch (Exception ex) {
        Logger.getLogger(CrossValidateShapelets.class.getName()).log(Level.SEVERE, null, ex);
    }
    //Save results to the appropriate file
    double[] cvAccs = hc.getWeights();
    OutFile results = new OutFile(filePath + file + "Predictions" + fold + ".csv");
    for (int i = 0; i < cvAccs.length; i++)
        results.writeString(cvAccs[i] + ",");
    results.writeString("\n Actual,Predicted\n");
    int correct = 0;
    for (int i = 0; i < preds[0].length; i++) {
        results.writeString(preds[0][i] + "," + preds[1][i] + "\n");
        if (preds[0][i] == preds[1][i])
            correct++;
    }
    System.out.println(
            " Fold =" + fold + " correct =" + correct + " acc = " + ((double) correct) / preds[0].length);
    return preds;

}

From source file:distributed.core.DistributedUtils.java

License:Open Source License

public static NumericStats getNumericAttributeStatsSparse(Instances denormalized, int attIndex) {
    NumericStats ns = new NumericStats(denormalized.attribute(attIndex).name());

    for (int j = 0; j < denormalized.numInstances(); j++) {
        double value = denormalized.instance(j).value(attIndex);

        if (Utils.isMissingValue(value) || value == 0) {
            ns.getStats()[ArffSummaryNumericMetric.MISSING.ordinal()]++;
        } else {/* w ww  .ja  va2s  . co  m*/
            ns.getStats()[ArffSummaryNumericMetric.COUNT.ordinal()]++;
            ns.getStats()[ArffSummaryNumericMetric.SUM.ordinal()] += value;
            ns.getStats()[ArffSummaryNumericMetric.SUMSQ.ordinal()] += value * value;
            if (Double.isNaN(ns.getStats()[ArffSummaryNumericMetric.MIN.ordinal()])) {
                ns.getStats()[ArffSummaryNumericMetric.MIN
                        .ordinal()] = ns.getStats()[ArffSummaryNumericMetric.MAX.ordinal()] = value;
            } else if (value < ns.getStats()[ArffSummaryNumericMetric.MIN.ordinal()]) {
                ns.getStats()[ArffSummaryNumericMetric.MIN.ordinal()] = value;
            } else if (value > ns.getStats()[ArffSummaryNumericMetric.MAX.ordinal()]) {
                ns.getStats()[ArffSummaryNumericMetric.MAX.ordinal()] = value;
            }
        }
    }

    ns.computeDerived();

    return ns;
}

From source file:distributions.ClassdistributionNumeric.java

/**
 *
 * @param inst/* ww  w  . j  a  v a2 s  . co  m*/
 * @param classID
 * @param kID
 */
public ClassdistributionNumeric(Instances inst, int classID, int kID) {

    this.inst = new Instances(inst);

    this.classID = classID;
    this.kID = kID;

    attwerten = new double[inst.numInstances()];
    for (int k = 0; k < inst.numInstances(); k++) {
        attwerten[k] = inst.instance(k).value(kID);
    }

    attwerten = super.EliminiereDopelt(attwerten);
    /*for(int j=0;j<attwerten.length;j++){
     for (int i = 0; i < inst.numInstances(); i++) {
               
            
     attwerten[(int) inst.instance(i).value(kID)] = inst.instance(i).value(kID);
     }
            
     }*/

    probs = new double[attwerten.length];

}

From source file:distributions.NumericDistribution.java

public NumericDistribution(Instances inst, int kID) {

    this.inst = new Instances(inst);
    this.kID = kID;
    attwerten = new double[inst.numAttributes()];
    for (int i = 0, j = 0; i < attwerten.length && j < inst.numInstances(); i++, j++) {
        attwerten[i] = inst.instance(j).value(kID);
    }//from ww  w.  j  a va  2s  .  c  o m

    attwerten = super.EliminiereDopelt(attwerten);
    /* for (int k = 0; k < inst.numInstances(); k++) {
    attwerten[(int) inst.instance(k).value(kID)] = inst.instance(k).value(kID);
     }*/

    probs = new double[attwerten.length];

}

From source file:distributions.NumericNumericDistribution.java

public NumericNumericDistribution(Instances inst, int kID, int pID) {

    this.inst = new Instances(inst);
    attwerten1 = new double[inst.numInstances()];
    attwerten2 = new double[inst.numInstances()];
    for (int k = 0, j = 0; k < attwerten1.length && j < attwerten2.length; k++, j++) {
        attwerten2[k] = inst.instance(k).value(pID);
        attwerten1[j] = inst.instance(j).value(kID);
    }/* w w  w.  ja v a2s  .  com*/
    /* for (int i = 0; i < inst.numInstances(); i++) {
            
    attwerten1[(int) inst.instance(i).value(kID)] = inst.instance(i).value(kID);
            
            
    attwerten2[(int) inst.instance(i).value(pID)] = inst.instance(i).value(pID);
            
     }*/

    this.attwerten1 = super.EliminiereDopelt(this.attwerten1);
    this.attwerten2 = super.EliminiereDopelt(this.attwerten2);
    this.probs = new double[attwerten1.length][attwerten2.length];
    zaelt = new double[attwerten1.length][attwerten2.length];

}