Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 package org.apache.mahout.df.builder;
Builds a Decision Tree
Based on the algorithm described in the "Decision Trees" tutorials by Andrew W. Moore, available at:
 public class DefaultTreeBuilder implements TreeBuilder {

number of attributes to select randomly at each node
   private int m = 1;

IgSplit implementation
   private IgSplit igSplit;
   public DefaultTreeBuilder() {
      = new OptIgSplit();
   public void setM(int m) {
     this. = m;
   public void setIgSplit(IgSplit igSplit) {
     this. = igSplit;
   public Node build(Random rngData data) {
     if (data.isEmpty())
       return new Leaf(-1);
     if (data.isIdentical())
       return new Leaf(data.majorityLabel(rng));
     if (data.identicalLabel())
       return new Leaf(data.get(0).);
     int[] attributes = randomAttributes(data.getDataset(), rng);
     // find the best split
     Split best = null;
     for (int attr : attributes) {
       Split split = .computeSplit(dataattr);
       if (best == null || best.ig < split.ig)
         best = split;
     if (data.getDataset().isNumerical(best.attr)) {
       Data loSubset = data.subset(Condition.lesser(best.attrbest.split));
       Node loChild = build(rngloSubset);
       Data hiSubset = data.subset(Condition.greaterOrEquals(best.attr,
       Node hiChild = build(rnghiSubset);
       return new NumericalNode(best.attrbest.splitloChildhiChild);
     } else { // CATEGORICAL attribute
       double[] values = data.values(best.attr);
       Node[] childs = new Node[values.length];
       for (int index = 0; index < values.lengthindex++) {
         Data subset = data.subset(Condition.equals(best.attrvalues[index]));
         childs[index] = build(rngsubset);
      return new CategoricalNode(best.attrvalueschilds);

Randomly selects m attributes to consider for split, excludes IGNORED and LABEL attributes

m number of attributes to select
  protected static int[] randomAttributes(Dataset datasetRandom rngint m) {
    if (m > dataset.nbAttributes()) {
      throw new IllegalArgumentException("m > num attributes");
    int[] result = new int[m];
    Arrays.fill(result, -1);
    for (int index = 0; index < mindex++) {
      int rvalue;
      do {
        rvalue = rng.nextInt(dataset.nbAttributes());
      } while (ArrayUtils.contains(resultrvalue));
      result[index] = rvalue;
    return result;
New to GrepCode? Check out our FAQ X