📄 defaultlearner.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.lazy;
import edu.udo.cs.yale.operator.learner.AbstractLearner;
import edu.udo.cs.yale.operator.learner.Model;
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.example.SkipNANExampleReader;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.tools.LogService;
import java.util.List;
/** This learner creates a model, that will simply predict a default value for all examples,
* i.e. the average or median of the true labels or a fixed specified value.
* This learner can be used to compare the results of "real" learning schemes with
* guessing.
*
* @yale.xmlclass DefaultLearner
* @yale.index DefaultLearner
* @author Stefan R?ping
* @version $Id: DefaultLearner.java,v 1.3 2004/08/27 11:57:40 ingomierswa Exp $
* @see edu.udo.cs.yale.operator.learner.lazy.DefaultModel
* @see edu.udo.cs.yale.example.ExampleSet
* @yale.todo H?ufigste Klasse f?r Klassifikation zuf?gen, in Datei speichern, imports aufr?umen (kopiert von MySVMLearner
*/
public class DefaultLearner extends AbstractLearner {
private static final String[] METHODS = { "median","average","constant" };
public static final int MEDIAN = 0;
public static final int AVERAGE = 1;
public static final int CONSTANT = 2;
public Model learn(ExampleSet exampleSet) throws OperatorException {
double value = 0.0;
int method = getParameterAsInt("method");
int labelIndex = exampleSet.getLabel().getIndex();
switch (method) {
case MEDIAN:
double[] labels = new double[exampleSet.getSize()];
ExampleReader r = exampleSet.getExampleReader();
int numberOfExamples = 0;
while (r.hasNext()) {
// ---- read next example ----
Example example = (Example)r.next();
labels[numberOfExamples] = example.getLabel();
numberOfExamples++;
};
java.util.Arrays.sort(labels,0,numberOfExamples-1);
value = labels[numberOfExamples/2];
break;
case AVERAGE:
r = exampleSet.getExampleReader();
numberOfExamples = 0;
while (r.hasNext()) {
// ---- read next example ----
Example example = (Example)r.next();
numberOfExamples++;
value += example.getLabel();
};
value /= (double)numberOfExamples;
break;
default:
try {
value = getParameterAsDouble("value");
}
catch(Exception e){
LogService.logMessage("No constant set.",LogService.ERROR);
}
break;
}
LogService.logMessage("Default value is "+value,LogService.TASK);
return new DefaultModel(exampleSet.getLabel(), value);
}
public List getParameterTypes() {
List types = super.getParameterTypes();
ParameterType type = new ParameterTypeCategory("method", "The method to compute the default.", METHODS, 0);
type.setExpert(false);
types.add(type);
types.add(new ParameterTypeDouble("constant", "Value returned when method = \"constant\".", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,0.0));
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -