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

📄 ruleset.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.decisiontree;

import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.operator.learner.SimpleModel;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.LogService;

import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;

/** Rule set is a ordered set of rules containing several rules and a default goal (classification).
 *  If an inconsistance occurs or the example isn't covered the default goal is set for an example.
 *  @author Ingo
 *  @version $Id: RuleSet.java,v 2.5 2004/08/27 11:57:38 ingomierswa Exp $
 */
public class RuleSet extends SimpleModel {

    /** The default goal which is used for classification.
     */
    private int defaultGoal;

    /** The set of rules.
     */
    private ArrayList rules;

    protected RuleSet() {
	super();
    }

    /** Creates a new rule set with the given default goal and rules.
     */
    public RuleSet(Attribute label, int defaultGoal, ArrayList rules) {
	super(label);
	this.defaultGoal = defaultGoal;
	this.rules       = rules;
    }

    /** Creates an empty rule set without default goal.
     */
    public RuleSet(Attribute label) {
	super(label);
	this.rules = new ArrayList();
    }

    /** Adds a rule at the end of the set.
     */
    void addRule(Rule rule) {
	rules.add(rule);
    }
    
    /** Iterates through the set and returns a suitable goal for the example. If no goal can be found
     *  the default goal is returned.
     */
    public int getGoal(Example example) { 
	ListIterator i = rules.listIterator();
	while (i.hasNext()) {
	    Rule rule = (Rule)i.next();
	    if (rule.isTrue(example)) {
		return rule.getGoal().getGoal(example);
	    }
	}
	return defaultGoal; 
    }

    public double predict(Example example) {
	return getGoal(example);
    }

    public String toString() {
	return toRuleSetString();
    }
    
    public String toRuleSetString() {
	String result = "RuleSet (default: " + getLabel().getAsString(defaultGoal) + ")\n";
	ListIterator i = rules.listIterator();
	while (i.hasNext()) {
	    Rule rule = (Rule)i.next();
	    result += rule + "\n";
	}
	return result;
    }

    public void writeModel(String filename) {
	throw new UnsupportedOperationException("writeModel(String) in RuleSet not supported!");
    }


    // ---------- C4.5 ----------

    /** Parses the output of von <tt>c4.5rules</tt> and creates a <tt>RuleSet</tt>.
     */
    public static RuleSet parseC45Rules(BufferedReader in, ExampleSet exampleSet) {
	try {
	    ArrayList rules = new ArrayList();
	    String line = "";
	    int defaultClass = -1;
	    // Regeln durchgehen
	    while ((line = in.readLine()) != null) {
		if ((line.indexOf("Rule") != -1) && (line.indexOf(":") != -1)) {

		    // Regel --> Constraints bestimmen
		    ArrayList premiseList = new ArrayList();
		    String premiseLine = in.readLine().trim();
		    int goal;
		    while ((premiseLine != null) && (!premiseLine.equals(""))) {
			if (premiseLine.indexOf("->") == -1) {
			    // normalen Premise bestimmen
			    premiseList.add(Premise.parseC45Premise(premiseLine, exampleSet));
			} else if (premiseLine.indexOf("->") != -1) {
			    // Ziel auslesen
			    String goalStr = premiseLine.substring(premiseLine.indexOf("class") + 6, 
								   premiseLine.indexOf("[") - 2);
			    goal = exampleSet.getLabel().mapString(goalStr);
			    Premise[] premises = new Premise[premiseList.size()];
			    premiseList.toArray(premises);
			    rules.add(new Rule(new Tree(exampleSet.getLabel(), goal), new AndPremise(premises)));
			    break;
			}
			// naechste Zeile einlesen
			premiseLine = in.readLine().trim();
		    }   
		} else if (line.indexOf("Default") != -1) {
		    // default class auslesen
		    defaultClass = exampleSet.getLabel().mapString(line.substring(line.indexOf(":") + 2));
		    break;
		}
	    }
	    return new RuleSet(exampleSet.getLabel(), defaultClass, rules);
	} catch (IOException e) { 
	    LogService.logException("Can not parse C4.5 rules!", e); 
	    return null;
	}
    }


}

⌨️ 快捷键说明

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