📄 inequalitysplitterbuilder.java
字号:
} return result; } /** * The constructor for the root splitterbuilder * @param index the index of the relevant attribute * @param booster the booster that is to be used by this builder */ InequalitySplitterBuilder(int index, Booster booster, AttributeDescription[] ad) { isRoot= true; isFinalized= false; attributeIndex= index; this.booster= booster; tempList= new ArrayList(); largestIndex= -1; desc= ad; m_type= SplitterType.INEQUALITY_SPLITTER; } /** * Add a single example to the internal data structure * @param index the index of the example in the dataset * @param example the example */ public void addExample(int index, Example example) throws IncompAttException { if (!isRoot || isFinalized) throw new RuntimeException("Attempt to addExample() to non-root or finalized SplitterBuilder"); RealAttribute a= null; Attribute t= example.getAttribute(attributeIndex); // check that attribute is of the correct class try { a= (RealAttribute) t; // try downcasting } catch (ClassCastException e) { throw new IncompAttException( index, attributeIndex, "RealAttribute", t.getClass()); } Label l= example.getLabel(); largestIndex= (largestIndex > index) ? largestIndex : index; // remember the largest index seen // if(Monitor.logLevel>3) Monitor.log("In RootInequalitySplitterBuilder: a="+a+" t="+t // +" example="+example); if (a.isDefined() && (l != null)) { // check if attribute and label are defined IVL ivl= new IVL(index, a.getValue(), l); tempList.add(ivl); // add item to list } // if(Monitor.logLevel>3) Monitor.log("at end of addExample("+index+")\n"+tempList); } /** Initializes attributeValues and sortedIndices */ public void finalizeData() { if (!isRoot || isFinalized) throw new RuntimeException("Attempt to finalizeData() to non-root or finalized SplitterBuilder"); // if(Monitor.logLevel>3) Monitor.log(tempList); Collections.sort(tempList); // if(Monitor.logLevel>3) Monitor.log(tempList); sortedListOwner= true; indexedValues= new double[largestIndex + 1]; int size= tempList.size(); sortedIndices= new int[size]; potentialSplits= new boolean[size]; if (size > 0) { IVL element= (IVL) tempList.get(0); double prevValue= element.value; Label prevLabel= element.label; boolean labelChange= false; boolean valueChange= false; int firstItemNewValue= 0; for (int i= 0; i < size; i++) { IVL iv= (IVL) tempList.get(i); indexedValues[iv.index]= iv.value; sortedIndices[i]= iv.index; // a potential split can occur when the value chages and // the m_labels associated with the previous and the current value // are different potentialSplits[i]= false; // default is false labelChange |= !iv.label.equals(prevLabel); valueChange |= (iv.value != prevValue); // if(Monitor.logLevel>3) Monitor.log("start "+i+ // "\tprevLabel="+prevLabel+ // "\tiv.label="+iv.label+ // "\tlabelChange="+labelChange+ // "\tprevValue="+prevValue+ // "\tiv.value="+iv.value+ // "\tvalueChange="+valueChange); if (valueChange) { if (labelChange) { // label has changed since last split point potentialSplits[i]= true; labelChange= false; } else { // label has not changed, but might change // before next value change, in which case // we want to come back here and mark it as // a potential split point firstItemNewValue= i; } valueChange= false; } else { if (labelChange) { // label changed without the value changing - // mark previous potential split point, // keep labelChange on for the next value change potentialSplits[firstItemNewValue]= true; } } prevValue= iv.value; prevLabel= iv.label; // if(Monitor.logLevel>3) Monitor.log("end "+i+ // "\tlabelChange="+labelChange+ // "\tvalueChange="+valueChange+ // "\tfirstItemNewValue="+firstItemNewValue); } } tempList.clear(); // free the memory isFinalized= true; } //----------------------------- Protected Members -------------------------------------// /** The index of the attribute on which this builder works */ protected int attributeIndex; /** The indices of (a superset of) the examples that reach this node, sorted by attribute value. */ protected int[] sortedIndices; /** true if all of the elements in sortedExamples are relevant to this builder*/ protected boolean sortedListOwner; /** The value of the attribute for all of the examples. The values are kept in their original order, the entries that corresponds to undefined attribute values are not initialized. One copy of this is generated by the root splitterBuilder and is pointed to by all of its descendents */ protected double[] indexedValues; /** a flag for each element in sortedIndices, indicating whether or not the att. value of this example is different from the value of the previous (unmasked) example in sortedIndices */ protected boolean[] potentialSplits; //----------------------------- Private Members -------------------------------------// /** an internal class defining the elements of {@link tempList} */ class IVL implements Comparable { IVL(int i, double v, Label l) { index= i; value= v; label= l; } public int compareTo(Object o) { // defined so that we can use "Collections.sort" double value2= ((IVL) o).value; if (value < value2) { return -1; } else if (value > value2) { return 1; } else { return 0; } } public String toString() { return ( "index=" + index + ", value=" + value + ", label=" + label + "\n"); } int index; double value; Label label; } /** a temporary storage for the values and indices of the * attributes. This storage is freed up when the builder is finalized. */ private List tempList; /** the largest example index seen */ private int largestIndex; //-------------------------------- Test Stuff ---------------------------------------// /** A main for testing this class */ /* static public void main(String[] argv) { try { int[] labels= { 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0 }; double[] values= { 1.0, 2.0, 2.0, 2.0, 1.1, 2.1, 5.5, -3.2, 0.0, 53.2, 0.2, 1.1 }; AbstractBooster booster= AbstractBooster.getInstance(); InequalitySplitterBuilder sb= new InequalitySplitterBuilder( 0, booster, new AttributeDescription[] { null }); Example x; Attribute[] attArray= new Attribute[1]; Label l; if (Monitor.logLevel > 3) Monitor.log("Input: \t index \t value \t label"); for (int i= 0; i < labels.length; i++) { l= new Label(labels[i]); attArray[0]= new RealAttribute(values[i]); x= new Example(attArray, l); if (Monitor.logLevel > 3) Monitor.log( " \t " + i + "\t " + values[i] + "\t " + labels[i]); try { sb.addExample(i, x); booster.addExample(i, l); } catch (IncompAttException e) { if (Monitor.logLevel > 3) Monitor.log(e.getMessage()); } } booster.finalizeData(); if (Monitor.logLevel > 3) Monitor.log(booster); sb.finalizeData(); if (Monitor.logLevel > 3) Monitor.log("\n\nOutput:"); if (Monitor.logLevel > 3) Monitor.log(sb); if (Monitor.logLevel > 3) Monitor.log("checking building"); CandidateSplit h= sb.build(); if (Monitor.logLevel > 3) Monitor.log( "\n===\n checking detection of incompatible attribute"); sb= new InequalitySplitterBuilder( 0, booster, new AttributeDescription[] { null }); l= new Label(0); attArray[0]= new DiscreteAttribute(0); x= new Example(attArray, l); try { sb.addExample((int) 0, x); } catch (IncompAttException e) { if (Monitor.logLevel > 3) Monitor.log(e.getMessage()); } } catch (Exception e) { if (Monitor.logLevel > 3) Monitor.log(e.getMessage()); e.printStackTrace(); } finally { if (Monitor.logLevel > 3) Monitor.log("finished testing InequalitySplitterBuilder"); } } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -