Example usage for weka.core Instances firstInstance

List of usage examples for weka.core Instances firstInstance

Introduction

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

Prototype


publicInstance firstInstance() 

Source Link

Document

Returns the first instance in the set.

Usage

From source file:decisiontree.MyID3.java

private void makeTree(Instances data) {
    // Check if no instances have reached this node.  
    if (data.numInstances() == 0) {
        splitAttr = null;/*from  w  w w  . j a v  a  2 s  .c  om*/
        leafValue = Double.NaN;
        leafDist = new double[data.numClasses()];
        return;
    }

    if (data.numDistinctValues(data.classIndex()) == 1) {
        leafValue = data.firstInstance().classValue();
        return;
    }

    // Compute attribute with maximum information gain.  
    double[] infoGains = new double[data.numAttributes()];
    Enumeration attEnum = data.enumerateAttributes();
    while (attEnum.hasMoreElements()) {
        Attribute att = (Attribute) attEnum.nextElement();
        infoGains[att.index()] = computeInfoGain(data, att);
    }
    splitAttr = data.attribute(maxIndex(infoGains));

    // Make leaf if information gain is zero.   
    // Otherwise create successors.  
    if (Utils.eq(infoGains[splitAttr.index()], 0)) {
        splitAttr = null;
        leafDist = new double[data.numClasses()];
        Enumeration instEnum = data.enumerateInstances();
        while (instEnum.hasMoreElements()) {
            Instance inst = (Instance) instEnum.nextElement();
            leafDist[(int) inst.classValue()]++;
        }
        normalize(leafDist);
        leafValue = Utils.maxIndex(leafDist);
        classAttr = data.classAttribute();
    } else {
        Instances[] splitData = splitData(data, splitAttr);
        child = new MyID3[splitAttr.numValues()];
        for (int j = 0; j < splitAttr.numValues(); j++) {
            child[j] = new MyID3();
            child[j].makeTree(splitData[j]);
        }
    }
}

From source file:edu.umbc.cs.maple.utils.WekaUtils.java

License:Open Source License

/** Merge two instance sets.
 * @param instances1//w w w  .ja v a 2 s.co  m
 * @param instances2
 * @return the merged instance sets
 */
public static Instances mergeInstances(Instances instances1, Instances instances2) {
    if (instances1 == null)
        return instances2;
    if (instances2 == null)
        return instances1;
    if (!instances1.checkInstance(instances2.firstInstance()))
        throw new IllegalArgumentException("The instance sets are incompatible.");
    Instances mergedInstances = new Instances(instances1);
    Instances tempInstances = new Instances(instances2);
    for (int i = 0; i < tempInstances.numInstances(); i++) {
        mergedInstances.add(tempInstances.instance(i));
    }
    return mergedInstances;
}

From source file:edu.washington.cs.knowitall.utilities.Classifier.java

License:Open Source License

public double classifyInstance(String features) {
    StringReader testReader = new StringReader(features + "\n");
    Instances testingInstances = setupInstances(testReader);
    try {//from  ww w .  ja v  a2 s  .c  o m
        classifier.classifyInstance(testingInstances.firstInstance());
        return classifier.distributionForInstance(testingInstances.firstInstance())[0];
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0.5;
}

From source file:eksploracja.Eksploracja.java

/**
 * @param args the command line arguments
 *///from w ww  .  jav  a 2s.c  o m
public static void main(String[] args) throws Exception {
    // TODO code application logic here
    //sout +tabualcja

    System.out.println("Hello world - tu eksploracja");

    //Pobieranie danych
    String filename = "C:\\Program Files\\Weka-3-8\\data\\weather.numeric.arff";
    DataSource source = new DataSource(filename);
    Instances mojeDane = source.getDataSet();

    //Wywietlanie danych
    System.out.println("Dane: ");
    // System.out.println(mojeDane); //cao danych

    Instance wiersz0 = mojeDane.firstInstance();

    System.out.println("Pocztek " + mojeDane.firstInstance()); //pierwszy wiersz
    System.out.println("Koniec " + mojeDane.lastInstance()); //ostatni wiersz

    System.out.println("\nLiczba danych: " + mojeDane.numInstances());
    System.out.println("\nAtrybuty w liczbie: " + mojeDane.numAttributes());

    for (int i = 0; i < mojeDane.numAttributes(); i++) {
        System.out.println(i + ". " + mojeDane.attribute(i));
        Attribute atr = mojeDane.attribute(i);
        System.out.println(i + " " + atr.name());

        if (atr.isNominal()) {
            System.out.println("Typ danych nominalne");
        } else {
            System.out.println("Typ danych numeryczne");
        }
    }

    //Zapisywanie danych w posataci liczbowej
    System.out.println("Dane - jako liczby: ");
    System.out.println(Arrays.toString(wiersz0.toDoubleArray()));

}

From source file:farm_ads.MyClassifier.java

public String ClassifyInstance(Classifier c, String instance) throws Exception {

    String format = "%4s %15s %15s\n";
    FarmAds fa = new FarmAds(instance, 1);
    FarmAdsVector fav = new FarmAdsVector();
    fav.writeFile("data\\dataVecto.dat", fa);
    DataSource source = new DataSource("data\\dataVecto.dat");
    Instances instances = source.getDataSet();
    if (instances.classIndex() == -1) {
        instances.setClassIndex(instances.numAttributes() - 1);
    }/*from   w  w w.  j a v a 2 s . c om*/

    String s = new String();

    s += "======= Kt qu d on qung co========\n";
    s += String.format(format, "STT", "Trc d on", "Sau d on");

    String[] classAds = { "Ph hp", "Khng Ph Hp" };
    double actValue = instances.firstInstance().classValue();

    Instance newInst = instances.firstInstance();

    double pred = c.classifyInstance(newInst);

    s += String.format(format, Integer.toString(1), classAds[(int) actValue], classAds[(int) pred]);

    if (actValue == pred) {
        s += "\n\n ==> D on ng";
    } else {
        s += "\n\n ==> D on sai";
    }

    return s;
}

From source file:lu.lippmann.cdb.lab.beta.util.WekaUtil2.java

License:Open Source License

/**
 * Generate the centroid coordinates based 
 * on it's  members (objects assigned to the cluster of the centroid) and the distance 
 * function being used./*from w  ww .  j av a  2  s . c o  m*/
 * @return the centroid
 */
public static MixedCentroid computeMixedCentroid(final boolean preserveOrder,
        final NormalizableDistance distanceFunction, final Instances numericInstances,
        final Instances originalInstances, final int clusterIndex) {
    final int numInstances = numericInstances.numInstances();
    final int numAttributes = numericInstances.numAttributes();

    final Map<TupleSI, Integer> addedAttr = new HashMap<TupleSI, Integer>();

    if (numInstances == 1) {
        Instance uniqueNumInstance = numericInstances.firstInstance();
        Instance uniqueMixInstance = originalInstances.firstInstance();
        double[] centroid = uniqueNumInstance.toDoubleArray();
        for (int i = 0; i < uniqueMixInstance.numAttributes(); i++) {
            if (!uniqueMixInstance.attribute(i).isNumeric()) {
                final String catVal = uniqueMixInstance.attribute(i).value((int) uniqueMixInstance.value(i));
                addedAttr.put(new TupleSI(catVal, i), 1);
            }
        }
        return new MixedCentroid(clusterIndex, centroid, addedAttr);
    }

    final double[] vals = new double[numAttributes];

    //used only for Manhattan Distance
    Instances sortedMembers = null;
    int middle = 0;
    boolean dataIsEven = false;

    final boolean isManhattanDist = (distanceFunction instanceof ManhattanDistance);
    final boolean isEuclideanDist = (distanceFunction instanceof EuclideanDistance);

    if (isManhattanDist) {
        middle = (numInstances - 1) / 2;
        dataIsEven = ((numInstances % 2) == 0);
        if (preserveOrder) {
            sortedMembers = numericInstances;
        } else {
            sortedMembers = new Instances(numericInstances);
        }
    }

    for (int j = 0; j < numAttributes; j++) {
        //in case of Euclidian distance the centroid is the mean point
        //in case of Manhattan distance the centroid is the median point
        //in both cases, if the attribute is nominal, the centroid is the mode            
        if (isEuclideanDist) {
            vals[j] = numericInstances.meanOrMode(j);

            for (int i = 0; i < numInstances; i++) {
                if (!originalInstances.attribute(j).isNumeric()) {
                    final Instance instance = originalInstances.instance(i);
                    final String catVal = instance.attribute(j).value((int) instance.value(j));
                    //Initialize map
                    final TupleSI key = new TupleSI(catVal, j);
                    if (!addedAttr.containsKey(key))
                        addedAttr.put(key, 0);
                    addedAttr.put(key, addedAttr.get(key) + 1);
                }
            }
        } else if (isManhattanDist) {
            sortedMembers.kthSmallestValue(j, middle + 1);
            vals[j] = sortedMembers.instance(middle).value(j);
            if (dataIsEven) {
                sortedMembers.kthSmallestValue(j, middle + 2);
                vals[j] = (vals[j] + sortedMembers.instance(middle + 1).value(j)) / 2;
            }
        } else {
            throw new IllegalStateException("Not handled distance ...");
        }
    }

    return new MixedCentroid(clusterIndex, vals, addedAttr);
}

From source file:machinelearninglabs.OENaiveBayesClassifier.java

/***************************** HELPER METHODS  ************************************/

public void classDistribution(Instances data) {

    classCount = new int[data.firstInstance().numClasses()];
    classProbs = new double[data.firstInstance().numClasses()];

    // Get the frequency/count of each class in the data
    for (Instance eachInstance : data) {
        double classValue = eachInstance.value(eachInstance.classIndex());
        classCount[(int) classValue]++;
    }//from  w w  w . j  a  va2 s.c  o m

    // Get the probability of the occurence of each class
    for (int i = 0; i < classProbs.length; i++) {
        classProbs[i] = (double) classCount[i] / data.numInstances();
    }

    printIntArray(classCount);
    System.out.println(data.firstInstance().value(0));
    printDoubleArray(classProbs);
}

From source file:machinelearninglabs.OENaiveBayesClassifier.java

public int[][] attributeCounts(Instances data, int att) {
    int numberOfPossibleValuesForAttribute = data.firstInstance().attribute(att).numValues();
    int[][] result = new int[data.numClasses()][numberOfPossibleValuesForAttribute];

    // for each class
    for (Instance eachInstance : data) {
        double classValue = eachInstance.value(eachInstance.classIndex());
        result[(int) classValue][(int) eachInstance.value(att)]++;
    }/*from   www  . jav  a2s  .  co m*/
    //printIntMatrix(result);
    return result;
}

From source file:machinelearninglabs.OENaiveBayesClassifier.java

public double[][] attributeProbs(Instances data, int att) {
    int numberOfPossibleValuesForAttribute = data.firstInstance().attribute(att).numValues();
    double[][] result = new double[data.numClasses()][numberOfPossibleValuesForAttribute];

    // for each class
    for (Instance eachInstance : data) {
        double classValue = eachInstance.value(eachInstance.classIndex());
        result[(int) classValue][(int) eachInstance.value(att)]++;
    }/*  w w  w  .  j a  v  a  2  s  .  c o  m*/

    // Get conditional probabilities ie probability that attribute = x given some class
    for (int i = 0; i < result.length; i++) {
        for (int j = 0; j < result[i].length; j++) {
            result[i][j] = (double) result[i][j] / classCount[i];
        }
    }
    //printDoubleMatrix(result);
    return result;
}

From source file:meka.filters.multilabel.SuperNodeFilter.java

License:Open Source License

@Override
public Instances process(Instances D) throws Exception {

    //System.out.println("PROCESS! = "+D.numInstances());

    int L = D.classIndex();
    D = new Instances(D); // D_

    // rename classes 
    for (int j = 0; j < L; j++) {
        D.renameAttribute(j, encodeClass(j));
    }//from w w  w . j  a va 2 s. c  om

    // merge labels
    D = mergeLabels(D, indices, m_P, m_N);

    // templates
    x_template = D.firstInstance();
    setOutputFormat(D);

    //System.out.println("PROCESS! => "+D);
    return D;
}