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

Last change on this file since 71 was 42, checked in by sherbold, 9 years ago
  • added CLA/CLAMI data processors (ASE 2015)
  • modified normal WEKA training to have a fallback in case to few instances of a certain class (defect-prone, non-defect-prone) are available: it now uses ZeroR in that case, i.e., a trivial classifier that always predicts the class that appears more often.
  • 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.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        PrintStream errStr = System.err;
51        System.setErr(new PrintStream(new NullOutputStream()));
52        try {
53            if (classifier == null) {
54                Console.traceln(Level.WARNING, String.format("classifier null!"));
55            }
56            classifier.buildClassifier(traindata);
57        }
58        catch (Exception e) {
59            if (e.getMessage().contains("Not enough training instances with class labels")) {
60                Console.traceln(Level.SEVERE,
61                                "failure due to lack of instances: " + e.getMessage());
62                Console.traceln(Level.SEVERE, "training ZeroR classifier instead");
63                classifier = new ZeroR();
64                try {
65                    classifier.buildClassifier(traindata);
66                }
67                catch (Exception e2) {
68                    throw new RuntimeException(e2);
69                }
70            }
71            else {
72                throw new RuntimeException(e);
73            }
74        }
75        finally {
76            System.setErr(errStr);
77        }
78    }
79}
Note: See TracBrowser for help on using the repository browser.