📄 informationgain.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.operator.features;import edu.udo.cs.yale.operator.FatalException;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.Ontology;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.Tools;import edu.udo.cs.yale.operator.AttributeInformationGain;import edu.udo.cs.yale.operator.Operator;import java.util.LinkedList;import java.util.List;/** This {@link PopulationOperator} applies the InformationGain algorithm as described in * {@link AttributeInformationGain} to all ExampleSets. Additional population operators canuse * the generated information gain for their purposes, e.g. selection or generation. * * @see edu.udo.cs.yale.operator.AttributeInformationGain * @author ingo * @version $Id: InformationGain.java,v 2.4 2003/06/18 17:11:18 fischer Exp $ */public class InformationGain implements PopulationOperator { /** The learning operator. */ private Operator learner; /** The model applier. */ private Operator applier; /** Epsilon is the variation range for the RegressionInformationGain (in percent). Values * between 0.1% and 1% have been emerged as good. */ private double epsilon; /** Indicates whether the true or the predicted label should be used. Default is the true label. */ private boolean usePredictedLabel; /** Indicates whether or not ratio gain should be used for classification problems. */ private boolean ratioGain; /** Constructs a new InformationGain object which calculates for both classification and regression problems * the information gain */ public InformationGain(Operator learner, Operator applier, double epsilon, boolean usePredictedLabel, boolean ratioGain) { this.learner = learner; this.applier = applier; this.epsilon = epsilon; this.usePredictedLabel = usePredictedLabel; this.ratioGain = ratioGain; } /** Calculates the information gain for all attributes in this example set either using the classical * information gain or the RegressionInformationGain algorithm. */ private void calculateInformationGain(ExampleSet eSet) throws Exception { double[] informationGain = (double[])eSet.getUserData(AttributeInformationGain.INFORMATION_GAIN_KEY); // muss es ueberhaupt neu berechnet werden ? if (informationGain != null) { // Bestimmung des info gains informationGain = new double[eSet.getNumberOfAttributes()]; if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(eSet.getLabel().getValueType(), Ontology.NOMINAL)) { informationGain = Tools.getInformationGain(eSet, ratioGain); } else if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(eSet.getLabel().getValueType(), Ontology.NUMERICAL)) { // Regression --> benutze Regression Information Gain um die Werte zu bekommen. if ((learner == null) || (applier == null)) throw new FatalException("No learner or applier for information gain."); // get information gain informationGain = AttributeInformationGain.getRegressionInformationGain(eSet, epsilon, usePredictedLabel, learner, applier); } else informationGain = AttributeInformationGain.handleProblem(eSet); AttributeInformationGain.normalize(informationGain); // Setzen der Infolabel fuer die Attribute. double smallestInformationGainValue = Double.POSITIVE_INFINITY; for (int i = 0 ; i < eSet.getNumberOfAttributes(); i++) { if (informationGain[i] < smallestInformationGainValue) smallestInformationGainValue = informationGain[i]; //eSet.setInformationGain(i, informationGain[i]); } //eSet.setSmallestInformationGain(smallestInformationGainValue); eSet.setUserData(AttributeInformationGain.INFORMATION_GAIN_KEY, informationGain); eSet.setUserData(AttributeInformationGain.SMALLEST_INFORMATION_GAIN_KEY, new Double(smallestInformationGainValue)); } } /** if another individual with equal attributes is contained within the population, * the information gain values can be copied. */ private void copyInformationGain(ExampleSet target, ExampleSet source) { target.setUserData(AttributeInformationGain.INFORMATION_GAIN_KEY, source.getUserData(AttributeInformationGain.INFORMATION_GAIN_KEY)); target.setUserData(AttributeInformationGain.SMALLEST_INFORMATION_GAIN_KEY, source.getUserData(AttributeInformationGain.SMALLEST_INFORMATION_GAIN_KEY)); //for (int i = 0; i < target.getNumberOfAttributes(); i++) // target.setInformationGain(i, source.getInformationGain(i)); } /** Calculates the information gain values if necessary (i.e. not yet calculated) */ public void operate(Population population) throws Exception { int n = population.getNumberOfIndividuals(); for (int i = 0; i < n; i++) { boolean infoGainSet = false; ExampleSet target = (ExampleSet)population.get(i); for (int j = 0; j < i; j++) { ExampleSet source = (ExampleSet)population.get(j); if (target.equals(source)) { copyInformationGain(target, source); infoGainSet = true; break; } } if (!infoGainSet) calculateInformationGain(target); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -