📄 wekaattributeweighting.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.features.weighting;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.AttributeWeights;
import edu.udo.cs.yale.tools.WekaTools;
import edu.udo.cs.yale.tools.LogService;
import weka.attributeSelection.AttributeEvaluator;
import weka.attributeSelection.ASEvaluation;
import weka.core.Instances;
import java.util.List;
/** Performs one of Weka's AttributeEvaluator classes to determine a sort of attribute relevance.
* These relevance values build an instance of AttributeWeights. Therefore, they can be used by
* other operators which make use of such weights, like weight based selection or search heuristics
* which use attribute weights to speed up the search.
*
* @version $Id: WekaAttributeWeighting.java,v 1.2 2004/09/01 12:39:50 ingomierswa Exp $
*/
public class WekaAttributeWeighting extends Operator {
public static final String[] WEKA_ATTRIBUTE_EVALUATORS = WekaTools.getWekaClasses(AttributeEvaluator.class);
public IOObject[] apply() throws OperatorException {
ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class);
AttributeWeights weights = new AttributeWeights();
String operatorName = getWekaLearnerName();
String[] parameters = getWekaParameters();
AttributeEvaluator evaluator = null;
try {
evaluator = (AttributeEvaluator)ASEvaluation.forName(operatorName, parameters);
} catch (Exception e) {
throw new UserError(this, e, 904, new Object[] { operatorName, e});
}
LogService.logMessage(getName() + ": Converting to Weka instances.", LogService.MINIMUM);
Instances instances = WekaTools.toWekaInstances(exampleSet, "TempInstances", exampleSet.getLabel(), true);
try {
LogService.logMessage(getName() + ": Building Weka attribute evaluator.", LogService.MINIMUM);
evaluator.buildEvaluator(instances);
} catch (Exception e) {
throw new UserError(this, e, 905, new Object[] {operatorName, e});
}
for (int i = 0; i < exampleSet.getNumberOfAttributes(); i++) {
Attribute attribute = exampleSet.getAttribute(i);
try {
double result = evaluator.evaluateAttribute(i);
weights.setWeight(attribute.getName(), result);
} catch (Exception e) {
LogService.logMessage(getName() + ": Cannot evaluate attribute '" + attribute.getName() +
"', use unknown weight.", LogService.WARNING);
}
}
return new IOObject[] { exampleSet, weights };
}
public Class[] getOutputClasses() {
return new Class[] { ExampleSet.class, AttributeWeights.class };
}
public Class[] getInputClasses() {
return new Class[] { ExampleSet.class };
}
public String getWekaLearnerName() {
return getParameterAsString("weka_attribute_evaluator");
}
public String[] getWekaParameters() {
List wekaParameters = getParameterList("weka_parameters");
return WekaTools.getWekaParameters(wekaParameters);
}
public List getParameterTypes() {
List types = super.getParameterTypes();
ParameterType type = new ParameterTypeStringCategory("weka_attribute_evaluator",
"The fully qualified classname of the Weka attribute evaluator.",
WEKA_ATTRIBUTE_EVALUATORS);
type.setExpert(false);
types.add(type);
types.add(new ParameterTypeList("weka_parameters", "Parameters for the Weka attribute evaluator as described in the Weka manual.", new ParameterTypeString(null, null)));
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -