📄 corpusbasedfeatureweighting.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 java.util.*;
import edu.udo.cs.yale.example.*;
import edu.udo.cs.yale.operator.*;
import edu.udo.cs.yale.operator.parameter.*;
/** This operator uses a corpus of examples to characterize a single class by setting feature weights.
* Characteristic features receive higher weights than less characteristic features. The weight for
* a feature is determined by calculating the average value of this feature for all examples of the
* target class. This operator assumes that the feature values characterize the importance of this feature
* for an example (e.g. TFIDF or others). Therefore, this operator is mainly used on textual data based on
* TFIDF weighting schemes. To extract such feature values from text collections you can use the Word Vector
* Tool plugin.
*
* @author Michael Wurst, Ingo Mierswa
* @version $Id: CorpusBasedFeatureWeighting.java,v 1.2 2004/09/17 16:47:16 ingomierswa Exp $
*/
public class CorpusBasedFeatureWeighting extends Operator{
private static String TARGET_VALUE_NAME = "class_to_characterize";
private double epsilon = 0.001;
/**
* @see edu.udo.cs.yale.operator.Operator#apply()
*/
public IOObject[] apply() throws OperatorException {
ExampleSet es = (ExampleSet) getInput(ExampleSet.class, false);
String targetValue = getParameterAsString(TARGET_VALUE_NAME);
double[] weights = generateWeightsForClass(es, targetValue);
double maxWeight = Double.NEGATIVE_INFINITY;
for (int i = 0; i < weights.length; i++)
if (weights[i] > maxWeight)
maxWeight = weights[i];
maxWeight += epsilon;
AttributeWeights attWeights = new AttributeWeights();
for(int i = 0; i < es.getNumberOfAttributes(); i++)
if(weights[i] > 0.0)
attWeights.setWeight(es.getAttribute(i).getName(), weights[i]/ maxWeight);
else
attWeights.setWeight(es.getAttribute(i).getName(), -1.0);
return new IOObject[]{attWeights};
}
/**
* @see edu.udo.cs.yale.operator.Operator#getInputClasses()
*/
public Class[] getInputClasses() {
return new Class[]{ExampleSet.class};
}
/**
* @see edu.udo.cs.yale.operator.Operator#getOutputClasses()
*/
public Class[] getOutputClasses() {
return new Class[]{ExampleSet.class, AttributeWeights.class};
}
private double[] generateWeightsForClass(ExampleSet es, String value) {
double[] result = new double[es.getNumberOfAttributes()];
for(int i = 0; i < es.getNumberOfAttributes(); i++)
result[i] = 0.0;
ExampleReader er = es.getExampleReader();
while(er.hasNext()) {
Example e = er.next();
if(e.getLabelAsString().equalsIgnoreCase(value)) {
for(int i = 0; i < es.getNumberOfAttributes(); i++)
result[i] = result[i] + e.getValue(i);
}
}
return result;
}
public List getParameterTypes() {
List types = super.getParameterTypes();
types.add(new ParameterTypeString(TARGET_VALUE_NAME,
"The target class for which to find characteristic feature weights.",
false));
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -