Changeset 36824
- Timestamp:
- 2022-10-17T16:13:53+13:00 (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs3-installations/mars/trunk/sites/mars/src/java/org/greenstone/mars/WekaFindInstanceKNN.java
r35229 r36824 1 1 package org.greenstone.mars; 2 2 3 import weka.core.DenseInstance; 3 4 import weka.core.Instance; 4 5 import weka.core.Instances; … … 11 12 // Based on StackOverflow: 12 13 // 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/ 13 17 14 18 public class WekaFindInstanceKNN … … 37 41 } 38 42 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) 40 45 { 41 46 try { 42 Instances nearest_instances= knn.kNearestNeighbours(sample_instance, k_nearest);43 44 47 //cycle through the instances and printout the nearestneighbors 45 48 46 49 System.out.println("\n" + sample_instance); 47 50 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)); 49 52 } 50 53 } … … 57 60 58 61 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 60 72 if (args.length != 2) { 61 73 System.err.println("Usage: k-nearest-num file.{arff,csv}"); … … 74 86 NearestNeighbourSearch knn = initKNN(instances); 75 87 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 */83 88 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 /* 86 113 int num_instances = instances.numInstances(); 87 114 for(int j=0; j<num_instances; j++) { 88 //Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);89 115 90 116 Instance sample_instance = instances.instance(j); 91 117 findNearestKNN(knn,sample_instance,k_nearest); 92 } 118 } 119 */ 120 93 121 } 94 122 }
Note:
See TracChangeset
for help on using the changeset viewer.