2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* TwoWayNumericSplit.java
19
* Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
23
package weka.classifiers.trees.adtree;
25
import weka.core.Instance;
26
import weka.core.Instances;
27
import weka.core.Utils;
29
import java.util.Enumeration;
32
* Class representing a two-way split on a numeric attribute, of the form:
33
* either 'is < some_value' or 'is >= some_value'.
35
* @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
36
* @version $Revision: 1.5 $
38
public class TwoWayNumericSplit
41
/** for serialization */
42
private static final long serialVersionUID = 449769177903158283L;
44
/** The index of the attribute the split depends on */
47
/** The attribute value that is compared against */
48
private double splitPoint;
50
/** The children of this split */
51
private PredictionNode[] children;
54
* Creates a new two-way numeric splitter.
56
* @param _attIndex the index of the attribute this split depeneds on
57
* @param _splitPoint the attribute value that the splitter splits on
59
public TwoWayNumericSplit(int _attIndex, double _splitPoint) {
62
splitPoint = _splitPoint;
63
children = new PredictionNode[2];
67
* Gets the number of branches of the split.
69
* @return the number of branches (always = 2)
71
public int getNumOfBranches() {
77
* Gets the index of the branch that an instance applies to. Returns -1 if no branches
80
* @param inst the instance
81
* @return the branch index
83
public int branchInstanceGoesDown(Instance inst) {
85
if (inst.isMissing(attIndex)) return -1;
86
else if (inst.value(attIndex) < splitPoint) return 0;
91
* Gets the subset of instances that apply to a particluar branch of the split. If the
92
* branch index is -1, the subset will consist of those instances that don't apply to
95
* @param branch the index of the branch
96
* @param instances the instances from which to find the subset
97
* @return the set of instances that apply
99
public ReferenceInstances instancesDownBranch(int branch, Instances instances) {
101
ReferenceInstances filteredInstances = new ReferenceInstances(instances, 1);
103
for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) {
104
Instance inst = (Instance) e.nextElement();
105
if (inst.isMissing(attIndex)) filteredInstances.addReference(inst);
107
} else if (branch == 0) {
108
for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) {
109
Instance inst = (Instance) e.nextElement();
110
if (!inst.isMissing(attIndex) && inst.value(attIndex) < splitPoint)
111
filteredInstances.addReference(inst);
114
for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) {
115
Instance inst = (Instance) e.nextElement();
116
if (!inst.isMissing(attIndex) && inst.value(attIndex) >= splitPoint)
117
filteredInstances.addReference(inst);
120
return filteredInstances;
124
* Gets the string describing the attributes the split depends on.
125
* i.e. the left hand side of the description of the split.
127
* @param dataset the dataset that the split is based on
128
* @return a string describing the attributes
130
public String attributeString(Instances dataset) {
132
return dataset.attribute(attIndex).name();
136
* Gets the string describing the comparision the split depends on for a particular
137
* branch. i.e. the right hand side of the description of the split.
139
* @param branchNum the branch of the split
140
* @param dataset the dataset that the split is based on
141
* @return a string describing the comparison
143
public String comparisonString(int branchNum, Instances dataset) {
145
return ((branchNum == 0 ? "< " : ">= ") + Utils.doubleToString(splitPoint, 3));
149
* Tests whether two splitters are equivalent.
151
* @param compare the splitter to compare with
152
* @return whether or not they match
154
public boolean equalTo(Splitter compare) {
156
if (compare instanceof TwoWayNumericSplit) { // test object type
157
TwoWayNumericSplit compareSame = (TwoWayNumericSplit) compare;
158
return (attIndex == compareSame.attIndex &&
159
splitPoint == compareSame.splitPoint);
164
* Sets the child for a branch of the split.
166
* @param branchNum the branch to set the child for
167
* @param childPredictor the new child
169
public void setChildForBranch(int branchNum, PredictionNode childPredictor) {
171
children[branchNum] = childPredictor;
175
* Gets the child for a branch of the split.
177
* @param branchNum the branch to get the child for
180
public PredictionNode getChildForBranch(int branchNum) {
182
return children[branchNum];
186
* Clones this node. Performs a deep copy, recursing through the tree.
190
public Object clone() {
192
TwoWayNumericSplit clone = new TwoWayNumericSplit(attIndex, splitPoint);
193
clone.orderAdded = orderAdded;
194
if (children[0] != null)
195
clone.setChildForBranch(0, (PredictionNode) children[0].clone());
196
if (children[1] != null)
197
clone.setChildForBranch(1, (PredictionNode) children[1].clone());