source: trunk/CrossPare/src/de/ugoe/cs/cpdp/dataselection/LACE2.java @ 146

Last change on this file since 146 was 135, checked in by sherbold, 8 years ago
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 4.8 KB
Line 
1// Copyright 2016 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.Collections;
18import java.util.LinkedList;
19
20import org.apache.commons.collections4.list.SetUniqueList;
21import org.apache.commons.math3.stat.descriptive.rank.Median;
22import org.apache.commons.math3.util.MathArrays;
23
24import de.ugoe.cs.cpdp.dataprocessing.MORPH;
25import de.ugoe.cs.cpdp.util.WekaUtils;
26import weka.core.Instance;
27import weka.core.Instances;
28import weka.filters.Filter;
29import weka.filters.supervised.instance.Resample;
30
31/**
32 * <p>
33 * Implements LACE2 data privacy filter after Peters et al.
34 * </p>
35 *
36 * @author Steffen Herbold
37 */
38public class LACE2 implements ISetWiseDataselectionStrategy {
39
40    /**
41     * percentage of data selected by the internal CLIFF.
42     */
43    private double percentage = 0.10;
44
45    /*
46     * (non-Javadoc)
47     *
48     * @see de.ugoe.cs.cpdp.IParameterizable#setParameter(java.lang.String)
49     */
50    @Override
51    public void setParameter(String parameters) {
52        if (parameters != null && !parameters.isEmpty()) {
53            percentage = Double.parseDouble(parameters);
54        }
55    }
56
57    /*
58     * (non-Javadoc)
59     *
60     * @see de.ugoe.cs.cpdp.dataselection.ISetWiseDataselectionStrategy#apply(weka.core.Instances,
61     * org.apache.commons.collections4.list.SetUniqueList)
62     */
63    @Override
64    public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
65        Instances selectedData = new Instances(testdata);
66        selectedData.clear();
67
68        LinkedList<Instances> traindataCopy = new LinkedList<>(traindataSet);
69        Collections.shuffle(traindataCopy);
70
71        CLIFF cliff = new CLIFF();
72        cliff.setParameter(Double.toString(percentage));
73        MORPH morph = new MORPH();
74        Median median = new Median();
75        double minDist = Double.MIN_VALUE;
76
77        for (Instances traindata : traindataCopy) {
78            Instances cliffedData = cliff.applyCLIFF(traindata);
79            if (minDist == Double.MIN_VALUE) {
80                // determine distance for leader-follower algorithm
81                Instances sample;
82                if (traindata.size() > 100) {
83                    Resample resample = new Resample();
84                    resample.setSampleSizePercent(100.0 / traindata.size() * 100.0);
85                    resample.setBiasToUniformClass(0.0);
86                    resample.setNoReplacement(true);
87                    try {
88                        resample.setInputFormat(traindata);
89                        sample = Filter.useFilter(traindata, resample);
90                    }
91                    catch (Exception e) {
92                        throw new RuntimeException(e);
93                    }
94                }
95                else {
96                    sample = new Instances(traindata);
97                }
98                double[] distances = new double[sample.size()];
99                for (int i = 0; i < sample.size(); i++) {
100                    Instance unlikeNeighbor = morph.getNearestUnlikeNeighbor(sample.get(i), sample);
101                    distances[i] = MathArrays.distance(WekaUtils.instanceValues(sample.get(i)),
102                                                       WekaUtils.instanceValues(unlikeNeighbor));
103                }
104                minDist = median.evaluate(distances);
105            }
106            for (int i = 0; i < cliffedData.size(); i++) {
107                Instance unlikeNeighbor =
108                    morph.getNearestUnlikeNeighbor(cliffedData.get(i), selectedData);
109                if (unlikeNeighbor == null) {
110                    selectedData.add(cliffedData.get(i));
111                }
112                else {
113                    double distance =
114                        MathArrays.distance(WekaUtils.instanceValues(cliffedData.get(i)),
115                                            WekaUtils.instanceValues(unlikeNeighbor));
116                    if (distance > minDist) {
117                        morph.morphInstance(cliffedData.get(i), cliffedData);
118                        selectedData.add(cliffedData.get(i));
119                    }
120                }
121            }
122        }
123    }
124
125}
Note: See TracBrowser for help on using the repository browser.