All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.classifiers.trees.ht.ActiveHNode Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 3.9.6
Show newest 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 .
 */

/*
 *    ActiveHNode.java
 *    Copyright (C) 2013 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.classifiers.trees.ht;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weka.core.Attribute;
import weka.core.Instance;

/**
 * Node that is "active" (i.e. growth can occur) in a Hoeffding tree
 * 
 * @author Richard Kirkby ([email protected])
 * @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
 * @version $Revision: 9705 $
 */
public class ActiveHNode extends LeafNode implements LearningNode, Serializable {

  /**
   * For serialization
   */
  private static final long serialVersionUID = 3284585939739561683L;

  /** The weight of instances seen at the last split evaluation */
  public double m_weightSeenAtLastSplitEval = 0;

  /** Statistics for nominal or numeric attributes conditioned on the class */
  protected Map m_nodeStats = new HashMap();

  @Override
  public void updateNode(Instance inst) throws Exception {
    super.updateDistribution(inst);

    for (int i = 0; i < inst.numAttributes(); i++) {
      Attribute a = inst.attribute(i);
      if (i != inst.classIndex()) {
        ConditionalSufficientStats stats = m_nodeStats.get(a.name());
        if (stats == null) {
          if (a.isNumeric()) {
            stats = new GaussianConditionalSufficientStats();
          } else {
            stats = new NominalConditionalSufficientStats();
          }
          m_nodeStats.put(a.name(), stats);
        }

        stats
            .update(inst.value(a),
                inst.classAttribute().value((int) inst.classValue()),
                inst.weight());
      }
    }
  }

  /**
   * Returns a list of split candidates
   * 
   * @param splitMetric the splitting metric to use
   * @return a list of split candidates
   */
  public List getPossibleSplits(SplitMetric splitMetric) {

    List splits = new ArrayList();

    // null split
    List> nullDist = new ArrayList>();
    nullDist.add(m_classDistribution);
    SplitCandidate nullSplit = new SplitCandidate(null, nullDist,
        splitMetric.evaluateSplit(m_classDistribution, nullDist));
    splits.add(nullSplit);

    for (Map.Entry e : m_nodeStats
        .entrySet()) {
      ConditionalSufficientStats stat = e.getValue();

      SplitCandidate splitCandidate = stat.bestSplit(splitMetric,
          m_classDistribution, e.getKey());

      if (splitCandidate != null) {
        splits.add(splitCandidate);
      }
    }

    return splits;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy