📄 c45saver.java
字号:
setRetrieval(BATCH);
if(retrieveFile() == null || getWriter() == null){
throw new IOException("C4.5 format requires two files. Therefore no output to standard out can be generated.\nPlease specifiy output files using the -o option.");
}
setWriteMode(WRITE);
//print names file
setFileExtension(".names");
PrintWriter outW = new PrintWriter(getWriter());
for (int i = 0; i < instances.attribute(instances.classIndex()).numValues(); i++) {
outW.write(instances.attribute(instances.classIndex()).value(i));
if (i < instances.attribute(instances.classIndex()).numValues()-1) {
outW.write(",");
} else {
outW.write(".\n");
}
}
for (int i = 0; i < instances.numAttributes(); i++) {
if (i != instances.classIndex()) {
outW.write(instances.attribute(i).name()+": ");
if (instances.attribute(i).isNumeric() || instances.attribute(i).isDate()) {
outW.write("continuous.\n");
} else {
Attribute temp = instances.attribute(i);
for (int j = 0; j < temp.numValues(); j++) {
outW.write(temp.value(j));
if (j < temp.numValues()-1) {
outW.write(",");
} else {
outW.write(".\n");
}
}
}
}
}
outW.flush();
outW.close();
//print data file
String out = retrieveFile().getAbsolutePath();
setFileExtension(".data");
out = out.substring(0, out.lastIndexOf('.')) + getFileExtension();
File namesFile = new File(out);
try{
setFile(namesFile);
setDestination(retrieveFile());
} catch(Exception ex){
throw new IOException("Cannot create data file, only names file created.");
}
if(retrieveFile() == null || getWriter() == null){
throw new IOException("Cannot create data file, only names file created.");
}
outW = new PrintWriter(getWriter());
// print data file
for (int i = 0; i < instances.numInstances(); i++) {
Instance temp = instances.instance(i);
for(int j = 0; j < temp.numAttributes(); j++){
if(j != instances.classIndex()){
if (temp.isMissing(j)) {
outW.write("?,");
} else if (instances.attribute(j).isNominal() ||
instances.attribute(j).isString()) {
outW.write(instances.attribute(j).value((int)temp.value(j))+",");
} else {
outW.write(""+temp.value(j)+",");
}
}
}
// write the class value
if (temp.isMissing(instances.classIndex())) {
outW.write("?");
}
else {
outW.write(instances.attribute(instances.classIndex()).value((int)temp.value(instances.classIndex())));
}
outW.write("\n");
}
outW.flush();
outW.close();
setFileExtension(".names");
setWriteMode(WAIT);
}
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
FastVector newVector = new FastVector(3);
newVector.addElement(new Option("The input file", "i", 1,
"-i <the input file>"));
newVector.addElement(new Option("The output file", "o", 1,
"-o <the output file>"));
newVector.addElement(new Option("The class index", "c", 1,
"-c <the class index>"));
return newVector.elements();
}
/**
* Parses a given list of options. Valid option is:<p>
*
* -i input arff file <br>
* The input filw in ARFF format. <p>
*
* -o the output file <br>
* The output file. The prefix of the output file is sufficient.<p>
*
* -c class index <br>
* The index of the class attribute. first and last are valid as well (default: last). <p>
*
* @param options the list of options as an array of strings
* @exception Exception if an option is not supported
*/
public void setOptions(String[] options) throws Exception {
String outputString = Utils.getOption('o', options);
String inputString = Utils.getOption('i', options);
String indexString = Utils.getOption('c', options);
ArffLoader loader = new ArffLoader();
resetOptions();
if(inputString.length() != 0){
try{
File input = new File(inputString);
loader.setFile(input);
setInstances(loader.getDataSet());
} catch(Exception ex){
throw new IOException("No data set loaded. Data set has to be arff format.");
}
}
else
throw new IOException("No data set to save.");
if (outputString.length() != 0){
//add appropriate file extension
if(!outputString.endsWith(getFileExtension())){
if(outputString.lastIndexOf('.') != -1)
outputString = (outputString.substring(0,outputString.lastIndexOf('.'))) + getFileExtension();
else
outputString = outputString + getFileExtension();
}
try{
File output = new File(outputString);
setFile(output);
} catch(Exception ex){
throw new IOException("Cannot create output file.");
} finally{
setDestination(retrieveFile());
}
}
if(indexString.length() != 0){
if(indexString.equals("first"))
getInstances().setClassIndex(0);
else{
if(indexString.equals("last"))
getInstances().setClassIndex(getInstances().numAttributes()-1);
else{
int classIndex = Integer.parseInt(indexString);
if(classIndex >=0 && classIndex < getInstances().numAttributes())
getInstances().setClassIndex(classIndex);
else
throw new IOException("Invalid class index");
}
}
}
else
getInstances().setClassIndex(getInstances().numAttributes()-1);
}
/**
* Gets the current settings of the C45Saver object.
*
* @return an array of strings suitable for passing to setOptions
*/
public String [] getOptions() {
String [] options = new String [10];
int current = 0;
if(retrieveFile() != null){
options[current++] = "-o"; options[current++] = "" + retrieveFile();
}
else{
options[current++] = "-o"; options[current++] = "";
}
if(getInstances() != null){
options[current++] = "-i"; options[current++] = "" + getInstances().relationName();
options[current++] = "-c"; options[current++] = "" + getInstances().classIndex();
}
else{
options[current++] = "-i"; options[current++] = "";
options[current++] = "-c"; options[current++] = "";
}
while (current < options.length) {
options[current++] = "";
}
return options;
}
/**
* Main method.
*
* @param options should contain the options of a Saver.
*/
public static void main(String [] options) {
StringBuffer text = new StringBuffer();
try {
C45Saver csv = new C45Saver();
text.append("\n\nC45Saver options:\n\n");
Enumeration enumi = csv.listOptions();
while (enumi.hasMoreElements()) {
Option option = (Option)enumi.nextElement();
text.append(option.synopsis()+'\n');
text.append(option.description()+'\n');
}
try {
csv.setOptions(options);
} catch (Exception ex) {
System.out.println("\n"+text);
System.exit(1);
}
//incremental
/*
csv.setRetrieval(INCREMENTAL);
Instances instances = csv.getInstances();
csv.setStructure(instances);
for(int i = 0; i < instances.numInstances(); i++){ //last instance is null and finishes incremental saving
csv.writeIncremental(instances.instance(i));
}
csv.writeIncremental(null);
*/
//batch
csv.writeBatch();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -