source: trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaTestAwareTraining.java @ 116

Last change on this file since 116 was 99, checked in by sherbold, 9 years ago
  • improved error reporting
  • Property svn:mime-type set to text/plain
File size: 2.3 KB
Line 
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
15package de.ugoe.cs.cpdp.training;
16
17import java.util.logging.Level;
18
19import de.ugoe.cs.cpdp.wekaclassifier.ITestAwareClassifier;
20import de.ugoe.cs.util.console.Console;
21import weka.classifiers.rules.ZeroR;
22import weka.core.Instances;
23
24// TODO comment
25public 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}
Note: See TracBrowser for help on using the repository browser.