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

📄 libsvmlearner.java

📁 著名的开源仿真软件yale
💻 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.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.operator.OperatorException;import edu.udo.cs.yale.operator.UserError;import edu.udo.cs.yale.operator.parameter.*;import edu.udo.cs.yale.tools.LogService;import libsvm.svm;import libsvm.svm_problem;import libsvm.svm_parameter;import libsvm.svm_node;import libsvm.svm_model;import java.util.List;import java.util.LinkedList;/** Applies the <a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm">libsvm</a> learner *  by Chih-Chung Chang and Chih-Jen Lin. The SVM is a powerful method for both *  classification and regression. * *  @yale.index SVM *  @version $Id: LibSVMLearner.java,v 2.6 2003/08/14 10:24:57 fischer Exp $ */public class LibSVMLearner extends Learner {    public static final String[] SVM_TYPES    = { "C-SVC", "nu-SVC", "one-class", "epsilon-SVR", "nu-SVR" };    public static final String[] KERNEL_TYPES = { "linear", "poly", "rbf", "sigmoid" };    public static svm_node[] makeNodes(Example e) {	List nodes = new LinkedList();	for (int a = 0; a < e.getNumberOfAttributes(); a++) {	    double value = e.getValue(a);	    Attribute attr = e.getAttribute(a);	    if (!attr.isDefault(value)) {		svm_node node = new svm_node();		node.index = a;		node.value = value;		nodes.add(node);	    }	}	svm_node[] nodeArray = new svm_node[nodes.size()];	nodes.toArray(nodeArray);	return nodeArray;    }    public static svm_problem getProblem(ExampleSet exampleSet) {	LogService.logMessage("Creating LibSVM problem.", LogService.MINIMUM);	int nodeCount = 0;	svm_problem problem = new svm_problem();	problem.l = exampleSet.getSize();	problem.y = new double[exampleSet.getSize()];	problem.x = new svm_node[exampleSet.getSize()][];	ExampleReader i = exampleSet.getExampleReader();	int j = 0;	while (i.hasNext()) {	    Example e = i.next();	    problem.x[j] = makeNodes(e);	    problem.y[j] = e.getLabel();	    nodeCount += problem.x[j].length;	    j++;	}	LogService.logMessage("Created "+nodeCount+" nodes for "+j+" examples.", LogService.MINIMUM);	return problem;    }    private svm_parameter getParameters(ExampleSet exampleSet) {	svm_parameter params = new svm_parameter();	params.svm_type    = getParameterAsInt("svm_type");	params.kernel_type = getParameterAsInt("kernel_type");	params.degree      = getParameterAsDouble("degree");	params.gamma       = getParameterAsDouble("gamma");	if (params.gamma == 0)	    params.gamma = 1.0/exampleSet.getSize();	params.coef0       = getParameterAsDouble("coef0");	params.nu          = getParameterAsDouble("nu");	params.cache_size  = getParameterAsInt("cache_size");	params.C           = getParameterAsDouble("C");	params.eps         = getParameterAsDouble("epsilon");	params.p           = getParameterAsDouble("p");	params.shrinking   = getParameterAsBoolean("shrinking") ? 1 : 0;	return params;    }    public Model learn(ExampleSet exampleSet) throws OperatorException {	svm_parameter params = getParameters(exampleSet);	svm_problem problem  = getProblem(exampleSet);	String errorMsg = svm.svm_check_parameter(problem, params);	if (errorMsg != null)	    throw new UserError(this, 905, new Object[] {"libsvm", errorMsg});	LogService.logMessage("Training LibSVM.", LogService.MINIMUM);	return new LibSVMModel(svm.svm_train(problem, params));    }    public List getParameterTypes() {	List types = super.getParameterTypes();	types.add(new ParameterTypeCategory("svm_type", "SVM for classification (C-SVC, nu-SVC), regression (epsilon-SVR, nu-SVR) and distribution estimation (one-class)", SVM_TYPES, 0));	types.add(new ParameterTypeCategory("kernel_type", "The type of the kernel functions", KERNEL_TYPES, 0));	types.add(new ParameterTypeDouble("degree", "The degree for a polynomial kernel function.", 1, Double.POSITIVE_INFINITY, 3));	types.add(new ParameterTypeDouble("gamma", "The parameter gamma for polynomial, rbf, and sigmoid kernel functions. 0 means 1/#attributes.", 0, 1, 0));	types.add(new ParameterTypeDouble("coef0", "The parameter coef0 for polynomial and sigmoid kernel functions.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0));	types.add(new ParameterTypeDouble("nu", "The parameter nu for nu_svc, one_class, and nu_svr.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.5));	types.add(new ParameterTypeInt("cache_size", "Cache size in Megabyte.", 0, Integer.MAX_VALUE, 40));	types.add(new ParameterTypeDouble("C", "The cost parameter C for c_svc, epsilon_svr, and nu_svr.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100));	types.add(new ParameterTypeDouble("epsilon", "Tolerance of termination criterion.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.001));	types.add(new ParameterTypeDouble("p", "Tolerance of loss function of epsilon-SVR.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 0.1));	types.add(new ParameterTypeBoolean("shrinking", "Whether to use the shrinking heuristics.", true));	return types;    }}

⌨️ 快捷键说明

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