📄 model.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;
import edu.udo.cs.yale.operator.ResultObjectAdapter;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.Saveable;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Attribute;
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;
import java.lang.reflect.Constructor;
/** 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.19 2004/08/27 11:57:38 ingomierswa Exp $
*/
public abstract class Model extends ResultObjectAdapter implements Saveable {
/** We remember the target attribute in order to be able to create predicted label
* attributes based on this type. It is declared transient since we handle
* serialization of the attribute ourselves. */
private transient Attribute labelAttribute;
/** 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;
/** All serializable subclasses must have a zero-argument constructor which is invoked during
* deserialization. */
protected Model() { }
/** All subclasses must pass a label {@link Attribute} to their superclass constructors. */
public Model(Attribute label) {
this.labelAttribute = label;
}
/** 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()) + ") for label " + labelAttribute;
}
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());
if (labelAttribute != null) {
out.writeUTF("label");
labelAttribute.writeType(out);
} else {
out.writeUTF("nolabel");
}
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 IOException {
String className = in.readUTF();
String labelString = in.readUTF();
Attribute label = null;
if (labelString.equals("label")) {
label = Attribute.readType(in);
}
Model model = null;
try {
Class c = edu.udo.cs.yale.tools.Tools.classForName(className);
if (!(Model.class.isAssignableFrom(c))) {
throw new IOException("Model file does not contain a model (but "+c.getName()+")!");
}
if (SerializableModel.class.isAssignableFrom(c)) {
model = SerializableModel.readSerializableModel(in);
model.labelAttribute = label;
} else if (IOModel.class.isAssignableFrom(c)) {
Constructor constructor = c.getConstructor(new Class[] { Attribute.class });
model = (Model)constructor.newInstance(new Object[] { label });
((IOModel)model).readData(in);
} else {
throw new IOException("Don't know how to read a '"+className+"' model!");
}
} catch (ClassCastException e) {
throw new IOException("'"+className+"' is not a model!");
} catch (ClassNotFoundException e) {
throw new IOException("Don't know anything about class '"+className+"'!");
} catch (InstantiationException e) {
throw new IOException("Cannot instantiate '"+className+"': " + e);
} catch (IllegalAccessException e) {
throw new IOException("Cannot instantiate '"+className+"': " + e);
} catch (NoSuchMethodException e) {
throw new IOException("Cannot instantiate '"+className+"' (no one-argument constructor taking an Attribute): " + e);
} catch (java.lang.reflect.InvocationTargetException e) {
throw new IOException("Cannot instantiate '"+className+"': Exception in constructor: " + e);
}
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) {
throw new UnsupportedOperationException(getClass().getName() + " does not support prediction of confidence.");
}
/** Returns true iff the given object has the same class. */
public boolean equals(Object o) {
return (o != null) && (this.getClass().equals(o.getClass()));
}
/** Returns the label for which this model was learned, i.e. the learning target attribute. */
public Attribute getLabel() {
return labelAttribute;
}
/** Creates a predicted label from the label of the given example set.
* Invokes createPredictedLabel(exampleSet, null). */
public Attribute createPredictedLabel(ExampleSet exampleSet) {
return createPredictedLabel(exampleSet, null);
}
/** Creates a predicted label with the given name. If name is null, the name "prediction(labelname)"
* is used. */
public Attribute createPredictedLabel(ExampleSet exampleSet, String name) {
Attribute predictedLabel = new Attribute(getLabel(), ExampleSet.PREDICTION_NAME);
if (name != null) predictedLabel.setName(name);
exampleSet.getExampleTable().addAttribute(predictedLabel);
exampleSet.setPredictedLabel(predictedLabel);
return predictedLabel;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -