[35226] | 1 | package org.greenstone.mars;
|
---|
| 2 |
|
---|
[35227] | 3 | import weka.core.Instance;
|
---|
[35226] | 4 | import weka.core.Instances;
|
---|
| 5 | import weka.core.converters.ConverterUtils.DataSource;
|
---|
| 6 |
|
---|
| 7 | import weka.core.neighboursearch.LinearNNSearch;
|
---|
[35227] | 8 | import weka.core.neighboursearch.NearestNeighbourSearch;
|
---|
[35226] | 9 | //import weka.core.neighboursearch.KDTree;
|
---|
| 10 |
|
---|
| 11 | // Based on StackOverflow:
|
---|
| 12 | // https://stackoverflow.com/questions/31350506/how-to-calculate-the-nearest-neighbors-using-weka-from-the-command-line
|
---|
| 13 |
|
---|
[35229] | 14 | public class WekaFindInstanceKNN
|
---|
[35226] | 15 | {
|
---|
[35229] | 16 |
|
---|
| 17 | public static Instances loadDataset(String input_filename)
|
---|
[35227] | 18 | {
|
---|
[35229] | 19 | Instances instances = null;
|
---|
| 20 |
|
---|
[35227] | 21 | try {
|
---|
[35229] | 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)
|
---|
| 40 | {
|
---|
| 41 | try {
|
---|
[35227] | 42 | Instances nearest_instances= knn.kNearestNeighbours(sample_instance, k_nearest);
|
---|
| 43 |
|
---|
| 44 | //cycle through the instances and printout the nearestneighbors
|
---|
| 45 |
|
---|
| 46 | System.out.println("\n" + sample_instance);
|
---|
| 47 | for(int i =0; i<k_nearest; i++) {
|
---|
| 48 | System.out.println("\t" + nearest_instances.instance(i));
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | catch (Exception e) {
|
---|
| 52 | e.printStackTrace();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
[35229] | 58 | public static void main(String[] args)
|
---|
| 59 | {
|
---|
[35226] | 60 | if (args.length != 2) {
|
---|
[35227] | 61 | System.err.println("Usage: k-nearest-num file.{arff,csv}");
|
---|
[35226] | 62 | System.exit(1);
|
---|
| 63 | }
|
---|
[35229] | 64 |
|
---|
[35227] | 65 | String k_nearest_str = args[0];
|
---|
| 66 | String input_filename = args[1];
|
---|
[35226] | 67 |
|
---|
[35227] | 68 | int k_nearest = Integer.parseInt(k_nearest_str);
|
---|
[35226] | 69 |
|
---|
[35227] | 70 | System.out.println("Weka Command Line Find Nearest " + k_nearest_str
|
---|
| 71 | + " Neighbors for each Instance in " + input_filename);
|
---|
[35229] | 72 |
|
---|
| 73 | Instances instances = loadDataset(input_filename);
|
---|
| 74 | NearestNeighbourSearch knn = initKNN(instances);
|
---|
| 75 |
|
---|
| 76 | /*
|
---|
[35227] | 77 | DataSource source = new DataSource(input_filename);
|
---|
[35226] | 78 | Instances instances = source.getDataSet();
|
---|
| 79 |
|
---|
| 80 | LinearNNSearch knn = new LinearNNSearch(instances);
|
---|
| 81 | //KDTree knn = new KDTree(instances);
|
---|
[35229] | 82 | */
|
---|
| 83 |
|
---|
[35226] | 84 | //cycle through the dataset and get instances for the nearestneighbors
|
---|
| 85 |
|
---|
| 86 | int num_instances = instances.numInstances();
|
---|
| 87 | for(int j=0; j<num_instances; j++) {
|
---|
[35229] | 88 | //Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);
|
---|
[35226] | 89 |
|
---|
[35227] | 90 | Instance sample_instance = instances.instance(j);
|
---|
[35229] | 91 | findNearestKNN(knn,sample_instance,k_nearest);
|
---|
[35226] | 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|