Example usage for weka.core Instances numInstances

List of usage examples for weka.core Instances numInstances

Introduction

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

Prototype


publicint numInstances() 

Source Link

Document

Returns the number of instances in the dataset.

Usage

From source file:examples.Pair.java

License:Open Source License

public static Pair<Instances, Instances> seprateTestAndTrainingSets(Instances instances, double probability) {
    Instances trainingSet = new Instances(instances, 0, 0);
    Instances testSet = new Instances(instances, 0, 0);
    Random rand = new Random();
    rand.setSeed(1L);//from  ww  w . j av  a2  s  .com

    for (int i = 0; i < instances.numInstances(); i++) {
        Instance instance = instances.instance(i);
        if (rand.nextDouble() > probability) {
            testSet.add(instance);
        } else {
            trainingSet.add(instance);
        }
    }

    return new Pair<Instances, Instances>(trainingSet, testSet);
}

From source file:explorer.ChordalysisModelling.java

License:Open Source License

public void buildModelNoExplore(Instances dataset) {
    this.nbInstances = dataset.numInstances();
    this.dataset = dataset;
    int[] variables = new int[dataset.numAttributes()];
    int[] nbValuesForAttribute = new int[variables.length];
    for (int i = 0; i < variables.length; i++) {
        variables[i] = i;// ww w. j  a va  2 s. c om
        nbValuesForAttribute[i] = dataset.numDistinctValues(i);
    }
    this.lattice = new Lattice(dataset);
    this.entropyComputer = new EntropyComputer(dataset.numInstances(), this.lattice);
    this.scorer = new GraphActionScorerPValue(nbInstances, entropyComputer);
    this.bestModel = new DecomposableModel(variables, nbValuesForAttribute);
    this.pq = new MyPriorityQueue(variables.length, bestModel, scorer);
    for (int i = 0; i < variables.length; i++) {
        for (int j = i + 1; j < variables.length; j++) {
            pq.enableEdge(i, j);
        }
    }

}

From source file:explorer.ChordalysisModellingMML.java

License:Open Source License

public void buildModelNoExplore(Instances dataset) {
    this.nbInstances = dataset.numInstances();
    this.dataset = dataset;
    int[] variables = new int[dataset.numAttributes()];
    int[] nbValuesForAttribute = new int[variables.length];
    for (int i = 0; i < variables.length; i++) {
        variables[i] = i;//from www  . j a  v  a 2s  . c o  m
        nbValuesForAttribute[i] = dataset.numDistinctValues(i);
    }
    this.lattice = new Lattice(dataset);
    this.computer = new MessageLengthFactorialComputer(dataset.numInstances(), this.lattice);
    this.scorer = new GraphActionScorerMML(nbInstances, computer);
    this.bestModel = new DecomposableModel(variables, nbValuesForAttribute);
    this.pq = new MyPriorityQueue(variables.length, bestModel, scorer);
    for (int i = 0; i < variables.length; i++) {
        for (int j = i + 1; j < variables.length; j++) {
            pq.enableEdge(i, j);
        }
    }

}

From source file:expshell.NeuralClassifier.java

@Override
public void buildClassifier(Instances i) throws Exception {
    List<Integer> numNodes = new ArrayList<Integer>();
    //numNodes.add(5);
    //numNodes.add(6);
    numNodes.add(i.numClasses());//  w ww  .  j a v  a2s .c  o m

    nn = new NeuralNetwork(numNodes.size(), i.numAttributes() - 1, numNodes);
    for (int j = 0; j < i.numInstances(); j++) {
        System.out.println(nn.run(i.instance(j)));
    }

}

From source file:fantail.algorithms.AbstractRanker.java

License:Open Source License

public static double[] getAvgRankValues(Instances data) throws Exception {

    if (data.numInstances() == 0) {
        throw new Exception("data can't be empty.");
    }//w  w w. ja  v  a 2 s. c o m
    int numLabels = Tools.getNumberTargets(data);
    double[] avgVals = new double[numLabels];
    for (int m = 0; m < data.numInstances(); m++) {
        Instance inst = data.instance(m);
        double[] targetValues = Tools.getTargetVector(inst);

        for (int j = 0; j < targetValues.length; j++) {
            avgVals[j] += (targetValues[j] * inst.weight());
        }
    }
    for (int i = 0; i < avgVals.length; i++) {
        avgVals[i] /= data.numInstances();
    }
    return avgVals;
}

From source file:fantail.algorithms.AverageRanking.java

License:Open Source License

@Override
public void buildRanker(Instances data) throws Exception {
    Instances workingData = new Instances(data);
    int numLabels = Tools.getNumberTargets(workingData);
    m_DefRanking = new double[numLabels];
    for (int m = 0; m < workingData.numInstances(); m++) {
        Instance inst = workingData.instance(m);
        double[] targetValues = Tools.getTargetVector(inst);
        for (int j = 0; j < targetValues.length; j++) {
            m_DefRanking[j] += (targetValues[j]);
        }//  w  w  w . j  a v  a2 s .com
    }
}

