Changeset 35227
- Timestamp:
- 2021-07-15T23:34:59+12:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/model-sites-dev/mars/src/java/org/greenstone/mars/WekaCLFindNN.java
r35226 r35227 1 1 package org.greenstone.mars; 2 2 3 import weka.core.Instance; 3 4 import weka.core.Instances; 4 5 import weka.core.converters.ConverterUtils.DataSource; 5 6 6 7 import weka.core.neighboursearch.LinearNNSearch; 8 import weka.core.neighboursearch.NearestNeighbourSearch; 7 9 //import weka.core.neighboursearch.KDTree; 8 10 … … 12 14 public class WekaCLFindNN 13 15 { 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 14 35 public static void main(String[] args) throws Exception { 15 36 16 37 if (args.length != 2) { 17 System.err.println("Usage: N ARFF-file");38 System.err.println("Usage: k-nearest-num file.{arff,csv}"); 18 39 System.exit(1); 19 40 } 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]; 22 43 23 int num_nearest = Integer.parseInt(num_nearest_str);44 int k_nearest = Integer.parseInt(k_nearest_str); 24 45 25 System.out.println("Weka Command Line Find Nearest " + num_nearest_str26 + " 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); 27 48 28 DataSource source = new DataSource(input_ arff_filename);49 DataSource source = new DataSource(input_filename); 29 50 Instances instances = source.getDataSet(); 30 51 … … 36 57 int num_instances = instances.numInstances(); 37 58 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); 39 60 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); 45 63 } 46 64 }
Note:
See TracChangeset
for help on using the changeset viewer.