📄 perfestimator.java
字号:
Destructor. Only needs to get rid of performance data.
***************************************************************************/
protected void finalizer()
{
perfData = null;
}
abstract public String description();
abstract public double estimate_performance(BaseInducer inducer, InstanceList dataList);
// estimate performance from a series of files
abstract public double estimate_performance(BaseInducer inducer, String fileStem);
// Estimate performance from a fileStem with assumed suffixes
// -#.{names,data,test} where # denotes a sequence number.
/***************************************************************************
Trains and tests the inducer from a series of data/test files. Uses the
files fileStem-#.{names,data} Does NOT shuffle the files that it reads. It
deletes the perf data by default (not deleting it may be useful for
repeating this process more than once). The result returned is always
untrimmed. Restores the CrossValidator's original training list upon
completion.
@return
@param
@param
@param
@param
***************************************************************************/
public double estimate_file_performance(BaseInducer inducer,
int numFiles,
String fStem,
PerfData localPerfData)
{
logOptions.LOG(1, "PerfEstimator::estimate_performance: Inducer: "
+inducer.description()
+"\n");
logOptions.LOG(2, "PerfEstimator::estimate_performance: Number of files: "
+numFiles+"\n");
if (dumpStem != Globals.EMPTY_STRING)
Error.fatalErr("PerfEstimator::estimate_file_performance: it does not "
+"make sense to estimate from a file and dump too");
PerfData totalPerfData = new PerfData();
for (int fileNum = 0; fileNum < numFiles; fileNum++) {
String dataFile = new String(fStem + "-" + fileNum);
double error = single_file_performance(inducer, dataFile,
localPerfData);
totalPerfData.insert_error(error);
}
MLJ.ASSERT(totalPerfData.size() == numFiles,
"PerfEstimator.estimate_file_performance: "
+"totalPerfData.size() != numFiles");
logOptions.LOG(2, "Error of set: " +totalPerfData +"\n");
return totalPerfData.get_error_data().mean();
}
// Estimate one file from a fileStem
/***************************************************************************
Trains and tests the inducer from a single dumped file. Restores the
estimator's original training list upon completion.
@return
@param
@param
@param
***************************************************************************/
public double single_file_performance(BaseInducer inducer,
String dataFile,
PerfData localPerfData)
{
if (dumpStem != Globals.EMPTY_STRING)
Error.fatalErr("PerfEstimator::single_file_performance: it does not "
+"make sense to estimate from a file and dump too");
logOptions.LOG(3, "Reading data file: "+dataFile+"\n");
// Not local because train_and_test gets ownership and returns it.
InstanceList trainList = new InstanceList(dataFile);
logOptions.LOG(3, "Number of instances in file "+dataFile+": "
+trainList.num_instances()+". Total weight "+
trainList.total_weight()+"\n");
String testFile = new String(dataFile + Globals.DEFAULT_TEST_EXT);
logOptions.LOG(3, "Reading test file: "+testFile+"\n");
InstanceList testList = new InstanceList(trainList, testFile);
logOptions.LOG(3, "Instances in test list: "+testList.num_instances()+
". Total weight "+testList.total_weight()+"\n");
boolean saveDribble = GlobalOptions.dribble;
GlobalOptions.dribble = false;
double error = train_and_test(inducer, trainList, testList, "dummy",
localPerfData);
GlobalOptions.dribble = saveDribble;
logOptions.LOG(3, "Error of file: "+error+"\n");
trainList = null;
return error;
}
/***************************************************************************
Verify that perf data is available. Aborts with an error message if not.
***************************************************************************/
public void check_error_data()
{
if (perfData == null)
Error.fatalErr("PerfEstimator::check_error_data: Must be called "
+"after estimate_performance. No error data");
}
public void check_perf_data()
{
if (perfData == null)
Error.fatalErr("PerfEstimator::check_perf_data: Must be called "
+"after estimate_performance. No performance data");
}
/***************************************************************************
Return performance data or aborts if it is NULL. get_error_data is a
convenience function which gets the error portion of the performance data.
Mostly useful for the testers.
@return
***************************************************************************/
public ErrorData get_error_data()
{
check_perf_data();
return perfData.get_error_data();
}
public PerfData get_perf_data()
{
check_perf_data();
return perfData;
}
public void display_error_data()
{display_error_data(Globals.Mcout,0,ErrorData.defaultPrecision);}
public void display_error_data(Writer out)
{display_error_data(out,0,ErrorData.defaultPrecision);}
public void display_error_data(Writer out, double trim)
{display_error_data(out,trim,ErrorData.defaultPrecision);}
/***************************************************************************
Nicely display the error data, or print "no error data" if none.
***************************************************************************/
public void display_error_data(Writer out, double trim, int precision)
{
try{
if (perfData == null)
out.write("no error data");
else
perfData.display_error(out, trim, precision);
}catch(IOException e){e.printStackTrace();System.exit(1);}
}
public void display_perf_data()
{display_perf_data(Globals.Mcout,0,ErrorData.defaultPrecision);}
public void display_perf_data(Writer out)
{display_perf_data(out,0,ErrorData.defaultPrecision);}
public void display_perf_data(Writer out, double trim)
{display_perf_data(Globals.Mcout,trim,ErrorData.defaultPrecision);}
public void display_perf_data(Writer out, double trim, int precision)
{
try{
if (perfData == null|| perfData.perf_empty())
out.write("no performance data");
else
perfData.display(out, false, trim, precision);
}catch(IOException e){e.printStackTrace();System.exit(1);}
}
/***************************************************************************
Description : Returns the trimmed mean/standard deviation for the
error of the induced Categorizers for the
training and test lists generated through cross-validation.
The standard deviation is the std-dev of the MEAN error.
Comments : trim defaults to 0.
***************************************************************************/
public double error(double trim)
{
check_error_data();
return perfData.get_error_data().mean(trim);
}
public double error_std_dev()
{return error_std_dev(0);}
public double error_std_dev(double trim)
{
check_error_data();
return perfData.get_error_data().std_dev_of_mean(trim);
}
public double error()
{return error(0);}
/***************************************************************************
File dump: writes out all training lists used in estimation.
***************************************************************************/
public void dump_files(InstanceList dataList, String fStem)
{
if (fStem == Globals.EMPTY_STRING)
Error.fatalErr("PerfEstimator::dump_files: empty filestem");
dumpStem = fStem;
NullInducer inducer = new NullInducer(fStem + " Null Inducer", false);
logOptions.LOG(1, "Dumping files; estimated performances will be empty\n");
estimate_performance(inducer, dataList);
dumpStem = Globals.EMPTY_STRING; // Don't dump in the future.
}
/***************************************************************************
Display information about this estimator. The description is printed
only if descrip is TRUE. Performance data info is printed
***************************************************************************/
public void display(Writer out, boolean descrip,
double trim, int precision)
{
try{
if (descrip)
out.write(description()+": ");
display_error_data(out, trim, precision);
out.write("\n");
display_perf_data(out, trim, precision);
}catch(IOException e){e.printStackTrace(); System.exit(1);}
}
public void display()
{display(Globals.Mcout,false,0.0,ErrorData.defaultPrecision);}
public void display(Writer out)
{display(out,false,0.0,ErrorData.defaultPrecision);}
public void display(Writer out, boolean descrip)
{display(out,descrip,0.0,ErrorData.defaultPrecision);}
public void display(Writer out, boolean descrip, double trim)
{display(out,descrip,trim,ErrorData.defaultPrecision);}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -