Exemplo n.º 1
0
    public static void printNeighbors(ArrayList neighbors) {
        int i = 0;
        for(Neighbor neighbor : neighbors) {
            Instance instance = neighbor.getInstance();

            System.out.println("\nNeighbor " + (i + 1) + ", distance: " + neighbor.getDistance());
            i++;
            for(Feature f : instance.getAttributes()) {
                System.out.print(f.getName() + ": ");
                if(f instanceof Category) {
                    System.out.println(((Category)f).getCategory().toString());
                }
                else if(f instanceof Distance) {
                    System.out.println(((Distance)f).getDistance().toString());
                }
                else if (f instanceof Expiration) {
                    System.out.println(((Expiration)f).getExpiry().toString());
                }
                else if (f instanceof Handset) {
                    System.out.print(((Handset)f).getOs().toString() + ", ");
                    System.out.println(((Handset)f).getDevice().toString());
                }
                else if (f instanceof Offer) {
                    System.out.println(((Offer)f).getOfferType().toString());
                }
                else if (f instanceof WSAction) {
                    System.out.println(((WSAction)f).getAction().toString());
                }
            }
        }
    }
Exemplo n.º 2
0
    public static void main(String[] args) {
        ArrayList instances = null;
        ArrayList distances = null;
        ArrayList neighbors = null;
        WSAction.Action classification = null;
        Instance classificationInstance = null;
        FileReader reader = null;
        int numRuns = 0, truePositives = 0, falsePositives = 0, falseNegatives = 0, trueNegatives = 0;
        double precision = 0, recall = 0, fMeasure = 0;

        falsePositives = 1;

        reader = new FileReader(PATH_TO_DATA_FILE);
        instances = reader.buildInstances();

        do {
            classificationInstance = extractIndividualInstance(instances);

            distances = calculateDistances(instances, classificationInstance);
            neighbors = getNearestNeighbors(distances);
            classification = determineMajority(neighbors);

            System.out.println("Gathering " + K + " nearest neighbors to:");
            printClassificationInstance(classificationInstance);

            printNeighbors(neighbors);
            System.out.println("\nExpected situation result for instance: " + classification.toString());

            if(classification.toString().equals(((WSAction)classificationInstance.getAttributes().get(WSACTION_INDEX)).getAction().toString())) {
                truePositives++;
            }
            else {
                falseNegatives++;
            }
            numRuns++;

            instances.add(classificationInstance);
        } while(numRuns < NUM_RUNS);

        precision = ((double)(truePositives / (double)(truePositives + falsePositives)));
        recall = ((double)(truePositives / (double)(truePositives + falseNegatives)));
        fMeasure = ((double)(precision * recall) / (double)(precision + recall));

        System.out.println("Precision: " + precision);
        System.out.println("Recall: " + recall);
        System.out.println("F-Measure: " + fMeasure);
        System.out.println("Average distance: " + (double)(averageDistance / (double)(NUM_RUNS * K)));
    }
Exemplo n.º 3
0
 public static void printClassificationInstance(Instance classificationInstance) {
     for(Feature f : classificationInstance.getAttributes()) {
         System.out.print(f.getName() + ": ");
         if(f instanceof Category) {
             System.out.println(((Category)f).getCategory().toString());
         }
         else if(f instanceof Distance) {
             System.out.println(((Distance)f).getDistance().toString());
         }
         else if (f instanceof Expiration) {
             System.out.println(((Expiration)f).getExpiry().toString());
         }
         else if (f instanceof Handset) {
             System.out.print(((Handset)f).getOs().toString() + ", ");
             System.out.println(((Handset)f).getDevice().toString());
         }
         else if (f instanceof Offer) {
             System.out.println(((Offer)f).getOfferType().toString());
         }
         else if (f instanceof WSAction) {
             System.out.println(((WSAction)f).getAction().toString());
         }
     }
 }