Here you can find the source of computeEntropy(int pos, int neg)
Parameter | Description |
---|---|
pos | number of "positive" instances |
neg | number of "negative" instances |
-P(pos) * log2(P(pos)) - P(neg) * log2(P(neg))
public static double computeEntropy(int pos, int neg)
//package com.java2s; /*//from w w w. ja v a 2 s . c o m Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com) This file is part of the Semantic Discovery Toolkit. The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The Semantic Discovery Toolkit 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with The Semantic Discovery Toolkit. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Computes the entropy of two numbers. * @param pos number of "positive" instances * @param neg number of "negative" instances * @return the entropy as defined as <code>-P(pos) * log2(P(pos)) - P(neg) * log2(P(neg))</code> */ public static double computeEntropy(int pos, int neg) { if (pos == 0 || neg == 0) return 0d; final double total = (double) pos + neg; final double posRatio = pos / total; final double negRatio = neg / total; final double entropy = -posRatio * log2(posRatio) - negRatio * log2(negRatio); return entropy; } /** * Computes the entropy of <code>N</code> numbers. * @param values numbers from which to compute entropy * @return the entropy as defined as <code>Sum(-P(values[i])) * log2(P(values[i])))</code> */ public static double computeEntropy(int[] values) { double total = 0d; for (int i = 0; i < values.length; i++) { total += values[i]; } double entropy = 0d; for (int i = 0; i < values.length; i++) { final double ratio = values[i] / total; entropy += ratio == 0 ? 0d : -ratio * log2(ratio); } return entropy; } /** * Compute the Logarithm base 2 of the given value. */ public static final double log2(double d) { return Math.log(d) / Math.log(2.0); } }