Example usage for weka.core Instances get

List of usage examples for weka.core Instances get

Introduction

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

Prototype



@Override
publicInstance get(int index) 

Source Link

Document

Returns the instance at the given position.

Usage

From source file:machinelearningproject.Tree.java

public double calculateEntropy(Instances instances, int attrIdx) {
    HashMap<String, Integer> classMap = new HashMap<>();
    double entropy = (double) 0;
    int numInstances = instances.size();

    for (int i = 0; i < numInstances; i++) {
        Instance instance = instances.get(i);
        String key = instance.stringValue(attrIdx);
        if (classMap.isEmpty() || !classMap.containsKey(key)) {
            classMap.put(key, 1);//from ww w.  j  a  v a2  s. co  m
        } else {
            if (classMap.containsKey(key)) {
                classMap.put(key, classMap.get(key) + 1);
            }
        }
    }

    Iterator<String> keySetIterator = classMap.keySet().iterator();
    while (keySetIterator.hasNext()) {
        String key = keySetIterator.next();
        // reference source code http://onoffswitch.net/building-decision-tree/
        double prob = (double) classMap.get(key) / (double) numInstances;
        entropy -= prob * (Math.log(prob) / Math.log(2));
    }

    return entropy;
}

From source file:machinelearningproject.Tree.java

public double calculateInformationGain(Instances instances, int attrIdx, int classIdx) throws Exception {
    HashMap<String, Integer> attrCount = new HashMap<>();
    HashMap<String, Integer> attrClassCount = new HashMap<>();
    int numInstances = instances.size();

    for (int i = 0; i < numInstances; i++) {
        Instance instance = instances.get(i);

        String attrKey = instance.stringValue(attrIdx);
        if (attrCount.isEmpty() || !attrCount.containsKey(attrKey)) {
            attrCount.put(attrKey, 1);//from  www .jav a 2  s.  c  o  m
        } else {
            if (attrCount.containsKey(attrKey)) {
                attrCount.put(attrKey, attrCount.get(attrKey) + 1);
            }
        }

        String attrClassKey = instance.stringValue(attrIdx) + "-" + instance.stringValue(classIdx);
        if (attrClassCount.isEmpty() || !attrClassCount.containsKey(attrClassKey)) {
            attrClassCount.put(attrClassKey, 1);
        } else {
            if (attrClassCount.containsKey(attrClassKey)) {
                attrClassCount.put(attrClassKey, attrClassCount.get(attrClassKey) + 1);
            }
        }
    }
    double attrEntropy = (double) 0;

    Iterator<String> attrKeySetIterator = attrCount.keySet().iterator();
    while (attrKeySetIterator.hasNext()) {
        String attrKey = attrKeySetIterator.next();
        double bufferEntropy = (double) 0;
        Iterator<String> keySetIterator = attrClassCount.keySet().iterator();
        while (keySetIterator.hasNext()) {
            String key = keySetIterator.next();
            String[] keys = key.split("-");
            String attrValue = keys[0];
            if (attrKey.equals(attrValue)) {
                double prob = (double) attrClassCount.get(key) / (double) attrCount.get(attrKey);
                bufferEntropy -= prob * (Math.log(prob) / Math.log(2));
            }
        }
        attrEntropy += (attrCount.get(attrKey) / (double) numInstances) * bufferEntropy;
    }
    double classEntropy = calculateEntropy(instances, classIdx);

    return (classEntropy - attrEntropy);
}

From source file:machinelearningproject.Tree.java

public Tree buildTree(Instances instances) throws Exception {
    Tree tree = new Tree();
    ArrayList<String> availableAttributes = new ArrayList();

    int largestInfoGainAttrIdx = -1;
    double largestInfoGainAttrValue = 0.0;

    for (int idx = 0; idx < instances.numAttributes(); idx++) {
        if (idx != instances.classIndex()) {
            availableAttributes.add(instances.attribute(idx).name());
        }/* w w w.  j  a  va  2 s. co m*/
    }

    if (instances.numInstances() == 0) {
        return null;
    } else if (calculateClassEntropy(instances) == 0.0) {
        // all examples have the sama classification
        tree.attributeName = instances.get(0).stringValue(instances.classIndex());
    } else if (availableAttributes.isEmpty()) {
        // mode classification
        tree.attributeName = getModeClass(instances, instances.classIndex());
    } else {
        for (int idx = 0; idx < instances.numAttributes(); idx++) {
            if (idx != instances.classIndex()) {
                double attrInfoGain = calculateInformationGain(instances, idx, instances.classIndex());
                if (largestInfoGainAttrValue < attrInfoGain) {
                    largestInfoGainAttrIdx = idx;
                    largestInfoGainAttrValue = attrInfoGain;
                }
            }
        }

        if (largestInfoGainAttrIdx != -1) {
            tree.attributeName = instances.attribute(largestInfoGainAttrIdx).name();
            ArrayList<String> attrValues = new ArrayList();
            for (int i = 0; i < instances.numInstances(); i++) {
                Instance instance = instances.get(i);
                String attrValue = instance.stringValue(largestInfoGainAttrIdx);
                if (attrValues.isEmpty() || !attrValues.contains(attrValue)) {
                    attrValues.add(attrValue);
                }
            }

            for (String attrValue : attrValues) {
                Node node = new Node(attrValue);
                Instances copyInstances = new Instances(instances);
                copyInstances.setClassIndex(instances.classIndex());
                int i = 0;
                while (i < copyInstances.numInstances()) {
                    Instance instance = copyInstances.get(i);
                    // reducing examples
                    if (!instance.stringValue(largestInfoGainAttrIdx).equals(attrValue)) {
                        copyInstances.delete(i);
                        i--;
                    }
                    i++;
                }
                copyInstances.deleteAttributeAt(largestInfoGainAttrIdx);
                node.subTree = buildTree(copyInstances);
                tree.nodes.add(node);
            }
        }
    }

    return tree;
}

From source file:machinelearning_cw.MachineLearning_CW.java

/**
 * @param args the command line arguments
 *//*from www  . j a  va  2  s.co  m*/
public static void main(String[] args) throws Exception {
    // TODO code application logic here

    /* Initializing test datasets */
    ArrayList<Instances> trainData = new ArrayList<Instances>();
    ArrayList<Instances> testData = new ArrayList<Instances>();

    Instances train = WekaLoader.loadData("PitcherTrain.arff");
    Instances test = WekaLoader.loadData("PitcherTest.arff");
    trainData.add(train);
    testData.add(test);

    Instances bananaTrain = WekaLoader.loadData("banana-train.arff");
    Instances bananaTest = WekaLoader.loadData("banana-test.arff");
    trainData.add(bananaTrain);
    testData.add(bananaTest);

    Instances cloudTrain = WekaLoader.loadData("clouds-train.arff");
    Instances cloudTest = WekaLoader.loadData("clouds-test.arff");
    trainData.add(cloudTrain);
    testData.add(cloudTest);

    Instances concentricTrain = WekaLoader.loadData("concentric-train.arff");
    Instances concentricTest = WekaLoader.loadData("concentric-test.arff");
    trainData.add(concentricTrain);
    testData.add(concentricTest);

    // 3 dimensional data set
    Instances habermanTrain = WekaLoader.loadData("haberman-train.arff");
    Instances habermanTest = WekaLoader.loadData("haberman-test.arff");
    trainData.add(habermanTrain);
    testData.add(habermanTest);

    // >3 dimensional data sets
    Instances thyroidTrain = WekaLoader.loadData("thyroid-train.arff");
    Instances thyroidTest = WekaLoader.loadData("thyroid-test.arff");
    trainData.add(thyroidTrain);
    testData.add(thyroidTest);

    Instances heartTrain = WekaLoader.loadData("heart-train.arff");
    Instances heartTest = WekaLoader.loadData("heart-test.arff");
    trainData.add(heartTrain);
    testData.add(heartTest);

    Instances liverTrain = WekaLoader.loadData("liver-train.arff");
    Instances liverTest = WekaLoader.loadData("liver-test.arff");
    trainData.add(liverTrain);
    testData.add(liverTest);

    Instances pendigitisTrain = WekaLoader.loadData("pendigitis-train.arff");
    Instances pendigitisTest = WekaLoader.loadData("pendigitis-test.arff");
    trainData.add(pendigitisTrain);
    testData.add(pendigitisTest);

    Instances phonemeTrain = WekaLoader.loadData("phoneme-train.arff");
    Instances phonemeTest = WekaLoader.loadData("phoneme-test.arff");
    trainData.add(phonemeTrain);
    testData.add(phonemeTest);

    Instances yeastTrain = WekaLoader.loadData("yeast-train.arff");
    Instances yeastTest = WekaLoader.loadData("yeast-test.arff");
    trainData.add(yeastTrain);
    testData.add(yeastTest);

    /* Test to see that BasicKNN provides the same results obtained from
     * the hand exercise.
     */
    System.out.println(
            "Test to see that BasicKNN provides the same" + " results obtained from the hand exercise:");
    System.out.println("(Ties are settled randomly)");
    BasicKNN basicKNN = new BasicKNN();
    basicKNN.buildClassifier(train);
    for (int i = 0; i < test.size(); i++) {
        Instance inst = test.get(i);
        System.out.println(i + 1 + ": " + basicKNN.classifyInstance(inst));
    }

    /* Initializing alternative classifiers */
    IBk wekaKNN = new IBk();
    NaiveBayes naiveBayes = new NaiveBayes();
    J48 decisionTree = new J48();
    SMO svm = new SMO();

    /* Tests for experiments 1,2 & 3 */
    KNN myKNN = new KNN();
    myKNN.setUseStandardisedAttributes(true);
    myKNN.setAutoDetermineK(false);
    myKNN.setUseWeightedVoting(true);
    myKNN.buildClassifier(train);
    //myKNN.setUseAcceleratedNNSearch(true);
    System.out.println("\nAccuracy Experiments:");
    MachineLearning_CW.performClassifierAccuracyTests(myKNN, trainData, testData, 1);

    /* Timing tests */
    System.out.println("\n\nTiming Experiments:");
    MachineLearning_CW.performClassifierTimingTests(wekaKNN, trainData, testData);
}

From source file:machine_learing_clasifier.MyC45.java

public double BestContinousAttribute(Instances i, Attribute att) {

    i.sort(att);/*from   ww w  .java2s.  co m*/
    Enumeration enumForMissingAttr = i.enumerateInstances();
    double temp = i.get(0).classValue();
    double igtemp = 0;
    double bestthreshold = 0;
    double a;
    double b = i.get(0).value(att);
    while (enumForMissingAttr.hasMoreElements()) {
        Instance inst = (Instance) enumForMissingAttr.nextElement();
        if (temp != inst.classValue()) {
            temp = inst.classValue();
            a = b;
            b = inst.value(att);
            double threshold = a + ((b - a) / 2);
            double igtemp2 = computeInformationGainContinous(i, att, threshold);
            if (igtemp < igtemp2) {
                bestthreshold = threshold;
                igtemp = igtemp2;
            }

        }

    }
    return bestthreshold;
}

From source file:main.coba.java

public static void main(String[] args) throws Exception {
    BufferedReader breader = null;
    breader = new BufferedReader(new FileReader("src/main/Team.arff"));
    Instances inputTrain = new Instances(breader);
    inputTrain.setClassIndex(inputTrain.numAttributes() - 1);
    breader.close();/*w  ww  .  ja  v  a2 s  . c  o  m*/
    FeedForwardNeuralNetworkAlgorithm FFNN = new FeedForwardNeuralNetworkAlgorithm(inputTrain);
    FFNN.buildModel(1, 5);
    FFNN.printModel();
    FFNN.printAllWeights();

    double[] arr = inputTrain.get(60).toDoubleArray();
    FFNN.setInputLayer(arr);
    FFNN.determineOutput(inputTrain.get(60));
    System.out.println(FFNN.getClassOutputValues());
    FFNN.updateModel(inputTrain.get(60));
    FFNN.printModel();
    FFNN.printAllWeights();
    System.out.println("Class : " + FFNN.getClassOutputValues());

    System.out.println("\nupdate again!!!!\n");
    FFNN.clearModel();
    arr = null;
    arr = inputTrain.get(61).toDoubleArray();
    FFNN.setInputLayer(arr);
    FFNN.determineOutput(inputTrain.get(61));
    FFNN.updateModel(inputTrain.get(61));
    FFNN.printModel();
    FFNN.printAllWeights();
    System.out.println("Class : " + FFNN.getClassOutputValues());

    System.out.println("\nupdate again!!!!\n");
    FFNN.clearModel();
    arr = null;
    arr = inputTrain.get(62).toDoubleArray();
    FFNN.setInputLayer(arr);
    FFNN.determineOutput(inputTrain.get(62));
    FFNN.updateModel(inputTrain.get(62));
    FFNN.printModel();
    FFNN.printAllWeights();
    System.out.println("Class : " + FFNN.getClassOutputValues());

}

From source file:main.mFFNN.java

public static void main(String[] args) throws Exception {
    mFFNN m = new mFFNN();
    BufferedReader breader = null;
    breader = new BufferedReader(new FileReader("src\\main\\iris.arff"));
    Instances fileTrain = new Instances(breader);
    fileTrain.setClassIndex(fileTrain.numAttributes() - 1);
    System.out.println(fileTrain);
    breader.close();/*from w  w  w. j  a  v  a 2s .  c o m*/
    System.out.println("mFFNN!!!\n\n");
    FeedForwardNeuralNetwork FFNN = new FeedForwardNeuralNetwork();

    Evaluation eval = new Evaluation(fileTrain);
    FFNN.buildClassifier(fileTrain);

    eval.evaluateModel(FFNN, fileTrain);

    //OUTPUT
    Scanner scan = new Scanner(System.in);
    System.out.println(eval.toSummaryString("=== Stratified cross-validation ===\n" + "=== Summary ===", true));
    System.out.println(eval.toClassDetailsString("=== Detailed Accuracy By Class ==="));
    System.out.println(eval.toMatrixString("===Confusion matrix==="));
    System.out.println(eval.fMeasure(1) + " " + eval.recall(1));
    System.out.println("\nDo you want to save this model(1/0)? ");
    FFNN.distributionForInstance(fileTrain.get(0));
    /* int c = scan.nextInt();
    if (c == 1 ){
     System.out.print("Please enter your file name (*.model) : ");
     String infile = scan.next();
     m.saveModel(FFNN,infile);
    }
    else {
    System.out.print("Model not saved.");
    } */
}

From source file:meddle.PredictByDomainOS.java

License:Open Source License

private static boolean predictOneFlow(String line, String domainOS) {
    if (!domainOSModel.containsKey(domainOS))
        return false;
    else {//from  w  w  w . j  a  va 2 s. c  o m
        try {
            Classifier classifier = domainOSModel.get(domainOS);
            Map<String, Integer> fi = domainOSFeature.get(domainOS);
            Instances structure = domainOSStruct.get(domainOS);
            Instance current = getInstance(line, fi, fi.size());

            Instances is = new Instances(structure);
            is.setClassIndex(is.numAttributes() - 1);
            is.add(current);
            current = is.get(is.size() - 1);
            current.setClassMissing();
            double predicted = classifier.classifyInstance(current);
            if (predicted > 0) {
                return true;
            } else
                return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:ml.ann.MultiClassPTR.java

@Override
public void buildClassifier(Instances instances) throws Exception {
    initAttributes(instances);//  w w  w .j  a  v a2  s .c om

    // REMEMBER: only works if class index is in the last position
    for (int instanceIdx = 0; instanceIdx < instances.numInstances(); instanceIdx++) {
        Instance instance = instances.get(instanceIdx);
        double[] inputInstance = inputInstances[instanceIdx];
        inputInstance[0] = 1.0; // initialize bias value
        for (int attrIdx = 0; attrIdx < instance.numAttributes() - 1; attrIdx++) {
            inputInstance[attrIdx + 1] = instance.value(attrIdx); // the first index of input instance is for bias
        }
    }

    // Initialize target values
    if (instances.classAttribute().isNominal()) {
        for (int instanceIdx = 0; instanceIdx < instances.numInstances(); instanceIdx++) {
            Instance instance = instances.instance(instanceIdx);
            for (int classIdx = 0; classIdx < instances.numClasses(); classIdx++) {
                targetInstances[instanceIdx][classIdx] = 0.0;
            }
            targetInstances[instanceIdx][(int) instance.classValue()] = 1.0;
        }
    } else {
        for (int instanceIdx = 0; instanceIdx < instances.numInstances(); instanceIdx++) {
            Instance instance = instances.instance(instanceIdx);
            targetInstances[instanceIdx][0] = instance.classValue();
        }
    }

    if (algo == 1) {
        setActFunction();
        buildClassifier();
    } else if (algo == 2) {
        buildClassifier();
    } else if (algo == 3) {
        buildClassifierBatch();
    }
}

From source file:mlda.util.Utils.java

License:Open Source License

/**
 * Get number of labels associated with each instance
 * /*from   w ww.  j  a v a  2 s  .co m*/
 * @param mlData Multi-label dataset
 * @return Array with the number of labels associated with each instance
 */
public static int[] labelsForInstance(MultiLabelInstances mlData) {

    int nInstances = mlData.getNumInstances();
    int nLabels = mlData.getNumLabels();

    int[] labelsForInstance = new int[nInstances];

    int[] labelIndices = mlData.getLabelIndices();

    Instances instances = mlData.getDataSet();

    Instance inst;
    for (int i = 0; i < nInstances; i++) {
        inst = instances.get(i);

        for (int j = 0; j < nLabels; j++) {
            if (inst.value(labelIndices[j]) == 1) {
                labelsForInstance[i]++;
            }
        }
    }

    return (labelsForInstance);
}