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

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