📄 c45learner.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.Yale;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.learner.AbstractLearner;
import edu.udo.cs.yale.operator.learner.Model;
import edu.udo.cs.yale.example.ExampleReader;
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.tools.LogService;
import edu.udo.cs.yale.tools.TempFileService;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.ParameterService;
import edu.udo.cs.yale.tools.Tools;
import java.util.List;
import java.util.Iterator;
import java.io.*;
/** <code>C45Learner</code> encapsulates the
* <a TARGET="_top" href="http://www.cse.unsw.edu.au/~quinlan/">C4.5</a> program version 8
* by Ross Quinlan {@yale.cite Quinlan/93b}.
* It generates all input files required by C4.5 containing attribute names,
* ranges, and data and starts an external process to invoke C4.5.
* The output of C4.5 is parsed into a {@link edu.udo.cs.yale.operator.learner.decisiontree.RuleSet}
* which is a subclass of {@link Model} and can be applied without further calling of
* external programs.
*
* @yale.xmlclass C45Learner
* @yale.reference Quinlan/93b
* @see edu.udo.cs.yale.operator.learner.decisiontree.RuleSet
* @author Ingo Mierswa, Dave Meppelink
* @version $Id: C45Learner.java,v 2.4 2004/08/27 11:57:38 ingomierswa Exp $
*/
public class C45Learner extends AbstractLearner {
static {
Yale.registerYaleProperty(new ParameterTypeFile("yale.c45.learncommand", "Path to the C4.5 learner executable", true));
Yale.registerYaleProperty(new ParameterTypeFile("yale.c45.rulecommand", "Path to the C4.5 rule parser executable", true));
}
/** Parameters without value.
*/
static final String[] SINGLE_PARAMETER = { "u", "s", "p", "g" };
/** Parameters which need a value.
*/
static final String[] PARAMETER = { "m", "c" };
/** Starts the external process and feeds parameters and data to stdin.
* @return A RuleSet
*/
public Model learn(ExampleSet exampleSet) throws OperatorException {
LogService.logMessage("C4.5 learner '"+getName()+"': "
+"starts learning.", LogService.TASK);
// ---- Eingabedateien des C4.5 erzeugen ----
File[] files = new File[2];
String[] extensions = { ".names", ".data" };
String filestem = TempFileService.createTempFiles(getName()+"_", extensions, files);
writeExamples(exampleSet, files);
// ---- Prozess erzeugen ----
String learnCommand = ParameterService.getProperty("yale.c45.learncommand");
String ruleCommand = ParameterService.getProperty("yale.c45.rulecommand");
String parameterString = parseParameters();
// c4.5 process
Process process = null;
try {
String command = learnCommand + parameterString + " -f " + filestem;
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
throw new UserError(this, e, 310, new Object[] { learnCommand, e });
}
try {
// ---- get Output ----
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = Tools.readOutput(in);
if ((output==null)||(output.equals("")))
LogService.logMessage("C4.5 learner '" + getName() + "': "
+ "No output of C4.5.", LogService.WARNING);
in.close();
LogService.logMessage("Output of C4.5 (Learner: '"+getName()+"')\n"+output, LogService.MINIMUM);
} catch (IOException e) {
throw new UserError(this, e, 308, learnCommand);
}
// c4.5 rules process
try {
process = Runtime.getRuntime().exec(ruleCommand + " " + "-f " + filestem);
} catch (IOException e) {
throw new UserError(this, e, 310, new Object[] { ruleCommand, e });
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
RuleSet rules = RuleSet.parseC45Rules(in, exampleSet);
in.close();
Tools.waitForProcess(this, process, "c45rules");
LogService.logMessage("C4.5 learner '"+getName()+"': C4.5 has succesfully "
+"learned an example set.", LogService.TASK);
return rules;
} catch (IOException e) {
throw new UserError(this, e, 308, ruleCommand);
}
}
/** Constructs a parameter string from the parameters set in the configuration file. */
private String parseParameters() {
StringBuffer params = new StringBuffer();
for (int i = 0; i < SINGLE_PARAMETER.length; i++) {
String value = getParameterAsString(SINGLE_PARAMETER[i]);
if (value != null) {
params.append(" -");
params.append(SINGLE_PARAMETER[i]);
}
}
for (int i = 0; i < PARAMETER.length; i++) {
String value = getParameterAsString(PARAMETER[i]);
if (value != null) {
params.append(" -");
params.append(PARAMETER[i]);
params.append(value);
}
}
return params.toString();
}
/** First writes names and ranges of the attributs in the format <br>
* class1,class2,...,classN. <br>
* att1: range1. <br>
* att2: range2. <br>
* ... <br>
* to the file "filestem.names" and then writes the examples in the format<br>
* att1,att2,...,
*/
private void writeExamples(ExampleSet exampleSet, File[] files) throws OperatorException {
// ---- Schreiben der Namen und Bereiche in filestem.names ----
try {
PrintWriter nameout = new PrintWriter(new FileWriter(files[0]));
// classes
Iterator classes = exampleSet.getLabel().getValues().iterator();
while (classes.hasNext()) {
String classString = (String)classes.next();
nameout.print(classString);
if (classes.hasNext()) nameout.print(",");
}
nameout.println(".");
// namen und bereiche
for (int n = 0; n < exampleSet.getNumberOfAttributes(); n++) {
Attribute attribute = exampleSet.getAttribute(n);
nameout.print(attribute.getName()+": ");
if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NUMERICAL)) {
nameout.print("continuous");
} else {
Iterator values = attribute.getValues().iterator();
while (values.hasNext()) {
String valueString = (String)values.next();
nameout.print(valueString);
if (values.hasNext()) nameout.print(",");
}
}
nameout.println(".");
}
nameout.close();
} catch (java.io.IOException e) {
throw new UserError(this, e, 303, new Object[] { files[0], e});
}
// ---- Schreiben der Daten in filestem.data ----
try {
PrintWriter dataout = new PrintWriter(new FileWriter(files[1]));
ExampleReader r = exampleSet.getExampleReader();
while (r.hasNext()) {
// ---- Beispiel auslesen ----
Example example = (Example)r.next();
// ---- Attribute bestimmen und rausschreiben ----
String attributes = example.getAttributesAsString(",");
// ---- Label bestimmen und rausschreiben ----
String label = example.getLabelAsString();
dataout.print(attributes);
dataout.println(","+label + ".");
}
dataout.close();
} catch (java.io.IOException e) {
throw new UserError(this, e, 303, new Object[] { files[1], e});
}
}
public List getParameterTypes() {
List types = super.getParameterTypes();
for (int i = 0; i < SINGLE_PARAMETER.length; i++)
types.add(new ParameterTypeString(SINGLE_PARAMETER[i], "The C4.5 parameter "+SINGLE_PARAMETER[i]+" (no value needed)."));
for (int i = 0; i < PARAMETER.length; i++)
types.add(new ParameterTypeString(PARAMETER[i], "The C4.5 parameter "+PARAMETER[i]+"."));
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -