📄 removeuselessattributes.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* 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.preprocessing;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.tools.LogService;
import java.util.List;
import java.util.Collection;
import java.util.Iterator;
/** Removes useless attribute from the example set. Useless attributes are
* <ul>
* <li>nominal attributes which has the same value for more than <code>p</code> percent of all examples.</li>
* <li>numerical attributes which standard deviation is less or equal to a given deviation threshold <code>t</code>.</li>
* </ul>
*
* @version $Id: RemoveUselessAttributes.java,v 1.4 2004/08/28 17:04:49 ingomierswa Exp $
*/
public class RemoveUselessAttributes extends Operator {
private static final Class[] INPUT_CLASSES = { ExampleSet.class };
private static final Class[] OUTPUT_CLASSES = { ExampleSet.class };
public IOObject[] apply() throws OperatorException {
ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class);
ExampleSet clone = (ExampleSet)exampleSet.clone();
double numericalMinDeviation = getParameterAsDouble("numerical_min_deviation");
double nominalMaxSingleValue = getParameterAsDouble("nominal_max_single_value");
for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {
Attribute attribute = exampleSet.getAttribute(i);
if (attribute.isNominal()) {
Collection values = attribute.getValues();
double[] valueCounts = new double[values.size()];
Iterator v = values.iterator();
int n = 0;
while (v.hasNext()) {
String value = (String)v.next();
valueCounts[n] = attribute.getValueCount(value);
n++;
}
for (n = 0; n < valueCounts.length; n++) {
double percent = valueCounts[n] / (double)exampleSet.getSize();
if (percent >= nominalMaxSingleValue) {
clone.removeAttribute(attribute);
break;
}
}
} else {
if (Math.sqrt(attribute.getVariance()) <= numericalMinDeviation)
clone.removeAttribute(attribute);
}
}
if (clone.getNumberOfAttributes() <= 0) {
LogService.logMessage("Example set does not not have any attribute after removing the useless attributes!", LogService.WARNING);
}
return new IOObject[] { clone };
}
public Class[] getInputClasses() { return INPUT_CLASSES; }
public Class[] getOutputClasses() { return OUTPUT_CLASSES; }
public List getParameterTypes() {
List types = super.getParameterTypes();
ParameterType type = new ParameterTypeDouble("numerical_min_deviation",
"Removes all numerical attributes with standard deviation less or equal to this threshold.",
0.0d, Double.POSITIVE_INFINITY, 0.0d);
type.setExpert(false);
types.add(type);
type = new ParameterTypeDouble("nominal_max_single_value",
"Removes all nominal attributes which provides more than the given amount of only one value.",
0.0d, 1.0d, 1.0d);
type.setExpert(false);
types.add(type);
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -