source: main/trunk/model-sites-dev/mars/src/java/org/greenstone/mars/WekaFindInstanceKNN.java@ 35229

Last change on this file since 35229 was 35229, checked in by davidb, 3 years ago

Further refactoring

File size: 2.5 KB
Line 
1package org.greenstone.mars;
2
3import weka.core.Instance;
4import weka.core.Instances;
5import weka.core.converters.ConverterUtils.DataSource;
6
7import weka.core.neighboursearch.LinearNNSearch;
8import weka.core.neighboursearch.NearestNeighbourSearch;
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
14public class WekaFindInstanceKNN
15{
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)
40 {
41 try {
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
58 public static void main(String[] args)
59 {
60 if (args.length != 2) {
61 System.err.println("Usage: k-nearest-num file.{arff,csv}");
62 System.exit(1);
63 }
64
65 String k_nearest_str = args[0];
66 String input_filename = args[1];
67
68 int k_nearest = Integer.parseInt(k_nearest_str);
69
70 System.out.println("Weka Command Line Find Nearest " + k_nearest_str
71 + " Neighbors for each Instance in " + input_filename);
72
73 Instances instances = loadDataset(input_filename);
74 NearestNeighbourSearch knn = initKNN(instances);
75
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
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++) {
88 //Instances nearestInstances= knn.kNearestNeighbours(instances.instance(j), k_nearest);
89
90 Instance sample_instance = instances.instance(j);
91 findNearestKNN(knn,sample_instance,k_nearest);
92 }
93 }
94}
Note: See TracBrowser for help on using the repository browser.