KDTree knn = new KDTree(); knn.setInstances(ds);
/** * Adds one instance to the KDTree. This updates the KDTree structure to take * into account the newly added training instance. * * @param instance the instance to be added. Usually the newly added instance in the * training set. * @throws Exception If the instance cannot be added. */ public void update(Instance instance) throws Exception { // better to change // to addInstance if (m_Instances == null) throw new Exception("No instances supplied yet. Have to call " + "setInstances(instances) with a set of Instances " + "first."); addInstanceInfo(instance); addInstanceToTree(instance, m_Root); }
/** * Corrects the start and end indices of a * KDTreeNode after an instance is added to * the tree. The start and end indices for * the master index array (m_InstList) * stored in the nodes need to be updated * for all nodes in the subtree on the * right of a node where the instance * was added. * NOTE: No outside class should call this * method. * * @param node KDTreeNode whose start and end indices * need to be updated. */ protected void afterAddInstance(KDTreeNode node) { node.m_Start++; node.m_End++; if (!node.isALeaf()) { afterAddInstance(node.m_Left); afterAddInstance(node.m_Right); } }
int[] owners = refineOwners(node, centers, candidates); determineAssignments(node.m_Left, centers, owners, assignments, pc); determineAssignments(node.m_Right, centers, owners, assignments, pc); } else { assignSubToCenters(node, centers, owners, assignments);
&& getMaxRelativeNodeWidth(node.m_NodeRanges, universe) > m_MinBoxRelWidth) { m_Splitter.splitNode(node, m_NumNodes, node.m_NodeRanges, universe); m_NumNodes += 2; if (m_EuclideanDistance.valueIsSmallerEqual(inst, node.m_SplitDim, node.m_SplitValue)) { addInstanceToTree(inst, node.m_Left); afterAddInstance(node.m_Right); } else addInstanceToTree(inst, node.m_Right);
public void testSOExample() { KDTree tree = new KDTree(); LatLng Bangalore = new LatLng(12.971599, 77.594563); LatLng Delhi = new LatLng(28.635308, 77.224960); LatLng Mumbai = new LatLng(19.075984, 72.877656); LatLng Chennai = new LatLng(13.052414, 80.250825); LatLng Kolkata = new LatLng(22.572646, 88.363895); List<LatLng> cities = Arrays.asList(new LatLng[] { Bangalore, Delhi, Mumbai, Chennai, Kolkata }); KDTree.KDTNode root = tree.createTree(cities); LatLng Hyderabad = new LatLng(17.385044, 78.486671); LatLng nearestWp = tree.findNearestWp(root, Hyderabad); assertEquals(nearestWp, Bangalore); }
checkMissing(instances); if (m_EuclideanDistance == null) m_DistanceFunction = m_EuclideanDistance = new EuclideanDistance( universe); splitNodes(m_Root, universe, m_MaxDepth + 1);
checkMissing(target); findNearestNeighbours(target, m_Root, k, heap, 0.0);
boolean[] inside = new boolean[numCand]; for (int i = 0; i < numCand; i++) { distance[i] = distanceToHrect(node, centers.instance(candidates[i])); inside[i] = (distance[i] == 0.0); if (distance[i] < minDistance) { (!candidateIsFullOwner(node, owner, competitor))
/** Creates a default KDTree */ public NearestNeighbourSearch getNearestNeighbourSearch() { return new KDTree(); }
/** * Builds the KDTree on the given set of instances. * @param instances The insts on which the KDTree is to be * built. * @throws Exception If some error occurs while * building the KDTree */ public void setInstances(Instances instances) throws Exception { super.setInstances(instances); buildKDTree(instances); }
/** * Assigns instances to centers using KDTree. * * @param centers the current centers * @param assignments the centerindex for each instance * @param pc the threshold value for pruning. * @throws Exception If there is some problem * assigning instances to centers. */ public void centerInstances(Instances centers, int[] assignments, double pc) throws Exception { int[] centList = new int[centers.numInstances()]; for (int i = 0; i < centers.numInstances(); i++) centList[i] = i; determineAssignments(m_Root, centers, centList, assignments, pc); }
/** * Returns the distance between a point and an hyperrectangle. * * @param node The current node from whose hyperrectangle * the distance is to be measured. * @param x the point * @return the distance * @throws Exception If some problem occurs in determining * the distance to the hyperrectangle. */ protected double distanceToHrect(KDTreeNode node, Instance x) throws Exception { double distance = 0.0; Instance closestPoint = (Instance)x.copy(); boolean inside; inside = clipToInsideHrect(node, closestPoint); if (!inside) distance = m_EuclideanDistance.distance(closestPoint, x); return distance; }
int[] owners = refineOwners(node, centers, candidates); determineAssignments(node.m_Left, centers, owners, assignments, pc); determineAssignments(node.m_Right, centers, owners, assignments, pc); } else { assignSubToCenters(node, centers, owners, assignments);
&& getMaxRelativeNodeWidth(node.m_NodeRanges, universe) > m_MinBoxRelWidth) { m_Splitter.splitNode(node, m_NumNodes, node.m_NodeRanges, universe); m_NumNodes += 2; if (m_EuclideanDistance.valueIsSmallerEqual(inst, node.m_SplitDim, node.m_SplitValue)) { addInstanceToTree(inst, node.m_Left); afterAddInstance(node.m_Right); } else addInstanceToTree(inst, node.m_Right);
checkMissing(instances); if (m_EuclideanDistance == null) m_DistanceFunction = m_EuclideanDistance = new EuclideanDistance( universe); splitNodes(m_Root, universe, m_MaxDepth + 1);
checkMissing(target); findNearestNeighbours(target, m_Root, k, heap, 0.0);
boolean[] inside = new boolean[numCand]; for (int i = 0; i < numCand; i++) { distance[i] = distanceToHrect(node, centers.instance(candidates[i])); inside[i] = (distance[i] == 0.0); if (distance[i] < minDistance) { (!candidateIsFullOwner(node, owner, competitor))
/** Creates a default KDTree */ public NearestNeighbourSearch getNearestNeighbourSearch() { return new KDTree(); }
/** * Builds the KDTree on the given set of instances. * @param instances The insts on which the KDTree is to be * built. * @throws Exception If some error occurs while * building the KDTree */ public void setInstances(Instances instances) throws Exception { super.setInstances(instances); buildKDTree(instances); }