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

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