Changeset 35227


Ignore:
Timestamp:
2021-07-15T23:34:59+12:00 (3 years ago)
Author:
davidb
Message:

With some refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/model-sites-dev/mars/src/java/org/greenstone/mars/WekaCLFindNN.java

    r35226 r35227  
    11package org.greenstone.mars;
    22
     3import weka.core.Instance;
    34import weka.core.Instances;
    45import weka.core.converters.ConverterUtils.DataSource;
    56
    67import weka.core.neighboursearch.LinearNNSearch;
     8import weka.core.neighboursearch.NearestNeighbourSearch;
    79//import weka.core.neighboursearch.KDTree;
    810
     
    1214public class WekaCLFindNN
    1315{
     16    public static void findNearestNN(NearestNeighbourSearch knn, Instance sample_instance, int k_nearest)
     17    {
     18    try {
     19        Instances nearest_instances= knn.kNearestNeighbours(sample_instance, k_nearest);
     20       
     21        //cycle through the instances and printout the nearestneighbors
     22       
     23        System.out.println("\n" + sample_instance);
     24        for(int i =0; i<k_nearest; i++) {
     25        System.out.println("\t" + nearest_instances.instance(i));       
     26        }   
     27    }
     28    catch (Exception e) {
     29        e.printStackTrace();
     30    }
     31       
     32    }
     33
     34
    1435    public static void main(String[] args) throws Exception {
    1536   
    1637    if (args.length != 2) {
    17         System.err.println("Usage: N ARFF-file");
     38        System.err.println("Usage: k-nearest-num file.{arff,csv}");
    1839        System.exit(1);
    1940    }
    20     String num_nearest_str     = args[0];
    21     String input_arff_filename = args[1];
     41    String k_nearest_str     = args[0];
     42    String input_filename = args[1];
    2243   
    23     int num_nearest = Integer.parseInt(num_nearest_str);
     44    int k_nearest = Integer.parseInt(k_nearest_str);
    2445     
    25     System.out.println("Weka Command Line Find Nearest " + num_nearest_str
    26                + " Neighbors for each Instance in "  + input_arff_filename);
     46    System.out.println("Weka Command Line Find Nearest " + k_nearest_str
     47               + " Neighbors for each Instance in "  + input_filename);
    2748   
    28     DataSource source = new DataSource(input_arff_filename);
     49    DataSource source = new DataSource(input_filename);
    2950    Instances instances = source.getDataSet();
    3051   
     
    3657    int num_instances = instances.numInstances();
    3758    for(int j=0; j<num_instances; j++) {
    38         Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), num_nearest);
     59        Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);
    3960       
    40         //cycle through the instances and printout the nearestneighbors
    41         System.out.println("\n" + instances.instance(j));
    42         for(int i =0; i<num_nearest; i++) {
    43         System.out.println("\t" + nearestInstances.instance(i));         
    44         }       
     61        Instance sample_instance = instances.instance(j);
     62        findNearestNN(knn,sample_instance,k_nearest);
    4563    }   
    4664    }
Note: See TracChangeset for help on using the changeset viewer.