| 1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany
|
|---|
| 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 |
|
|---|
| 24 | // TODO comment
|
|---|
| 25 | public class WekaTestAwareTraining extends WekaBaseTraining implements ITestAwareTrainingStrategy {
|
|---|
| 26 |
|
|---|
| 27 | @Override
|
|---|
| 28 | public void apply(Instances testdata, Instances traindata) {
|
|---|
| 29 | classifier = setupClassifier();
|
|---|
| 30 | if( !(classifier instanceof ITestAwareClassifier) ) {
|
|---|
| 31 | throw new RuntimeException("classifier must implement the ITestAwareClassifier interface in order to be used as TestAwareTrainingStrategy");
|
|---|
| 32 | }
|
|---|
| 33 | ((ITestAwareClassifier) classifier).setTestdata(testdata);
|
|---|
| 34 | try {
|
|---|
| 35 | if (classifier == null) {
|
|---|
| 36 | Console.traceln(Level.WARNING, String.format("classifier null!"));
|
|---|
| 37 | }
|
|---|
| 38 | classifier.buildClassifier(traindata);
|
|---|
| 39 | }
|
|---|
| 40 | catch (Exception e) {
|
|---|
| 41 | if (e.getMessage().contains("Not enough training instances with class labels")) {
|
|---|
| 42 | Console.traceln(Level.SEVERE,
|
|---|
| 43 | "failure due to lack of instances: " + e.getMessage());
|
|---|
| 44 | Console.traceln(Level.SEVERE, "training ZeroR classifier instead");
|
|---|
| 45 | classifier = new ZeroR();
|
|---|
| 46 | try {
|
|---|
| 47 | classifier.buildClassifier(traindata);
|
|---|
| 48 | }
|
|---|
| 49 | catch (Exception e2) {
|
|---|
| 50 | throw new RuntimeException(e2);
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | else {
|
|---|
| 54 | throw new RuntimeException(e);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|