[86] | 1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
---|
[66] | 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
| 15 | package de.ugoe.cs.cpdp.training;
|
---|
| 16 |
|
---|
| 17 | import java.util.logging.Level;
|
---|
| 18 |
|
---|
| 19 | import de.ugoe.cs.cpdp.wekaclassifier.ITestAwareClassifier;
|
---|
| 20 | import de.ugoe.cs.util.console.Console;
|
---|
| 21 | import weka.classifiers.rules.ZeroR;
|
---|
| 22 | import weka.core.Instances;
|
---|
| 23 |
|
---|
[135] | 24 | /**
|
---|
| 25 | * <p>
|
---|
| 26 | * Trainer that allows classifiers access to the training data. Classifiers need to make sure that
|
---|
| 27 | * they do not use the classification.
|
---|
| 28 | * </p>
|
---|
| 29 | *
|
---|
| 30 | * @author Steffen Herbold
|
---|
| 31 | */
|
---|
[66] | 32 | public class WekaTestAwareTraining extends WekaBaseTraining implements ITestAwareTrainingStrategy {
|
---|
| 33 |
|
---|
[135] | 34 | /*
|
---|
| 35 | * (non-Javadoc)
|
---|
| 36 | *
|
---|
| 37 | * @see de.ugoe.cs.cpdp.training.ITestAwareTrainingStrategy#apply(weka.core.Instances,
|
---|
| 38 | * weka.core.Instances)
|
---|
| 39 | */
|
---|
[66] | 40 | @Override
|
---|
| 41 | public void apply(Instances testdata, Instances traindata) {
|
---|
| 42 | classifier = setupClassifier();
|
---|
[135] | 43 | if (!(classifier instanceof ITestAwareClassifier)) {
|
---|
[66] | 44 | throw new RuntimeException("classifier must implement the ITestAwareClassifier interface in order to be used as TestAwareTrainingStrategy");
|
---|
| 45 | }
|
---|
| 46 | ((ITestAwareClassifier) classifier).setTestdata(testdata);
|
---|
| 47 | try {
|
---|
| 48 | if (classifier == null) {
|
---|
| 49 | Console.traceln(Level.WARNING, String.format("classifier null!"));
|
---|
| 50 | }
|
---|
| 51 | classifier.buildClassifier(traindata);
|
---|
| 52 | }
|
---|
| 53 | catch (Exception e) {
|
---|
| 54 | if (e.getMessage().contains("Not enough training instances with class labels")) {
|
---|
| 55 | Console.traceln(Level.SEVERE,
|
---|
| 56 | "failure due to lack of instances: " + e.getMessage());
|
---|
| 57 | Console.traceln(Level.SEVERE, "training ZeroR classifier instead");
|
---|
| 58 | classifier = new ZeroR();
|
---|
| 59 | try {
|
---|
| 60 | classifier.buildClassifier(traindata);
|
---|
| 61 | }
|
---|
| 62 | catch (Exception e2) {
|
---|
| 63 | throw new RuntimeException(e2);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | else {
|
---|
| 67 | throw new RuntimeException(e);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|