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

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