weka.core.neighboursearch.balltrees.BottomUpConstructor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weka-dev Show documentation
Show all versions of weka-dev Show documentation
The Waikato Environment for Knowledge Analysis (WEKA), a machine
learning workbench. This version represents the developer version, the
"bleeding edge" of development, you could say. New functionality gets added
to this version.
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* BottomUpConstructor.java
* Copyright (C) 2007-2012 University of Waikato, Hamilton, New Zealand
*/
package weka.core.neighboursearch.balltrees;
import java.util.ArrayList;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
import weka.core.TechnicalInformation;
import weka.core.TechnicalInformation.Field;
import weka.core.TechnicalInformation.Type;
import weka.core.TechnicalInformationHandler;
/**
* The class that constructs a ball tree bottom up.
*
*
* BibTeX:
*
* @techreport{Omohundro1989,
* author = {Stephen M. Omohundro},
* institution = {International Computer Science Institute},
* month = {December},
* number = {TR-89-063},
* title = {Five Balltree Construction Algorithms},
* year = {1989}
* }
*
*
*
* Valid options are:
*
* -N <value>
* Set maximum number of instances in a leaf node
* (default: 40)
*
* -R
* Set internal nodes' radius to the sum
* of the child balls radii. So that it
* contains the child balls.
*
*
* @author Ashraf M. Kibriya (amk14[at-the-rate]cs[dot]waikato[dot]ac[dot]nz)
* @version $Revision: 8034 $
*/
public class BottomUpConstructor
extends BallTreeConstructor
implements TechnicalInformationHandler {
/** for serialization. */
private static final long serialVersionUID = 5864250777657707687L;
/**
* Returns a string describing this nearest neighbour search algorithm.
*
* @return a description of the algorithm for displaying in the
* explorer/experimenter gui
*/
public String globalInfo() {
return "The class that constructs a ball tree bottom up.";
}
/**
* Returns an instance of a TechnicalInformation object, containing detailed
* information about the technical background of this class, e.g., paper
* reference or book this class is based on.
*
* @return the technical information about this class
*/
public TechnicalInformation getTechnicalInformation() {
TechnicalInformation result;
result = new TechnicalInformation(Type.TECHREPORT);
result.setValue(Field.AUTHOR, "Stephen M. Omohundro");
result.setValue(Field.YEAR, "1989");
result.setValue(Field.TITLE, "Five Balltree Construction Algorithms");
result.setValue(Field.MONTH, "December");
result.setValue(Field.NUMBER, "TR-89-063");
result.setValue(Field.INSTITUTION, "International Computer Science Institute");
return result;
}
/**
* Creates a new instance of BottomUpConstructor.
*/
public BottomUpConstructor() {
}
/**
* Builds the ball tree bottom up.
* @return The root node of the tree.
* @throws Exception If there is problem building
* the tree.
*/
public BallNode buildTree() throws Exception {
ArrayList list = new ArrayList();
for(int i=0; i list, int startIdx, int endIdx,
int[] instList) throws Exception {
double minRadius=Double.POSITIVE_INFINITY, tmpRadius;
Instance pivot, minPivot=null; int min1=-1, min2=-1;
int [] minInstList=null; int merge=1;
TempNode parent;
while(list.size() > 1) { //main merging loop
System.err.print("merge step: "+merge+++" \r");
minRadius = Double.POSITIVE_INFINITY;
min1 = -1; min2 = -1;
for(int i=0; i m_MaxInstancesInLeaf &&
(rootRadius==0 ? false : node.radius/rootRadius >= m_MaxRelLeafRadius) &&
node.left!=null && node.right!=null) { //make an internal node
ball = new BallNode(
startidx, endidx, m_NumNodes,
(pivot=BallNode.calcCentroidPivot(startidx, endidx, instList, m_Instances)),
BallNode.calcRadius(startidx, endidx, instList, m_Instances, pivot,
m_DistanceFunction)
);
m_NumNodes += 1;
ball.m_Left = makeBallTree(node.left, startidx, startidx+node.left.points.length-1, instList, depth+1, rootRadius);
ball.m_Right= makeBallTree(node.right, startidx+node.left.points.length, endidx, instList, depth+1, rootRadius);
}
else { //make a leaf node
ball = new BallNode(startidx, endidx, m_NumNodes,
(pivot=BallNode.calcCentroidPivot(startidx, endidx, instList, m_Instances)),
BallNode.calcRadius(startidx, endidx, instList, m_Instances, pivot,
m_DistanceFunction)
);
m_NumNodes += 1;
m_NumLeaves++;
}
return ball;
}
/**
* Adds an instance to the ball tree.
* @param node The root node of the tree.
* @param inst The instance to add to the tree.
* @return The new master index array after adding the
* instance.
* @throws Exception Always as BottomUpConstructor
* does not allow addition of instances after batch
* construction.
*/
public int[] addInstance(BallNode node, Instance inst) throws Exception {
throw new Exception("BottomUpConstruction method does not allow addition " +
"of new Instances.");
}
/**
* Calculates the centroid pivot of a node based on its
* two child nodes.
* @param node1 The first child node.
* @param node2 The second child node.
* @param insts The instance on which the tree is to be
* built.
* @return The centre/pivot of the node.
* @throws Exception If there is some problem calculating
* the centre/pivot of the node.
*/
public Instance calcPivot(TempNode node1, TempNode node2, Instances insts)
throws Exception {
int classIdx = m_Instances.classIndex();
double[] attrVals = new double[insts.numAttributes()];
Instance temp;
double anchr1Ratio = (double)node1.points.length /
(node1.points.length+node2.points.length),
anchr2Ratio = (double)node2.points.length /
(node1.points.length+node2.points.length);
for(int k=0; k
© 2015 - 2024 Weber Informatics LLC | Privacy Policy