📄 wekaclassifier.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.learner.weka;
import edu.udo.cs.yale.operator.learner.SerializableModel;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.WekaTools;
import weka.core.Instances;
import weka.core.Instance;
import weka.core.Drawable;
import weka.gui.treevisualizer.TreeDisplayListener;
import weka.gui.treevisualizer.TreeDisplayEvent;
import weka.gui.treevisualizer.PlaceNode2;
import weka.gui.treevisualizer.TreeVisualizer;
import weka.classifiers.Classifier;
import java.awt.Component;
/** A Weka {@link weka.classifiers.Classifier} which can be used to classify
* {@link Example}s. It is learned by a {@link WekaLearner}.
*
* @author ingo
* @version $Id: WekaClassifier.java,v 1.8 2004/08/27 11:57:42 ingomierswa Exp $
*/
public class WekaClassifier extends SerializableModel {
/** The used weka classifier. */
private Classifier classifier;
/** Set to true iff this classifier is a distribution classifier which should deliver a
* distribution instead of a classification value. The predicted label should be the confidence
* (and not the class index). */
private boolean useDistributionClassifier = false;
protected WekaClassifier() { super(); }
public WekaClassifier(Attribute label) {
super(label);
}
public WekaClassifier(Attribute label, Classifier classifier) {
this(label, classifier, false);
}
public WekaClassifier(Attribute label, Classifier classifier, boolean setConfidence) {
super(label);
this.classifier = classifier;
this.useDistributionClassifier = setConfidence;
}
/** Returns true iff the parameter use_distribution was set and this classifier is a distribution classifier. */
public boolean isDistributionClassifier() {
return useDistributionClassifier;
}
public void apply(ExampleSet exampleSet) throws OperatorException {
LogService.logMessage("Converting to Weka instances.", LogService.MINIMUM);
Attribute predictedLabel = exampleSet.getPredictedLabel();
Instances instances = WekaTools.toWekaInstances(exampleSet,
"ApplierInstances",
predictedLabel,
false);
LogService.logMessage("Applying Weka classifier.", LogService.MINIMUM);
int i = 0;
ExampleReader r = exampleSet.getExampleReader();
while (r.hasNext()) {
Example e = r.next();
Instance instance = instances.instance(i++);
applyModelForInstance(instance, e, predictedLabel);
}
}
/** Classifies ervery weka instance and sets the result as predicted label of the current example.
*/
public void applyModelForInstance(Instance instance, Example e, Attribute predictedLabelAttribute) {
double predictedLabel = Double.NaN;
try {
if (useDistributionClassifier) {
double confidence[] = classifier.distributionForInstance(instance);
// TODO: to check
predictedLabel = 1 - confidence[0];
} else {
double wekaPrediction = classifier.classifyInstance(instance);
if (predictedLabelAttribute.isNominal()) {
String classification = instance.classAttribute().value((int)wekaPrediction);
predictedLabel = predictedLabelAttribute.mapString(classification);
} else {
predictedLabel = wekaPrediction;
}
}
} catch (Exception exc) {
LogService.logMessage("Exception occured while classifying example:"+exc.getMessage(),
LogService.ERROR);
}
e.setPredictedLabel(predictedLabel);
}
public String toString() {
return
"Weka model ("+classifier.getClass().getName()+") for label " + getLabel() + "\n" +
classifier.toString();
}
public String toResultString() {
return classifier.toString();
}
public Component getVisualisationComponent() {
if (classifier instanceof Drawable) {
try {
Drawable drawable = (Drawable)classifier;
return new TreeVisualizer(new TreeDisplayListener() {
public void userCommand(TreeDisplayEvent e) {
//System.out.println("TreeDisplayEvent: "+e);
}
},
drawable.graph(),
new PlaceNode2());
} catch (Exception e) {
e.printStackTrace();
return super.getVisualisationComponent();
}
} else {
return super.getVisualisationComponent();
}
}
public Attribute createPredictedLabel(ExampleSet exampleSet, String name) {
Attribute predictedLabel = super.createPredictedLabel(exampleSet, name);
if (isDistributionClassifier()) {
if (predictedLabel.isNominal())
predictedLabel.clearMaps();
predictedLabel.setValueType(Ontology.REAL);
}
return predictedLabel;
}
public boolean equals(Object o) {
if (!super.equals(o)) return false;
WekaClassifier other = (WekaClassifier)o;
if (other.useDistributionClassifier != this.useDistributionClassifier) return false;
if (!other.classifier.getClass().equals(this.classifier.getClass())) return false;
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -