📄 wekaclassifier.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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;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.tools.LogService;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 weka.classifiers.DistributionClassifier;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 2.5 2003/09/03 14:27:27 fischer Exp $ */public class WekaClassifier extends WekaModel { /** The used weka classifier. */ private Classifier classifier; /** Set to true iff {@link weka.classifiers.DistributionClassifier} implements the * Weka DistributionClassifier and the predicted label should be the confidence * (and not the class index). */ private boolean useDistributionClassifier = false; public WekaClassifier(Classifier classifier) { this(classifier, false); } public WekaClassifier(Classifier classifier, boolean setConfidence) { this.classifier = classifier; this.useDistributionClassifier = setConfidence; if (useDistributionClassifier && !(classifier instanceof DistributionClassifier)) throw new IllegalArgumentException("setConfidence must only be true if classifier is instance of DistributionClassifier!"); } public void apply(ExampleSet exampleSet) throws OperatorException { if (useDistributionClassifier) { exampleSet.getPredictedLabel().setMinimum(0); exampleSet.getPredictedLabel().setMaximum(1); } super.apply(exampleSet); } /** Classifies ervery weka instance and sets the result as predicted label of the current example. */ public void applyModelForInstance(Instance instance, Example e) { double predictedLabel = Double.NaN; try { if (useDistributionClassifier) { double confidence[] = ((DistributionClassifier)classifier).distributionForInstance(instance); predictedLabel = confidence[0]; } else { predictedLabel = classifier.classifyInstance(instance)+Attribute.FIRST_CLASS_INDEX; } } 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()+")"; } 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 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 + -