📄 model.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.ResultObjectAdapter;import edu.udo.cs.yale.operator.OperatorException;import edu.udo.cs.yale.operator.FatalException;import edu.udo.cs.yale.operator.Saveable;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.MethodNotSupportedException;import edu.udo.cs.yale.tools.LogService;import edu.udo.cs.yale.tools.Tools;import edu.udo.cs.yale.gui.SwingTools;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInput;import java.io.ObjectInputStream;import java.io.ObjectOutput;import java.io.ObjectOutputStream;import java.io.IOException;import java.net.MalformedURLException;import java.awt.Component;import javax.swing.JLabel;/** Model is the superclass for all objects generated by learners. Learners can apply models to example sets * and thus predict labels.<br> * * @author Ingo * @version $Id: Model.java,v 2.5 2003/05/22 11:51:18 fischer Exp $ */public abstract class Model extends ResultObjectAdapter implements Saveable { /** The predicted labels are the classifications. */ public static final int PREDICT_CLASSIFICATION = 0; /** The predicted labels are the confidence values. */ public static final int PREDICT_CONFIDENCE = 1; private int predictionType = PREDICT_CLASSIFICATION; public Model() { } /** Applies the model by setting the predicted labels. */ public abstract void apply(ExampleSet testSet) throws OperatorException; /** Writes the model to a stream. */ public abstract void writeData(ObjectOutputStream out) throws IOException; public String toString() { return "Model (type " + Tools.classNameWOPackage(this.getClass()) + ")"; } public void save(File file) throws IOException { writeModel(file); } /** Writes the model to a file by writing its class and calling writeModel(ObjectOutput). */ public final void writeModel(File file) throws IOException { LogService.logMessage("Writing model to file '"+file+"'.", LogService.MINIMUM); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); writeModel(out); out.close(); } /** Writes the model to a file by writing its class and calling writeModel(ObjectOutput). */ public final void writeModel(ObjectOutputStream out) throws IOException { out.writeUTF(getClass().getName()); writeData(out); } /** Reads the model from a file. */ public static final Model readModel(File file) throws OperatorException, IOException { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); Model model = readModel(in); in.close(); return model; } /** Reads the model from an ObjectInput. */ public static final Model readModel(ObjectInputStream in) throws OperatorException, IOException { String className = in.readUTF(); Model model = null; try { Class c = Class.forName(className); if (!(Model.class.isAssignableFrom(c))) { throw new FatalException("Model file does not contain a model!"); } if (SerializableModel.class.isAssignableFrom(c)) { model = SerializableModel.readSerializableModel(in); } else if (IOModel.class.isAssignableFrom(c)) { model = (Model)c.newInstance(); ((IOModel)model).readData(in); } else { throw new FatalException("Don't know how to read a '"+className+"' model!"); } } catch (ClassCastException e) { throw new FatalException("'"+className+"' is not a model!"); } catch (ClassNotFoundException e) { throw new FatalException("Don't know anything about class '"+className+"'!"); } catch (InstantiationException e) { throw new FatalException("Cannot instantiate '"+className+"'!"); } catch (IllegalAccessException e) { throw new FatalException("Cannot instantiate '"+className+"'!"); } return model; } /** Sets the type of the prediction. For binary classification tasks, the predicted label can * either be the classification or the confidence (i.e. a real number). * Currently, only the SVMLight makes use of this feature. * @param type one out of PREDICT_CLASSIFICATIONS and PREDICT_CONFIDENCE */ public void setPredictionType(int type) { this.predictionType = type; } /** @return One out of PREDICT_CLASSIFICATIONS and PREDICT_CONFIDENCE */ public int getPredictionType() { return predictionType; } public boolean equals(Object o) { return (o != null) && (this.getClass().equals(o.getClass())); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -