source: trunk/CrossPare/src/de/ugoe/cs/cpdp/training/FixClass.java @ 70

Last change on this file since 70 was 41, checked in by sherbold, 9 years ago
  • formatted code and added copyrights
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 weka.classifiers.AbstractClassifier;
18import weka.classifiers.Classifier;
19import weka.core.Capabilities;
20import weka.core.Instance;
21import weka.core.Instances;
22import weka.core.Utils;
23import weka.core.Capabilities.Capability;
24
25/**
26 * Simple classifier that always predicts the same class
27 *
28 * @author Steffen Herbold
29 */
30public class FixClass extends AbstractClassifier implements ITrainingStrategy,
31    IWekaCompatibleTrainer
32{
33
34    private static final long serialVersionUID = 1L;
35
36    private double fixedClassValue = 0.0d;
37
38    /**
39     * Returns default capabilities of the classifier.
40     *
41     * @return the capabilities of this classifier
42     */
43    @Override
44    public Capabilities getCapabilities() {
45        Capabilities result = super.getCapabilities();
46        result.disableAll();
47
48        // attributes
49        result.enable(Capability.NOMINAL_ATTRIBUTES);
50        result.enable(Capability.NUMERIC_ATTRIBUTES);
51        result.enable(Capability.DATE_ATTRIBUTES);
52        result.enable(Capability.STRING_ATTRIBUTES);
53        result.enable(Capability.RELATIONAL_ATTRIBUTES);
54        result.enable(Capability.MISSING_VALUES);
55
56        // class
57        result.enable(Capability.NOMINAL_CLASS);
58        result.enable(Capability.NUMERIC_CLASS);
59        result.enable(Capability.MISSING_CLASS_VALUES);
60
61        // instances
62        result.setMinimumNumberInstances(0);
63
64        return result;
65    }
66
67    @Override
68    public void setOptions(String[] options) throws Exception {
69        fixedClassValue = Double.parseDouble(Utils.getOption('C', options));
70    }
71
72    @Override
73    public double classifyInstance(Instance instance) {
74        return fixedClassValue;
75    }
76
77    @Override
78    public void buildClassifier(Instances traindata) throws Exception {
79        // do nothing
80    }
81
82    @Override
83    public void setParameter(String parameters) {
84        try {
85            this.setOptions(parameters.split(" "));
86        }
87        catch (Exception e) {
88            e.printStackTrace();
89        }
90    }
91
92    @Override
93    public void apply(Instances traindata) {
94        // do nothing!
95    }
96
97    @Override
98    public String getName() {
99        return "FixClass";
100    }
101
102    @Override
103    public Classifier getClassifier() {
104        return this;
105    }
106
107}
Note: See TracBrowser for help on using the repository browser.