- Timestamp:
- 09/24/15 10:59:05 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CrossPare/src/de/ugoe/cs/cpdp/dataprocessing/BiasedWeights.java
r2 r41 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 1 15 package de.ugoe.cs.cpdp.dataprocessing; 2 16 … … 7 21 8 22 /** 9 * Sets the bias of the weights of the training data. By using a bias of 0.5 (default value) the total weight of the positive instances (i.e. 10 * fault-prone) is equal to the total weight of the negative instances (i.e. non-fault-prone). Otherwise the weights between the two will be 11 * distributed according to the bias, where <0.5 means in favor of the negative instances and >0.5 in favor of the positive instances. 12 * equal to the total weight of the test 23 * Sets the bias of the weights of the training data. By using a bias of 0.5 (default value) the 24 * total weight of the positive instances (i.e. fault-prone) is equal to the total weight of the 25 * negative instances (i.e. non-fault-prone). Otherwise the weights between the two will be 26 * distributed according to the bias, where <0.5 means in favor of the negative instances and 27 * >0.5 in favor of the positive instances. equal to the total weight of the test 28 * 13 29 * @author Steffen Herbold 14 30 */ 15 31 public class BiasedWeights implements IProcessesingStrategy, ISetWiseProcessingStrategy { 16 32 17 /** 18 * bias used for the weighting 19 */ 20 private double bias = 0.5; 21 22 23 /** 24 * Sets the bias to be used for weighting. 25 * @param parameters string with the bias 26 */ 27 @Override 28 public void setParameter(String parameters) { 29 bias = Double.parseDouble(parameters); 30 } 33 /** 34 * bias used for the weighting 35 */ 36 private double bias = 0.5; 31 37 32 /** 33 * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances, weka.core.Instances) 34 */ 35 @Override 36 public void apply(Instances testdata, Instances traindata) { 37 //setBiasedWeights(testdata); 38 setBiasedWeights(traindata); 39 } 40 41 /** 42 * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances, org.apache.commons.collections4.list.SetUniqueList) 43 */ 44 @Override 45 public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) { 46 for( Instances traindata : traindataSet ) { 47 setBiasedWeights(traindata); 48 } 49 } 50 51 /** 52 * Helper method that sets the weights for a given data set. 53 * @param data data set whose weights are set 54 */ 55 private void setBiasedWeights(Instances data) { 56 final int classIndex = data.classIndex(); 57 58 final int[] counts = data.attributeStats(classIndex).nominalCounts; 59 60 final double weightNegatives = ((1-bias)*data.numInstances()) / counts[0]; 61 final double weightPositives = (bias*data.numInstances()) / counts[1]; 62 63 64 for( int i=0 ; i<data.numInstances() ; i++ ) { 65 Instance instance = data.instance(i); 66 if( instance.value(classIndex)==0 ) { 67 instance.setWeight(weightNegatives); 68 } 69 if( instance.value(classIndex)==1 ) { 70 instance.setWeight(weightPositives); 71 } 72 } 73 } 38 /** 39 * Sets the bias to be used for weighting. 40 * 41 * @param parameters 42 * string with the bias 43 */ 44 @Override 45 public void setParameter(String parameters) { 46 bias = Double.parseDouble(parameters); 47 } 74 48 75 49 /** 50 * @see de.ugoe.cs.cpdp.dataprocessing.ProcessesingStrategy#apply(weka.core.Instances, 51 * weka.core.Instances) 52 */ 53 @Override 54 public void apply(Instances testdata, Instances traindata) { 55 // setBiasedWeights(testdata); 56 setBiasedWeights(traindata); 57 } 58 59 /** 60 * @see de.ugoe.cs.cpdp.dataprocessing.SetWiseProcessingStrategy#apply(weka.core.Instances, 61 * org.apache.commons.collections4.list.SetUniqueList) 62 */ 63 @Override 64 public void apply(Instances testdata, SetUniqueList<Instances> traindataSet) { 65 for (Instances traindata : traindataSet) { 66 setBiasedWeights(traindata); 67 } 68 } 69 70 /** 71 * Helper method that sets the weights for a given data set. 72 * 73 * @param data 74 * data set whose weights are set 75 */ 76 private void setBiasedWeights(Instances data) { 77 final int classIndex = data.classIndex(); 78 79 final int[] counts = data.attributeStats(classIndex).nominalCounts; 80 81 final double weightNegatives = ((1 - bias) * data.numInstances()) / counts[0]; 82 final double weightPositives = (bias * data.numInstances()) / counts[1]; 83 84 for (int i = 0; i < data.numInstances(); i++) { 85 Instance instance = data.instance(i); 86 if (instance.value(classIndex) == 0) { 87 instance.setWeight(weightNegatives); 88 } 89 if (instance.value(classIndex) == 1) { 90 instance.setWeight(weightPositives); 91 } 92 } 93 } 76 94 77 95 }
Note: See TracChangeset
for help on using the changeset viewer.