📄 examplesetinformationoperator.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;import edu.udo.cs.yale.operator.parameter.*;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.ExampleReader;import edu.udo.cs.yale.example.Example;import edu.udo.cs.yale.example.Attribute;import java.util.Collections;import java.util.Iterator;import java.util.List;import java.util.LinkedList;/** Provides some statistical information about the example set in human readable form. * Useless attributes, e.g. attributes having only one value can be automatically removed. * * @version $Id: ExampleSetInformationOperator.java,v 2.4 2003/07/03 16:01:30 fischer Exp $ */public class ExampleSetInformationOperator extends Operator { private static final Class[] INPUT_CLASSES = { ExampleSet.class }; private static final Class[] OUTPUT_CLASSES = { ExampleSetInformation.class, ExampleSet.class }; private static class AttributeInformation { protected Attribute attribute; private AttributeInformation(Attribute attribute) { this.attribute = attribute; } public String toString() { return attribute.getName() + ": "; } } private static class NominalAttributeInformation extends AttributeInformation implements Comparable { private int[] distribution; private NominalAttributeInformation(Attribute attribute, int[] distribution) { super(attribute); this.distribution = distribution; } private boolean isUseless() { int numNotNull = 0; for (int i = 0; i < distribution.length; i++) { if (distribution[i] > 0) numNotNull++; } return !(numNotNull >= 2); } public String toString() { String str = super.toString(); str += " ("; for (int j = 0; j < attribute.getNumberOfClasses(); j++) { if (j > 0) str += "/"; str += attribute.mapIndex(j+Attribute.FIRST_CLASS_INDEX); } str += ") -> "; for (int j = 0; j < distribution.length; j++) { if (j > 0) str += "/"; str += distribution[j]; } return str; } public String toHTML(int rank) { String str = "<tr><td>"+rank+"</td><td>"+attribute.getName()+"</td><td>"; for (int j = 0; j < attribute.getNumberOfClasses(); j++) { if (j > 0) str += "/"; str += attribute.mapIndex(j+Attribute.FIRST_CLASS_INDEX); } str += "</td><td>"; for (int j = 0; j < distribution.length; j++) { if (j > 0) str += "/"; str += distribution[j]; } str += "</td></tr>"; return str; } public int compareTo(Object o) { return this.distribution[0] - ((NominalAttributeInformation)o).distribution[0]; } } private static class ExampleSetInformation extends ResultObjectAdapter { private List nominalList = new LinkedList(); public String getName() { return "ExampleSet info"; } private void addNominalInformation(NominalAttributeInformation nai) { nominalList.add(nai); } private void sort() { Collections.sort(nominalList); } public String toString() { return "Information about "+nominalList.size()+" nominal attributes"; } public String toResultString() { StringBuffer str = new StringBuffer("Nominal attributes (value distribution):\n"); Iterator i = nominalList.iterator(); while (i.hasNext()) { NominalAttributeInformation nai = (NominalAttributeInformation)i.next(); str.append(nai.toString() + "\n"); } return str.toString(); } public String toHTML() { StringBuffer str = new StringBuffer("<html><h2>Nominal attributes:</h2>"); str.append("<table border=\"1\">"); str.append("<tr><th>Rank</th><th>Attribute name</th><th>classes</th><th>distribution</th></tr>"); Iterator i = nominalList.iterator(); int j = 1; while (i.hasNext()) { NominalAttributeInformation nai = (NominalAttributeInformation)i.next(); str.append(nai.toHTML(j++)); } str.append("</table></html>"); return str.toString(); } public java.awt.Component getVisualisationComponent() { javax.swing.JLabel label = new javax.swing.JLabel(toHTML()); label.setFont(label.getFont().deriveFont(java.awt.Font.PLAIN)); return label; } } public IOObject[] apply() throws OperatorException { ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class); ExampleSet clone = (ExampleSet)exampleSet.clone(); ExampleReader reader = exampleSet.getExampleReader(); int[][] columnValueCount = new int[exampleSet.getNumberOfAttributes()][]; for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) { Attribute attribute = exampleSet.getAttribute(i); if (attribute.isNominal()) { columnValueCount[i] = new int[attribute.getNumberOfClasses()]; } else { } } while (reader.hasNext()) { Example example = reader.next(); for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) { Attribute attribute = exampleSet.getAttribute(i); if (attribute.isNominal()) { columnValueCount[i][(int)example.getValue(attribute) - Attribute.FIRST_CLASS_INDEX]++; } else { } } } boolean remove = getParameterAsBoolean("remove_useless_attributes"); ExampleSetInformation esi = new ExampleSetInformation(); for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) { Attribute attribute = exampleSet.getAttribute(i); if (attribute.isNominal()) { NominalAttributeInformation nai = new NominalAttributeInformation(attribute, columnValueCount[i]); esi.addNominalInformation(nai); if (remove && nai.isUseless()) { clone.removeAttribute(attribute); } } else { if (attribute.getMinimum() == attribute.getMaximum()) clone.removeAttribute(attribute); } } esi.sort(); return new IOObject[] { esi, clone }; } public Class[] getInputClasses() { return INPUT_CLASSES; } public Class[] getOutputClasses() { return OUTPUT_CLASSES; } public List getParameterTypes() { List types = super.getParameterTypes(); types.add(new ParameterTypeBoolean("remove_useless_attributes", "If this parameter is set to true, useless attributes are removed.")); return types; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -