source: trunk/CrossPare/src/de/ugoe/cs/cpdp/training/WekaTraining.java @ 99

Last change on this file since 99 was 99, checked in by sherbold, 9 years ago
  • improved error reporting
  • Property svn:mime-type set to text/plain
File size: 2.7 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.util.console.Console;
20import weka.classifiers.rules.ZeroR;
21import weka.core.Instances;
22
23/**
24 * Programmatic WekaTraining
25 *
26 * first parameter is Trainer Name. second parameter is class name
27 *
28 * all subsequent parameters are configuration params (for example for trees) Cross Validation
29 * params always come last and are prepended with -CVPARAM
30 *
31 * XML Configurations for Weka Classifiers:
32 *
33 * <pre>
34 * {@code
35 * <!-- examples -->
36 * <trainer name="WekaTraining" param="NaiveBayes weka.classifiers.bayes.NaiveBayes" />
37 * <trainer name="WekaTraining" param="Logistic weka.classifiers.functions.Logistic -R 1.0E-8 -M -1" />
38 * }
39 * </pre>
40 *
41 */
42public class WekaTraining extends WekaBaseTraining implements ITrainingStrategy {
43
44    @Override
45    public void apply(Instances traindata) {
46        classifier = setupClassifier();
47        if( classifier==null ) {
48            Console.printerr("classifier of WekaTraining is null");
49            throw new RuntimeException("classifier of WekaTraining is null");
50        }
51        try {
52            if (classifier == null) {
53                Console.traceln(Level.WARNING, String.format("classifier null!"));
54            }
55            classifier.buildClassifier(traindata);
56        }
57        catch (Exception e) {
58            if (e.getMessage().contains("Not enough training instances with class labels")) {
59                Console.traceln(Level.SEVERE,
60                                "failure due to lack of instances: " + e.getMessage());
61                Console.traceln(Level.SEVERE, "training ZeroR classifier instead");
62                classifier = new ZeroR();
63                try {
64                    classifier.buildClassifier(traindata);
65                }
66                catch (Exception e2) {
67                    throw new RuntimeException(e2);
68                }
69            }
70            else {
71                throw new RuntimeException(e);
72            }
73        }
74    }
75}
Note: See TracBrowser for help on using the repository browser.