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
* TwoWayNominalSplit.java
19
* Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
23
package weka.classifiers.trees.adtree;
25
import weka.core.Attribute;
26
import weka.core.Instance;
27
import weka.core.Instances;
29
import java.util.Enumeration;
32
* Class representing a two-way split on a nominal attribute, of the form:
33
* either 'is some_value' or 'is not some_value'.
35
* @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
36
* @version $Revision: 1.5 $
38
public class TwoWayNominalSplit
41
/** for serialization */
42
private static final long serialVersionUID = -4598366190152721355L;
44
/** The index of the attribute the split depends on */
47
/** The attribute value that is compared against */
48
private int trueSplitValue;
50
/** The children of this split */
51
private PredictionNode[] children;
54
* Creates a new two-way nominal splitter.
56
* @param _attIndex the index of the attribute this split depeneds on
57
* @param _trueSplitValue the attribute value that the splitter splits on
59
public TwoWayNominalSplit(int _attIndex, int _trueSplitValue) {
61
attIndex = _attIndex; trueSplitValue = _trueSplitValue;
62
children = new PredictionNode[2];
66
* Gets the number of branches of the split.
68
* @return the number of branches (always = 2)
70
public int getNumOfBranches() {
76
* Gets the index of the branch that an instance applies to. Returns -1 if no branches
79
* @param inst the instance
80
* @return the branch index
82
public int branchInstanceGoesDown(Instance inst) {
84
if (inst.isMissing(attIndex)) return -1;
85
else if (inst.value(attIndex) == trueSplitValue) return 0;
90
* Gets the subset of instances that apply to a particluar branch of the split. If the
91
* branch index is -1, the subset will consist of those instances that don't apply to
94
* @param branch the index of the branch
95
* @param instances the instances from which to find the subset
96
* @return the set of instances that apply
98
public ReferenceInstances instancesDownBranch(int branch, Instances instances) {
100
ReferenceInstances filteredInstances = new ReferenceInstances(instances, 1);
102
for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) {
103
Instance inst = (Instance) e.nextElement();
104
if (inst.isMissing(attIndex)) filteredInstances.addReference(inst);
106
} else if (branch == 0) {
107
for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) {
108
Instance inst = (Instance) e.nextElement();
109
if (!inst.isMissing(attIndex) && inst.value(attIndex) == trueSplitValue)
110
filteredInstances.addReference(inst);
113
for (Enumeration e = instances.enumerateInstances(); e.hasMoreElements(); ) {
114
Instance inst = (Instance) e.nextElement();
115
if (!inst.isMissing(attIndex) && inst.value(attIndex) != trueSplitValue)
116
filteredInstances.addReference(inst);
119
return filteredInstances;
123
* Gets the string describing the attributes the split depends on.
124
* i.e. the left hand side of the description of the split.
126
* @param dataset the dataset that the split is based on
127
* @return a string describing the attributes
129
public String attributeString(Instances dataset) {
131
return dataset.attribute(attIndex).name();
135
* Gets the string describing the comparision the split depends on for a particular
136
* branch. i.e. the right hand side of the description of the split.
138
* @param branchNum the branch of the split
139
* @param dataset the dataset that the split is based on
140
* @return a string describing the comparison
142
public String comparisonString(int branchNum, Instances dataset) {
144
Attribute att = dataset.attribute(attIndex);
145
if (att.numValues() != 2)
146
return ((branchNum == 0 ? "= " : "!= ") + att.value(trueSplitValue));
147
else return ("= " + (branchNum == 0 ?
148
att.value(trueSplitValue) :
149
att.value(trueSplitValue == 0 ? 1 : 0)));
153
* Tests whether two splitters are equivalent.
155
* @param compare the splitter to compare with
156
* @return whether or not they match
158
public boolean equalTo(Splitter compare) {
160
if (compare instanceof TwoWayNominalSplit) { // test object type
161
TwoWayNominalSplit compareSame = (TwoWayNominalSplit) compare;
162
return (attIndex == compareSame.attIndex &&
163
trueSplitValue == compareSame.trueSplitValue);
168
* Sets the child for a branch of the split.
170
* @param branchNum the branch to set the child for
171
* @param childPredictor the new child
173
public void setChildForBranch(int branchNum, PredictionNode childPredictor) {
175
children[branchNum] = childPredictor;
179
* Gets the child for a branch of the split.
181
* @param branchNum the branch to get the child for
184
public PredictionNode getChildForBranch(int branchNum) {
186
return children[branchNum];
190
* Clones this node. Performs a deep copy, recursing through the tree.
194
public Object clone() {
196
TwoWayNominalSplit clone = new TwoWayNominalSplit(attIndex, trueSplitValue);
197
clone.orderAdded = orderAdded;
198
if (children[0] != null)
199
clone.setChildForBranch(0, (PredictionNode) children[0].clone());
200
if (children[1] != null)
201
clone.setChildForBranch(1, (PredictionNode) children[1].clone());