Changeset 36824


Ignore:
Timestamp:
2022-10-17T16:13:53+13:00 (19 months ago)
Author:
davidb
Message:

Next step in developing knn calculation for sampel AV input

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-installations/mars/trunk/sites/mars/src/java/org/greenstone/mars/WekaFindInstanceKNN.java

    r35229 r36824  
    11package org.greenstone.mars;
    22
     3import weka.core.DenseInstance;
    34import weka.core.Instance;
    45import weka.core.Instances;
     
    1112// Based on StackOverflow:
    1213//   https://stackoverflow.com/questions/31350506/how-to-calculate-the-nearest-neighbors-using-weka-from-the-command-line
     14
     15// The following was also useful as a reference:
     16//    https://waikato.github.io/weka-blog/posts/2018-10-08-making-a-weka-classifier/
    1317
    1418public class WekaFindInstanceKNN
     
    3741    }
    3842
    39     public static void findNearestKNN(NearestNeighbourSearch knn, Instance sample_instance, int k_nearest)
     43    public static void printNearestKNN(Instance sample_instance, Instances nearest_instances,
     44                      int k_nearest)
    4045    {
    4146    try {
    42         Instances nearest_instances= knn.kNearestNeighbours(sample_instance, k_nearest);
    43        
    4447        //cycle through the instances and printout the nearestneighbors
    45        
     48
    4649        System.out.println("\n" + sample_instance);
    4750        for(int i =0; i<k_nearest; i++) {
    48         System.out.println("\t" + nearest_instances.instance(i));       
     51        System.out.println("\t" + nearest_instances.instance(i));
    4952        }   
    5053    }
     
    5760
    5861    public static void main(String[] args)
    59     {   
     62    {
     63    // First example output, when working through the instances specified in the CSV file
     64    // (looking for similaries amongst all the instances in the CSV file)
     65
     66    // ds_22716_5743-6,-0.549489,-0.118439
     67    //    ds_22761_1171-12,-0.549489,-0.118439
     68    //    ds_21046_7743-30,-0.549489,-0.118439
     69    //    ds_24768_23507-6,-0.549489,-0.118439
     70    //    ds_22761_1171-15,-0.549489,-0.118439
     71
    6072    if (args.length != 2) {
    6173        System.err.println("Usage: k-nearest-num file.{arff,csv}");
     
    7486    NearestNeighbourSearch knn = initKNN(instances);
    7587
    76     /*
    77     DataSource source = new DataSource(input_filename);
    78     Instances instances = source.getDataSet();
    79    
    80     LinearNNSearch knn = new LinearNNSearch(instances);
    81     //KDTree knn = new KDTree(instances);
    82     */
    8388
    84     //cycle through the dataset and get instances for the nearestneighbors
    85    
     89    Instance sample_instance = new DenseInstance(3);
     90    sample_instance.setDataset(instances);
     91
     92    // sample sample:
     93    //   ds_22716_5743-6,-0.549489,-0.118439
     94    sample_instance.setValue(0, "ds_22716_5743-6");
     95    sample_instance.setValue(1, -0.549489);
     96    sample_instance.setValue(2, -0.118439);
     97
     98    //findNearestKNN(knn,sample_instance,k_nearest);
     99
     100    try {
     101        Instances nearest_instances= knn.kNearestNeighbours(sample_instance, k_nearest);
     102        System.out.println("**** nearest_instances len = " + nearest_instances.numInstances());
     103
     104        printNearestKNN(sample_instance,nearest_instances, k_nearest);
     105
     106    }
     107    catch (Exception e) {
     108        e.printStackTrace();
     109    }
     110
     111
     112    /* 
    86113    int num_instances = instances.numInstances();
    87114    for(int j=0; j<num_instances; j++) {
    88         //Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);
    89115       
    90116        Instance sample_instance = instances.instance(j);
    91117        findNearestKNN(knn,sample_instance,k_nearest);
    92     }   
     118        }   
     119    */
     120 
    93121    }
    94122}
Note: See TracChangeset for help on using the changeset viewer.