| 1 | package de.ugoe.cs.cpdp.dataprocessing;
|
|---|
| 2 |
|
|---|
| 3 | import org.apache.commons.collections4.list.SetUniqueList;
|
|---|
| 4 |
|
|---|
| 5 | import weka.core.Instance;
|
|---|
| 6 | import weka.core.Instances;
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 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
|
|---|
| 13 | * @author Steffen Herbold
|
|---|
| 14 | */
|
|---|
| 15 | public class BiasedWeights implements IProcessesingStrategy, ISetWiseProcessingStrategy {
|
|---|
| 16 |
|
|---|
| 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 | }
|
|---|
| 31 |
|
|---|
| 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 | }
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | }
|
|---|