testingEvaluation.setPriors(train);
trainTimeStart = System.currentTimeMillis();
if (objectInputFileName.length() == 0) {
classifier.buildClassifier(train);
}
Instance trainInst;
while (trainSource.hasMoreElements(train)) {
trainInst = trainSource.nextElement(train);
trainingEvaluation.updatePriors(trainInst);
testingEvaluation.updatePriors(trainInst);
((UpdateableClassifier)classifier).updateClassifier(trainInst);
}
trainTimeElapsed = System.currentTimeMillis() - trainTimeStart;
} else if (objectInputFileName.length() == 0) {
// Build classifier in one go
tempTrain = trainSource.getDataSet(actualClassIndex);
if (classifier instanceof weka.classifiers.misc.InputMappedClassifier &&
!trainingEvaluation.getHeader().equalHeaders(tempTrain)) {
// we need to make a new dataset that maps the training instances to
// the structure expected by the mapped classifier - this is only
// to ensure that the structure and priors computed by the *testing*
// evaluation object is correct with respect to the mapped classifier
Instances mappedClassifierDataset =
((weka.classifiers.misc.InputMappedClassifier)classifier).
getModelHeader(new Instances(template, 0));
for (int zz = 0; zz < tempTrain.numInstances(); zz++) {
Instance mapped = ((weka.classifiers.misc.InputMappedClassifier)classifier).
constructMappedInstance(tempTrain.instance(zz));
mappedClassifierDataset.add(mapped);
}
tempTrain = mappedClassifierDataset;
}
trainingEvaluation.setPriors(tempTrain);
testingEvaluation.setPriors(tempTrain);
trainTimeStart = System.currentTimeMillis();
classifier.buildClassifier(tempTrain);
trainTimeElapsed = System.currentTimeMillis() - trainTimeStart;
}
// backup of fully trained classifier for printing the classifications
if (classificationOutput != null) {
classifierClassifications = AbstractClassifier.makeCopy(classifier);
if (classifier instanceof weka.classifiers.misc.InputMappedClassifier) {
classificationOutput.setHeader(trainingEvaluation.getHeader());
}
}
// Save the classifier if an object output file is provided
if (objectOutputFileName.length() != 0) {
OutputStream os = new FileOutputStream(objectOutputFileName);
// binary
if (!(objectOutputFileName.endsWith(".xml") || (objectOutputFileName.endsWith(".koml") && KOML.isPresent()))) {
if (objectOutputFileName.endsWith(".gz")) {
os = new GZIPOutputStream(os);
}
ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
objectOutputStream.writeObject(classifier);
if (template != null) {
objectOutputStream.writeObject(template);
}
objectOutputStream.flush();
objectOutputStream.close();
}
// KOML/XML
else {
BufferedOutputStream xmlOutputStream = new BufferedOutputStream(os);
if (objectOutputFileName.endsWith(".xml")) {
XMLSerialization xmlSerial = new XMLClassifier();
xmlSerial.write(xmlOutputStream, classifier);
}
else
// whether KOML is present has already been checked
// if not present -> ".koml" is interpreted as binary - see above
if (objectOutputFileName.endsWith(".koml")) {
KOML.write(xmlOutputStream, classifier);
}
xmlOutputStream.close();
}
}
// If classifier is drawable output string describing graph
if ((classifier instanceof Drawable) && (printGraph)){
return ((Drawable)classifier).graph();
}
// Output the classifier as equivalent source
if ((classifier instanceof Sourcable) && (printSource)){
return wekaStaticWrapper((Sourcable) classifier, sourceClass);
}
// Output model
if (!(noOutput || printMargins)) {
if (classifier instanceof OptionHandler) {
if (schemeOptionsText != null) {
text.append("\nOptions: "+schemeOptionsText);
text.append("\n");
}
}
text.append("\n" + classifier.toString() + "\n");
}
if (!printMargins && (costMatrix != null)) {
text.append("\n=== Evaluation Cost Matrix ===\n\n");
text.append(costMatrix.toString());
}
// Output test instance predictions only
if (classificationOutput != null) {
DataSource source = testSource;
predsBuff = new StringBuffer();
classificationOutput.setBuffer(predsBuff);
// no test set -> use train set
if (source == null && noCrossValidation) {
source = trainSource;
predsBuff.append("\n=== Predictions on training data ===\n\n");
} else {
predsBuff.append("\n=== Predictions on test data ===\n\n");
}
if (source != null)
classificationOutput.print(classifierClassifications, source);
}
// Compute error estimate from training data
if ((trainStatistics) && (trainSetPresent)) {
if ((classifier instanceof UpdateableClassifier) &&
(testSetPresent) &&
(costMatrix == null)) {
// Classifier was trained incrementally, so we have to
// reset the source.
trainSource.reset();
// Incremental testing
train = trainSource.getStructure(actualClassIndex);
testTimeStart = System.currentTimeMillis();
Instance trainInst;
while (trainSource.hasMoreElements(train)) {
trainInst = trainSource.nextElement(train);
trainingEvaluation.evaluateModelOnce((Classifier)classifier, trainInst);
}
testTimeElapsed = System.currentTimeMillis() - testTimeStart;
} else {
testTimeStart = System.currentTimeMillis();
trainingEvaluation.evaluateModel(
classifier, trainSource.getDataSet(actualClassIndex));
testTimeElapsed = System.currentTimeMillis() - testTimeStart;
}
// Print the results of the training evaluation
if (printMargins) {
return trainingEvaluation.toCumulativeMarginDistributionString();
} else {
if (classificationOutput == null) {
text.append("\nTime taken to build model: "
+ Utils.doubleToString(trainTimeElapsed / 1000.0,2)
+ " seconds");
if (splitPercentage > 0)
text.append("\nTime taken to test model on training split: ");
else
text.append("\nTime taken to test model on training data: ");
text.append(Utils.doubleToString(testTimeElapsed / 1000.0,2) + " seconds");
if (splitPercentage > 0)
text.append(trainingEvaluation.toSummaryString("\n\n=== Error on training"
+ " split ===\n", printComplexityStatistics));
else
text.append(trainingEvaluation.toSummaryString("\n\n=== Error on training"
+ " data ===\n", printComplexityStatistics));
if (template.classAttribute().isNominal()) {
if (classStatistics) {
text.append("\n\n" + trainingEvaluation.toClassDetailsString());
}
if (!noCrossValidation)
text.append("\n\n" + trainingEvaluation.toMatrixString());
}
}
}
}
// Compute proper error estimates
if (testSource != null) {
// Testing is on the supplied test data
testSource.reset();
test = testSource.getStructure(test.classIndex());
Instance testInst;
while (testSource.hasMoreElements(test)) {
testInst = testSource.nextElement(test);
testingEvaluation.evaluateModelOnceAndRecordPrediction(
(Classifier)classifier, testInst);
}