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

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