From source file:fantail.algorithms.BinaryART.java

License:Open Source License

private double estimateR2(Instances data, int attIndex, double splitPoint) throws Exception {
    Instances[] P = splitData(data, attIndex, splitPoint);
    double m = m_NumObjects;
    double sum1 = 0;
    for (int l = 0; l < P.length; l++) {
        //double estimatedCenterRanking[] = AbstractRanker.getCenterRanking(P[l], m_ApproxCenterMethod);          
        // paper version
        double estimatedCenterRanking[] = AbstractRanker.getAvgRankValues(P[l]);
        //double estimatedCenterRanking2[] = AbstractRanker.getAvgRanking(P[l]); // working version
        int n_L = P[l].numInstances();
        double avgDistance_L = estimateAvgDistanceSpearman(m, estimatedCenterRanking, estimatedCenterRanking);
        sum1 += n_L * avgDistance_L;/*from   w  ww. j  a v  a2  s  . co  m*/
    }
    //
    double sum2 = 0;

    //double estimatedCenterRankingT[] = AbstractRanker.getCenterRanking(data, m_ApproxCenterMethod);
    double estimatedCenterRankingT[] = AbstractRanker.getAvgRankValues(data);
    double avgDistance_L = estimateAvgDistanceSpearman(m, estimatedCenterRankingT, estimatedCenterRankingT);

    //for (int l = 0; l < P.length; l++) {
    //double avgRankValues[] = AbstractRanker.getAvgRankValues(P[l]);
    //int n_L = P[l].numInstances();
    //double avgDistance_L = estimateAvgDistanceSpearman(P[l], m, estimatedCenterRankingT, avgRankValues);
    //sum2 += n_L * avgDistance_L;
    //}
    sum2 = data.numInstances() * avgDistance_L;
    //double r2 = 1.0 - sum1 / (sum2 + 0.00000001); // paper version
    double r2 = 1.0 - (sum1) / (sum2 + 0.0000); // working version 02 July 2013
    //double r2 = 1.0 - sum2 / (sum1 + 0.00000001);  // works well for larger label sets

    //System.out.println(r2);
    return r2;
}

From source file:fantail.algorithms.BinaryART.java

License:Open Source License

private Instances[] splitData(Instances data, int attIndex, double splitPoint) throws Exception {

    Instances[] subsets = new Instances[2];
    subsets[0] = new Instances(data, 0);
    subsets[1] = new Instances(data, 0);

    // changed on 7 Feb 2013, because for some LR datasets, the Alpo returns NaN
    int halfPoint = (int) (data.numInstances() * 0.50);
    for (int i = 0; i < data.numInstances(); i++) {
        Instance inst = data.instance(i);
        if (inst.value(attIndex) <= splitPoint && subsets[0].numInstances() < halfPoint) {
            subsets[0].add(inst);/*from  w  ww .  ja v a  2s .  c om*/
        } else {
            subsets[1].add(inst);
        }
    }

    if (subsets[1].numInstances() == 0) {
        subsets[1].add(subsets[0].instance(0));
    }

    if (subsets[0].numInstances() == 0) {
        subsets[0].add(subsets[1].instance(0));
    }
    return subsets;

    // following were used before 7 Feb 2013
    //        for (int i = 0; i < data.numInstances(); i++) {
    //            Instance inst = data.instance(i);
    //            if (inst.value(attIndex) <= splitPoint) {
    //                subsets[0].add(inst);
    //            } else {
    //                subsets[1].add(inst);
    //            }
    //        }
    //
    //        if (subsets[1].numInstances() == 0) {
    //            subsets[1].add(subsets[0].instance(0));
    //        }
    //
    //        if (subsets[0].numInstances() == 0) {
    //            subsets[0].add(subsets[1].instance(0));
    //        }
    //        return subsets;
}

From source file:fantail.algorithms.BinaryART.java

License:Open Source License

private double getMedian2(Instances data, int attIndex) throws Exception {
    double[] numArray = new double[data.numInstances()];
    for (int i = 0; i < data.numInstances(); i++) {
        Instance inst = data.instance(i);
        numArray[i] = inst.value(attIndex);
    }// w  ww .  java  2s . c o m
    Arrays.sort(numArray);
    double median;
    if (numArray.length % 2 == 0) {
        median = ((double) numArray[numArray.length / 2] + (double) numArray[numArray.length / 2 + 1]) / 2;
    } else {
        median = (double) numArray[numArray.length / 2];
    }
    return median;
}

From source file:fantail.algorithms.BinaryART.java

License:Open Source License

private double getMedian(Instances data, int attIndex) throws Exception {

    if (false) {//from  www  .j  av  a 2  s  .  co  m
        return getMedian2(data, attIndex); // added 07-july 2013; actually they are the same
        // removed 17/07/2013 
    }

    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (int i = 0; i < data.numInstances(); i++) {
        Instance inst = (Instance) data.instance(i);
        stats.addValue(inst.value(attIndex));
    }
    double median = stats.getPercentile(50);
    return median;
}