Beispiel #1
0
  public static ArrayList<Integer> getProfiles(Instances inst, List<Integer> marks)
      throws Exception {

    //		Instances inst = Utils.prepareProfileMatcherData(schoolNo, grade, term, subjects);

    //		ReplaceMissingValues rmv = new ReplaceMissingValues();
    //		rmv.setInputFormat(inst);
    //		inst = Filter.useFilter(inst, rmv);

    for (int i = 0; i < inst.numAttributes(); i++) {
      inst.deleteWithMissing(i);
    }

    KDTree tree = new KDTree();
    tree.setMeasurePerformance(true);

    try {
      tree.setInstances(inst);

      EuclideanDistance df = new EuclideanDistance(inst);
      df.setDontNormalize(true);
      df.setAttributeIndices("2-last");

      tree.setDistanceFunction(df);

    } catch (Exception e) {
      e.printStackTrace();
    }

    Instances neighbors = null;

    Instances test = CFilter.createInstance(112121, (ArrayList<Integer>) marks);

    Instance p = test.firstInstance();

    try {
      neighbors = tree.kNearestNeighbours(p, 50);
    } catch (Exception e) {
      e.printStackTrace();
    }
    //		System.out.println(tree.getPerformanceStats().getTotalPointsVisited());

    //		System.out.println(nn1 + " is the nearest neigbor for " + p);
    //		System.out.println(nn2 + " is the second nearest neigbor for " + p);

    ArrayList<Integer> profiles = new ArrayList<Integer>();
    for (int i = 0; i < neighbors.numInstances(); i++) {
      System.out.println(neighbors.instance(i));
      profiles.add(Integer.valueOf(neighbors.instance(i).toString(0)));
    }

    // Now we can also easily compute the distances as the KDTree does it

    DistanceFunction df = tree.getDistanceFunction();
    //		System.out.println("The distance between" + nn1 + " and " + p + " is " + df.distance(nn1,
    // p));
    //		System.out.println("The distance between" + nn2 + " and " + p + " is " + df.distance(nn2,
    // p));
    return profiles;
  }
Beispiel #2
0
  public static void main(String args[]) {
    Timers timer = new Timers();
    try {
      // Get the data set path.
      String referenceFile = Utils.getOption('r', args);
      String queryFile = Utils.getOption('q', args);
      if (referenceFile.length() == 0)
        throw new IllegalArgumentException(
            "Required option: File containing" + "the reference dataset.");

      // Load input dataset.
      DataSource source = new DataSource(referenceFile);
      Instances referenceData = source.getDataSet();

      Instances queryData = null;
      if (queryFile.length() != 0) {
        source = new DataSource(queryFile);
        queryData = source.getDataSet();
      }

      timer.StartTimer("total_time");

      // Get all the parameters.
      String leafSize = Utils.getOption('l', args);
      String neighbors = Utils.getOption('k', args);

      // Validate options.
      int k = 0;
      if (neighbors.length() == 0) {
        throw new IllegalArgumentException(
            "Required option: Number of " + "furthest neighbors to find.");
      } else {
        k = Integer.parseInt(neighbors);
        if (k < 1 || k > referenceData.numInstances())
          throw new IllegalArgumentException("[Fatal] Invalid k");
      }

      int l = 20;
      if (leafSize.length() != 0) l = Integer.parseInt(leafSize);

      // Create KDTree.
      KDTree tree = new KDTree();
      tree.setMaxInstInLeaf(l);
      tree.setInstances(referenceData);

      // Perform All K-Nearest-Neighbors.
      if (queryFile.length() != 0) {
        for (int i = 0; i < queryData.numInstances(); i++) {
          Instances out = tree.kNearestNeighbours(queryData.instance(i), k);
        }
      } else {
        for (int i = 0; i < referenceData.numInstances(); i++) {
          Instances out = tree.kNearestNeighbours(referenceData.instance(i), k);
        }
      }

      timer.StopTimer("total_time");
      timer.PrintTimer("total_time");
    } catch (IOException e) {
      System.err.println(USAGE);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }