[120] | 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 | package de.ugoe.cs.cpdp.dataselection;
|
---|
| 16 |
|
---|
| 17 | import java.util.Collections;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 |
|
---|
| 20 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
| 21 | import org.apache.commons.math3.stat.descriptive.rank.Median;
|
---|
| 22 | import org.apache.commons.math3.util.MathArrays;
|
---|
| 23 |
|
---|
| 24 | import de.ugoe.cs.cpdp.dataprocessing.MORPH;
|
---|
| 25 | import de.ugoe.cs.cpdp.util.WekaUtils;
|
---|
| 26 | import weka.core.Instance;
|
---|
| 27 | import weka.core.Instances;
|
---|
| 28 | import weka.filters.Filter;
|
---|
| 29 | import 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 | */
|
---|
| 38 | public class LACE2 implements ISetWiseDataselectionStrategy {
|
---|
| 39 |
|
---|
[135] | 40 | /**
|
---|
| 41 | * percentage of data selected by the internal CLIFF.
|
---|
| 42 | */
|
---|
[120] | 43 | private double percentage = 0.10;
|
---|
[135] | 44 |
|
---|
| 45 | /*
|
---|
| 46 | * (non-Javadoc)
|
---|
| 47 | *
|
---|
| 48 | * @see de.ugoe.cs.cpdp.IParameterizable#setParameter(java.lang.String)
|
---|
| 49 | */
|
---|
[120] | 50 | @Override
|
---|
| 51 | public void setParameter(String parameters) {
|
---|
[135] | 52 | if (parameters != null && !parameters.isEmpty()) {
|
---|
[120] | 53 | percentage = Double.parseDouble(parameters);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[135] | 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 | */
|
---|
[120] | 63 | @Override
|
---|
| 64 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
---|
| 65 | Instances selectedData = new Instances(testdata);
|
---|
| 66 | selectedData.clear();
|
---|
[135] | 67 |
|
---|
[120] | 68 | LinkedList<Instances> traindataCopy = new LinkedList<>(traindataSet);
|
---|
| 69 | Collections.shuffle(traindataCopy);
|
---|
[135] | 70 |
|
---|
[120] | 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;
|
---|
[135] | 76 |
|
---|
| 77 | for (Instances traindata : traindataCopy) {
|
---|
[120] | 78 | Instances cliffedData = cliff.applyCLIFF(traindata);
|
---|
[135] | 79 | if (minDist == Double.MIN_VALUE) {
|
---|
[120] | 80 | // determine distance for leader-follower algorithm
|
---|
| 81 | Instances sample;
|
---|
[135] | 82 | if (traindata.size() > 100) {
|
---|
[120] | 83 | Resample resample = new Resample();
|
---|
[135] | 84 | resample.setSampleSizePercent(100.0 / traindata.size() * 100.0);
|
---|
[120] | 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 | }
|
---|
[135] | 94 | }
|
---|
| 95 | else {
|
---|
[120] | 96 | sample = new Instances(traindata);
|
---|
| 97 | }
|
---|
| 98 | double[] distances = new double[sample.size()];
|
---|
[135] | 99 | for (int i = 0; i < sample.size(); i++) {
|
---|
[120] | 100 | Instance unlikeNeighbor = morph.getNearestUnlikeNeighbor(sample.get(i), sample);
|
---|
[135] | 101 | distances[i] = MathArrays.distance(WekaUtils.instanceValues(sample.get(i)),
|
---|
| 102 | WekaUtils.instanceValues(unlikeNeighbor));
|
---|
[120] | 103 | }
|
---|
| 104 | minDist = median.evaluate(distances);
|
---|
| 105 | }
|
---|
[135] | 106 | for (int i = 0; i < cliffedData.size(); i++) {
|
---|
| 107 | Instance unlikeNeighbor =
|
---|
| 108 | morph.getNearestUnlikeNeighbor(cliffedData.get(i), selectedData);
|
---|
| 109 | if (unlikeNeighbor == null) {
|
---|
[120] | 110 | selectedData.add(cliffedData.get(i));
|
---|
[135] | 111 | }
|
---|
| 112 | else {
|
---|
| 113 | double distance =
|
---|
| 114 | MathArrays.distance(WekaUtils.instanceValues(cliffedData.get(i)),
|
---|
| 115 | WekaUtils.instanceValues(unlikeNeighbor));
|
---|
| 116 | if (distance > minDist) {
|
---|
[120] | 117 | morph.morphInstance(cliffedData.get(i), cliffedData);
|
---|
| 118 | selectedData.add(cliffedData.get(i));
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
[135] | 124 |
|
---|
[120] | 125 | }
|
---|