source: trunk/CrossPare/src/de/ugoe/cs/cpdp/dataselection/AbstractCharacteristicSelection.java @ 54

Last change on this file since 54 was 41, checked in by sherbold, 9 years ago
  • formatted code and added copyrights
  • Property svn:mime-type set to text/plain
File size: 6.5 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.dataselection;
16
17import java.util.ArrayList;
18
19import org.apache.commons.collections4.list.SetUniqueList;
20
21import weka.core.Attribute;
22import weka.core.DenseInstance;
23import weka.core.Instance;
24import weka.core.Instances;
25import weka.experiment.Stats;
26import weka.filters.Filter;
27import weka.filters.unsupervised.attribute.Normalize;
28
29/**
30 * Abstract class that implements the foundation of setwise data selection strategies using
31 * distributional characteristics. This class provides the means to transform the data sets into
32 * their characteristic vectors.
33 *
34 * @author Steffen Herbold
35 */
36public abstract class AbstractCharacteristicSelection implements ISetWiseDataselectionStrategy {
37
38    /**
39     * vector with the distributional characteristics
40     */
41    private String[] characteristics = new String[]
42        { "mean", "stddev" };
43
44    /**
45     * Sets the distributional characteristics. The names of the characteristics are separated by
46     * blanks.
47     */
48    @Override
49    public void setParameter(String parameters) {
50        if (!"".equals(parameters)) {
51            characteristics = parameters.split(" ");
52        }
53    }
54
55    /**
56     * Transforms the data into the distributional characteristics. The first instance is the test
57     * data, followed by the training data.
58     *
59     * @param testdata
60     *            test data
61     * @param traindataSet
62     *            training data sets
63     * @return distributional characteristics of the data
64     */
65    protected Instances characteristicInstances(Instances testdata,
66                                                SetUniqueList<Instances> traindataSet)
67    {
68        // setup weka Instances for clustering
69        final ArrayList<Attribute> atts = new ArrayList<Attribute>();
70
71        final Attribute classAtt = testdata.classAttribute();
72        for (int i = 0; i < testdata.numAttributes(); i++) {
73            Attribute dataAtt = testdata.attribute(i);
74            if (!dataAtt.equals(classAtt)) {
75                for (String characteristic : characteristics) {
76                    atts.add(new Attribute(dataAtt.name() + "_" + characteristic));
77                }
78            }
79        }
80        final Instances data = new Instances("distributional_characteristics", atts, 0);
81
82        // setup data for clustering
83        double[] instanceValues = new double[atts.size()];
84        for (int i = 0; i < testdata.numAttributes(); i++) {
85            Attribute dataAtt = testdata.attribute(i);
86            if (!dataAtt.equals(classAtt)) {
87                Stats stats = testdata.attributeStats(i).numericStats;
88                for (int j = 0; j < characteristics.length; j++) {
89                    if ("mean".equals(characteristics[j])) {
90                        instanceValues[i * characteristics.length + j] = stats.mean;
91                    }
92                    else if ("stddev".equals(characteristics[j])) {
93                        instanceValues[i * characteristics.length + j] = stats.stdDev;
94                    }
95                    else if ("var".equals(characteristics[j])) {
96                        instanceValues[i * characteristics.length + j] = testdata.variance(j);
97                    }
98                    else {
99                        throw new RuntimeException("Unkown distributional characteristic: " +
100                            characteristics[j]);
101                    }
102                }
103            }
104        }
105        data.add(new DenseInstance(1.0, instanceValues));
106
107        for (Instances traindata : traindataSet) {
108            instanceValues = new double[atts.size()];
109            for (int i = 0; i < traindata.numAttributes(); i++) {
110                Attribute dataAtt = traindata.attribute(i);
111                if (!dataAtt.equals(classAtt)) {
112                    Stats stats = traindata.attributeStats(i).numericStats;
113                    for (int j = 0; j < characteristics.length; j++) {
114                        if ("mean".equals(characteristics[j])) {
115                            instanceValues[i * characteristics.length + j] = stats.mean;
116                        }
117                        else if ("stddev".equals(characteristics[j])) {
118                            instanceValues[i * characteristics.length + j] = stats.stdDev;
119                        }
120                        else if ("var".equals(characteristics[j])) {
121                            instanceValues[i * characteristics.length + j] = testdata.variance(j);
122                        }
123                        else {
124                            throw new RuntimeException("Unkown distributional characteristic: " +
125                                characteristics[j]);
126                        }
127                    }
128                }
129            }
130            Instance instance = new DenseInstance(1.0, instanceValues);
131
132            data.add(instance);
133        }
134        return data;
135    }
136
137    /**
138     * Returns the normalized distributional characteristics of the training data.
139     *
140     * @param testdata
141     *            test data
142     * @param traindataSet
143     *            training data sets
144     * @return normalized distributional characteristics of the data
145     */
146    protected Instances normalizedCharacteristicInstances(Instances testdata,
147                                                          SetUniqueList<Instances> traindataSet)
148    {
149        Instances data = characteristicInstances(testdata, traindataSet);
150        try {
151            final Normalize normalizer = new Normalize();
152            normalizer.setInputFormat(data);
153            data = Filter.useFilter(data, normalizer);
154        }
155        catch (Exception e) {
156            throw new RuntimeException(
157                                       "Unexpected exception during normalization of distributional characteristics.",
158                                       e);
159        }
160        return data;
161    }
162}
Note: See TracBrowser for help on using the repository browser.