- Timestamp:
- 07/18/16 12:26:03 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/dataprocessing/MORPH.java
r120 r135 25 25 26 26 /** 27 * Implements the MORPH data privatization. 27 * Implements the MORPH data privatization. 28 28 * 29 29 * … … 36 36 */ 37 37 Random rand = new Random(); 38 38 39 39 /** 40 40 * parameter alpha for MORPH, default is 0.15 41 41 */ 42 42 double alpha = 0.15; 43 43 44 44 /** 45 45 * parameter beta for MORPH, default is 0.35 46 46 */ 47 47 double beta = 0.35; 48 48 49 49 /** 50 50 * Does not have parameters. String is ignored. … … 57 57 if (parameters != null && !parameters.equals("")) { 58 58 String[] values = parameters.split(" "); 59 if ( values.length!=2) {59 if (values.length != 2) { 60 60 throw new InvalidParameterException("MORPH requires two doubles as parameter or no parameters to use default values"); 61 61 } … … 63 63 alpha = Double.parseDouble(values[0]); 64 64 beta = Double.parseDouble(values[1]); 65 } catch(NumberFormatException e) { 65 } 66 catch (NumberFormatException e) { 66 67 throw new InvalidParameterException("MORPH requires two doubles as parameter or no parameters to use default values"); 67 68 } … … 75 76 @Override 76 77 public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) { 77 for ( Instances traindata : traindataSet) {78 for (Instances traindata : traindataSet) { 78 79 applyMORPH(traindata); 79 80 } … … 88 89 applyMORPH(traindata); 89 90 } 90 91 91 92 /** 92 93 * … … 95 96 * </p> 96 97 * 97 * @param data data to which the processor is applied 98 * @param data 99 * data to which the processor is applied 98 100 */ 99 101 public void applyMORPH(Instances data) { 100 for (int i =0; i<data.numInstances(); i++) {102 for (int i = 0; i < data.numInstances(); i++) { 101 103 morphInstance(data.get(i), data); 102 104 } 103 105 } 104 106 105 107 /** 106 108 * <p> … … 108 110 * </p> 109 111 * 110 * @param instance instance that is morphed 111 * @param data data based on which the instance is morphed 112 * @param instance 113 * instance that is morphed 114 * @param data 115 * data based on which the instance is morphed 112 116 */ 113 117 public void morphInstance(Instance instance, Instances data) { 114 118 Instance nearestUnlikeNeighbor = getNearestUnlikeNeighbor(instance, data); 115 if( nearestUnlikeNeighbor==null ) { 116 throw new RuntimeException("could not find nearest unlike neighbor within the data: " + data.relationName()); 119 if (nearestUnlikeNeighbor == null) { 120 throw new RuntimeException("could not find nearest unlike neighbor within the data: " + 121 data.relationName()); 117 122 } 118 for( int j=0; j<data.numAttributes() ; j++ ) { 119 if( data.attribute(j)!=data.classAttribute() && data.attribute(j).isNumeric()) { 120 double randVal = rand.nextDouble()*(beta-alpha)+alpha; 121 instance.setValue(j, instance.value(j) + randVal*(instance.value(j)-nearestUnlikeNeighbor.value(j)) ); 123 for (int j = 0; j < data.numAttributes(); j++) { 124 if (data.attribute(j) != data.classAttribute() && data.attribute(j).isNumeric()) { 125 double randVal = rand.nextDouble() * (beta - alpha) + alpha; 126 instance.setValue(j, instance.value(j) + 127 randVal * (instance.value(j) - nearestUnlikeNeighbor.value(j))); 122 128 } 123 129 } 124 130 } 125 131 126 132 /** 127 133 * <p> 128 * Determines the nearest unlike neighbor of an instance. 134 * Determines the nearest unlike neighbor of an instance. 129 135 * </p> 130 136 * 131 * @param instance instance to which the nearest unlike neighbor is determined 132 * @param data data where the nearest unlike neighbor is determined from 137 * @param instance 138 * instance to which the nearest unlike neighbor is determined 139 * @param data 140 * data where the nearest unlike neighbor is determined from 133 141 * @return nearest unlike instance 134 142 */ 135 143 public Instance getNearestUnlikeNeighbor(Instance instance, Instances data) { 136 144 Instance nearestUnlikeNeighbor = null; 137 138 double[] instanceVector = new double[data.numAttributes() -1];145 146 double[] instanceVector = new double[data.numAttributes() - 1]; 139 147 int tmp = 0; 140 for ( int j=0; j<data.numAttributes(); j++) {141 if ( data.attribute(j)!=data.classAttribute() && data.attribute(j).isNumeric()) {148 for (int j = 0; j < data.numAttributes(); j++) { 149 if (data.attribute(j) != data.classAttribute() && data.attribute(j).isNumeric()) { 142 150 instanceVector[tmp] = instance.value(j); 143 151 } 144 152 } 145 153 146 154 double minDistance = Double.MAX_VALUE; 147 for ( int i=0 ; i<data.numInstances() ; i++) {148 if ( instance.classValue() != data.instance(i).classValue()) {155 for (int i = 0; i < data.numInstances(); i++) { 156 if (instance.classValue() != data.instance(i).classValue()) { 149 157 double[] otherVector = new double[data.numAttributes() - 1]; 150 158 tmp = 0; 151 159 for (int j = 0; j < data.numAttributes(); j++) { 152 if (data.attribute(j) != data.classAttribute() && data.attribute(j).isNumeric()) { 160 if (data.attribute(j) != data.classAttribute() && 161 data.attribute(j).isNumeric()) 162 { 153 163 otherVector[tmp++] = data.instance(i).value(j); 154 164 } 155 165 } 156 if ( MathArrays.distance(instanceVector, otherVector)<minDistance) {166 if (MathArrays.distance(instanceVector, otherVector) < minDistance) { 157 167 minDistance = MathArrays.distance(instanceVector, otherVector); 158 168 nearestUnlikeNeighbor = data.instance(i);
Note: See TracChangeset
for help on using the changeset viewer.