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

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

/*
 *   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 .
 */

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

package weka.classifiers.trees.ht;

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

/**
 * Implements the gini splitting criterion
 * 
 * @author Richard Kirkby ([email protected])
 * @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
 * @version $Revision: 9720 $
 */
public class GiniSplitMetric extends SplitMetric implements Serializable {

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

  @Override
  public double evaluateSplit(Map preDist,
      List> postDist) {
    double totalWeight = 0.0;
    double[] distWeights = new double[postDist.size()];

    for (int i = 0; i < postDist.size(); i++) {
      distWeights[i] = SplitMetric.sum(postDist.get(i));
      totalWeight += distWeights[i];
    }
    double gini = 0;
    for (int i = 0; i < postDist.size(); i++) {
      gini += (distWeights[i] / totalWeight)
          * gini(postDist.get(i), distWeights[i]);
    }

    return 1.0 - gini;
  }

  /**
   * Return the gini metric computed from the supplied distribution
   * 
   * @param dist the distribution to compute the gini metric from
   * @param sumOfWeights the sum of the distribution weights
   * @return the gini metric
   */
  protected static double gini(Map dist, double sumOfWeights) {
    double gini = 1.0;

    for (Map.Entry e : dist.entrySet()) {
      double frac = e.getValue().m_weight / sumOfWeights;
      gini -= frac * frac;
    }

    return gini;
  }

  /**
   * Return the gini metric computed from the supplied distribution
   * 
   * @param dist dist the distribution to compute the gini metric from
   * @return
   */
  public static double gini(Map dist) {
    return gini(dist, SplitMetric.sum(dist));
  }

  @Override
  public double getMetricRange(Map preDist) {
    return 1.0;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy