| 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 |
|
|---|
| 15 | package de.ugoe.cs.cpdp.dataselection;
|
|---|
| 16 |
|
|---|
| 17 | import java.util.Arrays;
|
|---|
| 18 |
|
|---|
| 19 | import org.apache.commons.collections4.list.SetUniqueList;
|
|---|
| 20 |
|
|---|
| 21 | import weka.core.Instances;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Implements CLIFF data pruning.
|
|---|
| 25 | *
|
|---|
| 26 | * @author Steffen Herbold
|
|---|
| 27 | */
|
|---|
| 28 | public class CLIFF implements IPointWiseDataselectionStrategy, ISetWiseDataselectionStrategy {
|
|---|
| 29 |
|
|---|
| 30 | private double percentage = 0.10;
|
|---|
| 31 |
|
|---|
| 32 | private final int numRanges = 10;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Sets the number of neighbors.
|
|---|
| 36 | *
|
|---|
| 37 | * @param parameters
|
|---|
| 38 | * number of neighbors
|
|---|
| 39 | */
|
|---|
| 40 | @Override
|
|---|
| 41 | public void setParameter(String parameters) {
|
|---|
| 42 | if( parameters!=null ) {
|
|---|
| 43 | percentage = Double.parseDouble(parameters);
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * @see de.ugoe.cs.cpdp.dataselection.SetWiseDataselectionStrategy#apply(weka.core.Instances,
|
|---|
| 49 | * org.apache.commons.collections4.list.SetUniqueList)
|
|---|
| 50 | */
|
|---|
| 51 | @Override
|
|---|
| 52 | public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
|---|
| 53 | for( Instances traindata : traindataSet ) {
|
|---|
| 54 | applyCLIFF(traindata);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * @see de.ugoe.cs.cpdp.dataselection.PointWiseDataselectionStrategy#apply(weka.core.Instances,
|
|---|
| 60 | * weka.core.Instances)
|
|---|
| 61 | */
|
|---|
| 62 | @Override
|
|---|
| 63 | public Instances apply(Instances testdata, Instances traindata) {
|
|---|
| 64 | return applyCLIFF(traindata);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | private Instances applyCLIFF(Instances data) {
|
|---|
| 68 | final double[][] powerAttributes = new double[data.size()][data.numAttributes()];
|
|---|
| 69 | final double[] powerEntity = new double[data.size()];
|
|---|
| 70 |
|
|---|
| 71 | final int[] counts = data.attributeStats(data.classIndex()).nominalCounts;
|
|---|
| 72 | final double probDefect = data.numInstances() / (double) counts[1];
|
|---|
| 73 |
|
|---|
| 74 | for( int j=0; j<data.numAttributes(); j++ ) {
|
|---|
| 75 | if( data.attribute(j)!=data.classAttribute()) {
|
|---|
| 76 | final double[] ranges = getRanges(data, j);
|
|---|
| 77 | final double[] probDefectRange = getRangeProbabilities(data, j, ranges);
|
|---|
| 78 |
|
|---|
| 79 | for( int i=0 ; i<data.numInstances() ; i++ ) {
|
|---|
| 80 | final double value = data.instance(i).value(j);
|
|---|
| 81 | final int range = determineRange(ranges, value);
|
|---|
| 82 | double probClass, probNotClass, probRangeClass, probRangeNotClass;
|
|---|
| 83 | if( data.instance(i).classValue()==1 ) {
|
|---|
| 84 | probClass = probDefect;
|
|---|
| 85 | probNotClass = 1.0-probDefect;
|
|---|
| 86 | probRangeClass = probDefectRange[range];
|
|---|
| 87 | probRangeNotClass = 1.0-probDefectRange[range];
|
|---|
| 88 | } else {
|
|---|
| 89 | probClass = 1.0-probDefect;
|
|---|
| 90 | probNotClass = probDefect;
|
|---|
| 91 | probRangeClass = 1.0-probDefectRange[range];
|
|---|
| 92 | probRangeNotClass = probDefectRange[range];
|
|---|
| 93 | }
|
|---|
| 94 | powerAttributes[i][j] = Math.pow(probRangeClass, 2.0)/(probRangeClass*probClass+probRangeNotClass*probNotClass);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | for( int i=0; i<data.numInstances(); i++ ) {
|
|---|
| 100 | powerEntity[i] = 1.0;
|
|---|
| 101 | for (int j=0; j<data.numAttributes() ; j++ ) {
|
|---|
| 102 | powerEntity[i] *= powerAttributes[i][j];
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | double[] sortedPower = powerEntity.clone();
|
|---|
| 106 | Arrays.sort(sortedPower);
|
|---|
| 107 | double cutOff = sortedPower[(int) (data.numInstances()*(1-percentage))];
|
|---|
| 108 |
|
|---|
| 109 | final Instances selected = new Instances(data);
|
|---|
| 110 | selected.delete();
|
|---|
| 111 | for (int i=0; i<data.numInstances(); i++) {
|
|---|
| 112 | if( powerEntity[i]>=cutOff ) {
|
|---|
| 113 | selected.add(data.instance(i));
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | return selected;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | private double[] getRanges(Instances data, int j) {
|
|---|
| 120 | double[] values = new double[numRanges+1];
|
|---|
| 121 | for( int k=0; k<numRanges; k++ ) {
|
|---|
| 122 | values[k] = data.kthSmallestValue(j, (int) (data.size()*(k+1.0)/numRanges));
|
|---|
| 123 | }
|
|---|
| 124 | values[numRanges] = data.attributeStats(j).numericStats.max;
|
|---|
| 125 | return values;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | private double[] getRangeProbabilities(Instances data, int j, double[] ranges) {
|
|---|
| 129 | double[] probDefectRange = new double[numRanges];
|
|---|
| 130 | int[] countRange = new int[numRanges];
|
|---|
| 131 | int[] countDefect = new int[numRanges];
|
|---|
| 132 | for( int i=0; i<data.numInstances() ; i++ ) {
|
|---|
| 133 | int range = determineRange(ranges, data.instance(i).value(j));
|
|---|
| 134 | countRange[range]++;
|
|---|
| 135 | if( data.instance(i).classValue()== 1 ) {
|
|---|
| 136 | countDefect[range]++;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | }
|
|---|
| 140 | for( int k=0; k<numRanges; k++ ) {
|
|---|
| 141 | probDefectRange[k] = ((double) countDefect[k]) / countRange[k];
|
|---|
| 142 | }
|
|---|
| 143 | return probDefectRange;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | private int determineRange(double[] ranges, double value) {
|
|---|
| 147 | for( int k=0; k<numRanges; k++ ) {
|
|---|
| 148 | if( value<=ranges[k+1] ) {
|
|---|
| 149 | return k;
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | throw new RuntimeException("invalid range or value");
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|