⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractlearner.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 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.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.operator.learner.Model;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.performance.PerformanceCriterion;
import edu.udo.cs.yale.operator.performance.PerformanceVector;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.AttributeWeights;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Ontology;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/** A <tt>Learner</tt> is an operator that encapsulates the learning step of a machine learning 
 *  method. Some Learners may be capable of estimating the performance of the generated model.
 *  In that case, they additionally return a 
 *  {@link edu.udo.cs.yale.operator.performance.PerformanceVector}. Some other learners can calculate a 
 *  weight vector for all used features.
 *
 *  @author Ingo
 *  @version $Id: AbstractLearner.java,v 2.6 2004/09/14 08:39:06 ingomierswa Exp $
 */
public abstract class AbstractLearner extends Operator implements Learner {

    public static final String[] TASK_TYPES = { "auto", "regression", "pattern" };
    public static final int  AUTO       = 0;
    public static final int  REGRESSION = 1;
    public static final int  PATTERN    = 2;

    private static final Class[] INPUT_CLASSES = { ExampleSet.class };

    /** Returns true iff the learner can generate a performance vector during training. */
    public boolean canEstimatePerformance() { return false; }
    
    public PerformanceVector getEstimatedPerformance() { return null; }

    /** Returns true iff the learner can generate a attribute weight vector. */
    public boolean canCalculateWeights() { return false; }

    /** Returns the calculated weight vectors. */
    public AttributeWeights getWeights(ExampleSet exampleSet) { return null; }


    /** Trains a model useing an ExampleSet from the input. The model is saved in <tt>model_file</tt>
     *  if given. Uses the method learn(ExampleSet).
     */
    public IOObject[] apply() throws OperatorException {
	String modelFile = getParameterAsString("model_file");
	ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class);

	Attribute  labelAttribute = exampleSet.getLabel();
	if (labelAttribute == null) {
	    throw new UserError(this, 105, new Object[0]); 
	}

	if (exampleSet.getNumberOfAttributes()==0) {
	    throw new UserError(this, 106, new Object[0]);
	}

	List results = new LinkedList();
	Model model = learn(exampleSet);
	results.add(model);
	try {
	    if (modelFile != null) model.writeModel(getExperiment().resolveFileName(modelFile));
	} catch (IOException e) {
	    throw new UserError(this, e, 303, new Object[] {modelFile, e.getMessage() });
	}
	
	// weights must be calculated _after_ learning  
	if (canCalculateWeights()) {
	    AttributeWeights weights = getWeights(exampleSet);
	    if (weights != null)
		results.add(weights);
	}

	if (canEstimatePerformance()) {
	    PerformanceVector perfVector = getEstimatedPerformance();
	    if (perfVector != null)
		results.add(perfVector);
	}

	IOObject[] resultArray = new IOObject[results.size()];
	results.toArray(resultArray);
	return resultArray;
    }

    
    public Class[] getInputClasses() { return INPUT_CLASSES; }

    public Class[] getOutputClasses() { 
	List classList = new LinkedList();
	classList.add(Model.class);
	if (canEstimatePerformance()) classList.add(PerformanceVector.class);
	if (canCalculateWeights()) classList.add(AttributeWeights.class);
	Class[] result = new Class[classList.size()];
	classList.toArray(result);
	return result;
    }

    /** Returns <tt>true</tt>, if the learning task at hand is a classification task, and <tt>false</tt>
     *  otherwise.  A learning task is a classification task (and no regression task), if
     *  <code>userOptionTaskType</code> is not <code>AUTO</code>, it determines the task type.
     *  If it does not match the type of the label, a warning is issues. Otherwise the task type
     *  is classification iff the label attribute is nominal. */
    protected boolean taskIsClassification(int userOptionTaskType, 
					   ExampleSet exampleSet) throws OperatorException {
	Attribute  labelAttribute = exampleSet.getLabel();
	boolean  labelIsNominal = Ontology.ATTRIBUTE_VALUE_TYPE.isA(labelAttribute.getValueType(), Ontology.NOMINAL);
	
	switch (userOptionTaskType) {
	case REGRESSION:
	    if (labelIsNominal)
		LogService.logMessage(getName() + ": Applying regression to nominal data.", LogService.WARNING);
	    return false;
		
	case PATTERN:
	    if (!labelIsNominal) {
		LogService.logMessage(getName() + ": Applying classification to numerical data.", LogService.WARNING);
	    }
	    if (labelAttribute.getValues().size() != 2) {
		LogService.logMessage(getName() + ": Class attribute should have exactly two values, but has " + 
				      labelAttribute.getValues().size() +
				      " class values. (Ignore warning in MultiClassLearner!)", LogService.WARNING);
	    }
	    return true;
	case AUTO:
	default:
	    if (labelIsNominal) { 
		if (labelAttribute.getValues().size() != 2) {
		    LogService.logMessage(getName() + ": Class attribute should have exactly two values, but has " + 
					  labelAttribute.getValues().size() +
					  " class values. (Ignore warning in MultiClassLearner!)", LogService.WARNING);
		}
		return true;
	    } else {
		return false;
	    }
	}
    }

    public List getParameterTypes() {
	List types = super.getParameterTypes();
	ParameterType type = new ParameterTypeFile("model_file", "If this parameter is set, the model is written to a file.", true);
	type.setExpert(false);
	types.add(type);
	return types;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -