Changeset 35229


Ignore:
Timestamp:
2021-07-15T23:55:57+12:00 (10 days ago)
Author:
davidb
Message:

Further refactoring

File:
1 edited

Legend:

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

    r35228 r35229  
    1212//   https://stackoverflow.com/questions/31350506/how-to-calculate-the-nearest-neighbors-using-weka-from-the-command-line
    1313
    14 public class WekaCLFindNN
     14public class WekaFindInstanceKNN
    1515{
    16     public static void findNearestNN(NearestNeighbourSearch knn, Instance sample_instance, int k_nearest)
     16
     17    public static Instances loadDataset(String input_filename)
     18    {
     19    Instances instances = null;
     20
     21    try {
     22        DataSource source = new DataSource(input_filename);
     23        instances = source.getDataSet();
     24    }
     25    catch (Exception e) {
     26        e.printStackTrace();
     27    }
     28   
     29    return instances;
     30    }
     31
     32    public static NearestNeighbourSearch initKNN(Instances instances)
     33    {
     34    LinearNNSearch knn = new LinearNNSearch(instances);
     35   
     36    return knn;
     37    }
     38
     39    public static void findNearestKNN(NearestNeighbourSearch knn, Instance sample_instance, int k_nearest)
    1740    {
    1841    try {
     
    3356
    3457
    35     public static void main(String[] args) throws Exception {
    36    
     58    public static void main(String[] args)
     59    {   
    3760    if (args.length != 2) {
    3861        System.err.println("Usage: k-nearest-num file.{arff,csv}");
    3962        System.exit(1);
    4063    }
     64
    4165    String k_nearest_str     = args[0];
    4266    String input_filename = args[1];
     
    4670    System.out.println("Weka Command Line Find Nearest " + k_nearest_str
    4771               + " Neighbors for each Instance in "  + input_filename);
    48    
     72
     73    Instances instances = loadDataset(input_filename);
     74    NearestNeighbourSearch knn = initKNN(instances);
     75
     76    /*
    4977    DataSource source = new DataSource(input_filename);
    5078    Instances instances = source.getDataSet();
     
    5280    LinearNNSearch knn = new LinearNNSearch(instances);
    5381    //KDTree knn = new KDTree(instances);
    54    
     82    */
     83
    5584    //cycle through the dataset and get instances for the nearestneighbors
    5685   
    5786    int num_instances = instances.numInstances();
    5887    for(int j=0; j<num_instances; j++) {
    59         Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);
     88        //Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);
    6089       
    6190        Instance sample_instance = instances.instance(j);
    62         findNearestNN(knn,sample_instance,k_nearest);
     91        findNearestKNN(knn,sample_instance,k_nearest);
    6392    }   
    6493    }
Note: See TracChangeset for help on using the changeset viewer.