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

Last change on this file since 98 was 86, checked in by sherbold, 9 years ago
  • switched workspace encoding to UTF-8 and fixed broken characters
  • Property svn:mime-type set to text/plain
File size: 7.8 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] = Utils.kthSmallestValue(testdata.attributeToDoubleArray(i), testdata.size()/2);
107                    }
108                    else {
109                        throw new RuntimeException("Unkown distributional characteristic: " +
110                            characteristics[j]);
111                    }
112                }
113            }
114        }
115        data.add(new DenseInstance(1.0, instanceValues));
116
117        for (Instances traindata : traindataSet) {
118            instanceValues = new double[atts.size()];
119            for (int i = 0; i < traindata.numAttributes(); i++) {
120                Attribute dataAtt = traindata.attribute(i);
121                if (!dataAtt.equals(classAtt)) {
122                    Stats stats = traindata.attributeStats(i).numericStats;
123                    for (int j = 0; j < characteristics.length; j++) {
124                        if ("mean".equals(characteristics[j])) {
125                            instanceValues[i * characteristics.length + j] = stats.mean;
126                        }
127                        else if ("stddev".equals(characteristics[j])) {
128                            instanceValues[i * characteristics.length + j] = stats.stdDev;
129                        }
130                        else if ("var".equals(characteristics[j])) {
131                            instanceValues[i * characteristics.length + j] = traindata.variance(j);
132                        }
133                        else if ("max".equals(characteristics[j])) {
134                            instanceValues[i * characteristics.length + j] = stats.max;
135                        }
136                        else if ("min".equals(characteristics[j])) {
137                            instanceValues[i * characteristics.length + j] = stats.min;
138                        }
139                        else if ("median".equals(characteristics[j])) {
140                            instanceValues[i * characteristics.length + j] = Utils.kthSmallestValue(traindata.attributeToDoubleArray(i), traindata.size()/2);
141                        }
142                        else {
143                            throw new RuntimeException("Unkown distributional characteristic: " +
144                                characteristics[j]);
145                        }
146                    }
147                }
148            }
149            Instance instance = new DenseInstance(1.0, instanceValues);
150
151            data.add(instance);
152        }
153        return data;
154    }
155
156    /**
157     * Returns the normalized distributional characteristics of the training data.
158     *
159     * @param testdata
160     *            test data
161     * @param traindataSet
162     *            training data sets
163     * @return normalized distributional characteristics of the data
164     */
165    protected Instances normalizedCharacteristicInstances(Instances testdata,
166                                                          SetUniqueList<Instances> traindataSet)
167    {
168        Instances data = characteristicInstances(testdata, traindataSet);
169        try {
170            final Normalize normalizer = new Normalize();
171            normalizer.setInputFormat(data);
172            data = Filter.useFilter(data, normalizer);
173        }
174        catch (Exception e) {
175            throw new RuntimeException(
176                                       "Unexpected exception during normalization of distributional characteristics.",
177                                       e);
178        }
179        return data;
180    }
181}
Note: See TracBrowser for help on using the repository browser.