📄 onerattributeeval.java
字号:
/**
*
* AgentAcademy - an open source Data Mining framework for
* training intelligent agents
*
* Copyright (C) 2001-2003 AA Consortium.
*
* This library is open source 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 2.0 of the License, or (at your option) any later
* version.
*
* This library 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 Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
package org.agentacademy.modules.dataminer.attributeSelection;
import org.agentacademy.modules.dataminer.classifiers.evaluation.Evaluation;
import org.agentacademy.modules.dataminer.core.Instances;
import org.agentacademy.modules.dataminer.filters.AttributeFilter;
import org.agentacademy.modules.dataminer.filters.Filter;
import org.apache.log4j.Logger;
/**
* Class for Evaluating attributes individually by using the OneR
* classifier. <p>
*
* No options. <p>
*
* @author Mark Hall (mhall@cs.waikato.ac.nz)
* @version $Revision: 1.3 $
*/
public class OneRAttributeEval
extends AttributeEvaluator
{
public static Logger log = Logger.getLogger(OneRAttributeEval.class);
/** The training instances */
private Instances m_trainInstances;
/** The class index */
private int m_classIndex;
/** The number of attributes */
private int m_numAttribs;
/** The number of instances */
private int m_numInstances;
/**
* Returns a string describing this attribute evaluator
* @return a description of the evaluator suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return "OneRAttributeEval :\n\nEvaluates the worth of an attribute by "
+"using the OneR classifier.\n";
}
/**
* Constructor
*/
public OneRAttributeEval () {
resetOptions();
}
/**
* Initializes an information gain attribute evaluator.
* Discretizes all attributes that are numeric.
*
* @param data set of instances serving as training data
* @exception Exception if the evaluator has not been
* generated successfully
*/
public void buildEvaluator (Instances data)
throws Exception
{
m_trainInstances = data;
if (m_trainInstances.checkForStringAttributes()) {
throw new Exception("Can't handle string attributes!");
}
m_classIndex = m_trainInstances.classIndex();
m_numAttribs = m_trainInstances.numAttributes();
m_numInstances = m_trainInstances.numInstances();
if (m_trainInstances.attribute(m_classIndex).isNumeric()) {
throw new Exception("Class must be nominal!");
}
}
/**
* rests to defaults.
*/
protected void resetOptions () {
m_trainInstances = null;
}
/**
* evaluates an individual attribute by measuring the amount
* of information gained about the class given the attribute.
*
* @param attribute the index of the attribute to be evaluated
* @exception Exception if the attribute could not be evaluated
*/
public double evaluateAttribute (int attribute)
throws Exception
{
int[] featArray = new int[2]; // feat + class
double errorRate;
Evaluation o_Evaluation;
AttributeFilter delTransform = new AttributeFilter();
delTransform.setInvertSelection(true);
// copy the instances
Instances trainCopy = new Instances(m_trainInstances);
featArray[0] = attribute;
featArray[1] = trainCopy.classIndex();
delTransform.setAttributeIndicesArray(featArray);
delTransform.setInputFormat(trainCopy);
trainCopy = Filter.useFilter(trainCopy, delTransform);
o_Evaluation = new Evaluation(trainCopy);
o_Evaluation.crossValidateModel("weka.classifiers.OneR", trainCopy, 10, null);
errorRate = o_Evaluation.errorRate();
return (1 - errorRate)*100.0;
}
/**
* Return a description of the evaluator
* @return description as a string
*/
public String toString () {
StringBuffer text = new StringBuffer();
if (m_trainInstances == null) {
text.append("\tOneR feature evaluator has not been built yet");
}
else {
text.append("\tOneR feature evaluator");
}
text.append("\n");
return text.toString();
}
// ============
// Test method.
// ============
/**
* Main method for testing this class.
*
* @param args the options
*/
public static void main (String[] args) {
try {
System.out.println(AttributeSelection.
SelectAttributes(new OneRAttributeEval(), args));
}
catch (Exception e) {
log.error(e.getStackTrace().toString());
log.error(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -