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

📄 classificationbyregression.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.meta;

import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.operator.IllegalInputException;
import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.OperatorChain;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.IOContainer;
import edu.udo.cs.yale.operator.IODescription;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.learner.Learner;
import edu.udo.cs.yale.operator.learner.Model;
import edu.udo.cs.yale.operator.performance.PerformanceVector;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.Example; 
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.tools.TempFileService;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.math.RunVector;

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

/** For a classified dataset (with possibly more than two classes) builds a classifier
 *  using a regression method which is specified by the inner operator. For each 
 *  class {@yale.math i} a regression model is trained after setting the label to {@yale.math +1} if
 *  the label equals {@yale.math i} and to {@yale.math -1} if it is not. Then the regression models
 *  are combined into a classification model. In order to determine
 *  the prediction for an unlabeled example, all models are applied
 *  and the class belonging to the regression model which predicts the
 *  greatest value is chosen.
 *  <br />
 *  If the parameter <code>estimate_performance</code> is used, this operator will build
 *  the average of the (estimated) {@link PerformanceVector}s returned by its inner 
 *  operator and return it as an additional output. Please note, that this estimation is 
 *  not a reliable estimation for the true performance, and should only be used to guide 
 *  heuristics or to get hints which of two multi-class learners is better. Especially they 
 *  cannot be used to compare them with estimated performances of other learners. Example: Assume a 
 *  dataset with five uniformly distributed classes and a default learner which always returns the
 *  majority class. This learner will achieve an accuracy of roughly 4/5, which also yields an 
 *  average of 4/5. Despite this, the combined multiclass model will only have an accuracy of 1/5.
 *
 *  @see edu.udo.cs.yale.operator.learner.meta.MultiClassLearner
 *  @version $Id: ClassificationByRegression.java,v 1.3 2004/08/27 11:57:41 ingomierswa Exp $
 */
public class ClassificationByRegression extends OperatorChain {

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

    private int numberOfClasses;

    public IOObject[] apply() throws OperatorException {
	ExampleSet inputSet = (ExampleSet)getInput(ExampleSet.class);
	Attribute classLabel = inputSet.getLabel();
	if (classLabel == null) {
	    throw new UserError(this, 105, new Object[0]); 
	}
	numberOfClasses = classLabel.getValues().size();
	Model[] models = new Model[numberOfClasses];
	String filename = getParameterAsString("model_file");

	Operator learner = getOperator(0);

	ExampleSet eSet = (ExampleSet)inputSet.clone();
	Attribute tempLabel =  new Attribute("temp_regression_label");
	tempLabel.setValueType(Ontology.REAL);
	eSet.getExampleTable().addAttribute(tempLabel);
	eSet.setLabel(tempLabel);

	RunVector runVector = null;
	if (getParameterAsBoolean("estimate_performance")) {
	    runVector = new RunVector();
	}

	for (int i = 0; i < numberOfClasses; i++) {
	    // 1. Set regression labels
	    ExampleReader r = eSet.getExampleReader();
	    while (r.hasNext()) {
		Example e = r.next();
		if (e.getValue(classLabel) == i + Attribute.FIRST_CLASS_INDEX) {
		    e.setValue(tempLabel, +1.0);
		} else {
		    e.setValue(tempLabel, -1.0);
		}
	    }
	    // 2. Apply learner
	    IOContainer learnResult = learner.apply(getInput().append(new IOObject[] { eSet }));
	    models[i] = (Model)learnResult.getInput(Model.class);
	    if (runVector != null) {
		runVector.addVector((PerformanceVector)learnResult.getInput(PerformanceVector.class));
	    }
	}

	Model multiModel = new MultiModelByRegression(classLabel, models);

	try {
	    if (filename != null) multiModel.writeModel(getExperiment().resolveFileName(filename));
	} catch (IOException e) {
	    LogService.logMessage("MultiClassLearner'" + getName() + "': " +
				  "Cannot write MultiModel into file '"+filename+"'!", LogService.ERROR);
	}
	if (runVector != null) {
	    return new IOObject[] { multiModel, runVector.average() };
	} else {
	    return new IOObject[] { multiModel };
	}
    }

    public Class[] getInputClasses() { return INPUT_CLASSES; }
    public Class[] getOutputClasses() { 
	if (getParameterAsBoolean("estimate_performance")) {
	    return new Class[] { Model.class, PerformanceVector.class };
	} else {
	    return new Class[] { Model.class };
	}
    }

    /** Ok if the only inner operator returns a learner. */
    public Class[] checkIO(Class[] input) throws IllegalInputException {
	Operator operator = getOperator(0);
	if (!IODescription.containsClass(ExampleSet.class, input))
	    throw new IllegalInputException(this, operator, ExampleSet.class);

	Class[] innerOutput = operator.checkIO(new Class[] { ExampleSet.class });
	if (!IODescription.containsClass(Model.class, innerOutput))
	    throw new IllegalInputException(this, operator, Model.class);
	if (getParameterAsBoolean("estimate_performance")) {
	    if (!IODescription.containsClass(PerformanceVector.class, innerOutput))
		throw new IllegalInputException(this, operator, PerformanceVector.class);
	    return new Class[] { Model.class, PerformanceVector.class };
	} else {
	    return new Class[] { Model.class };
	}
    }

    /** Returns the maximum number of innner operators. */
    public int getMaxNumberOfInnerOperators() { return 1; }
    /** Returns the minimum number of innner operators. */
    public int getMinNumberOfInnerOperators() { return 1; }

    public int getNumberOfSteps() {
	return getNumberOfChildrensSteps() * numberOfClasses + 1;
    }


    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);
	types.add(new ParameterTypeBoolean("estimate_performance", "If this parameter is set to true, the average of the performance estimates of the inner operator model is returned as an additional output.", false));

	return types;
    }
}

⌨️ 快捷键说明

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