[2] | 1 | package de.ugoe.cs.cpdp.weighting;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.*;
|
---|
| 4 |
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.List;
|
---|
| 7 |
|
---|
| 8 | import org.junit.Test;
|
---|
| 9 |
|
---|
| 10 | import de.ugoe.cs.cpdp.dataprocessing.BiasedWeights;
|
---|
| 11 |
|
---|
| 12 | import weka.core.Attribute;
|
---|
| 13 | import weka.core.DenseInstance;
|
---|
| 14 | import weka.core.Instances;
|
---|
| 15 |
|
---|
| 16 | public class BiasedWeightsTest {
|
---|
| 17 |
|
---|
| 18 | @Test
|
---|
| 19 | public void testApply_1() {
|
---|
| 20 | ArrayList<Attribute> attributes = new ArrayList<Attribute>();
|
---|
| 21 | attributes.add(new Attribute("attr1"));
|
---|
| 22 | List<String> classAttVals = new ArrayList<String>();
|
---|
| 23 | classAttVals.add("0");
|
---|
| 24 | classAttVals.add("1");
|
---|
| 25 | attributes.add(new Attribute("bug", classAttVals));
|
---|
| 26 | attributes.add(new Attribute("attr2"));
|
---|
| 27 |
|
---|
| 28 | double[] value1 = new double[]{1.5, 0.0, 3.0};
|
---|
| 29 | double[] value2 = new double[]{1.4, 1.0, 6.0};
|
---|
| 30 | double[] value3 = new double[]{1.6, 0.0, 15.0};
|
---|
| 31 | double[] value4 = new double[]{ 3.0, 0.0, 1.5};
|
---|
| 32 | double[] value5 = new double[]{ 6.0, 1.0, 1.4 };
|
---|
| 33 | double[] value6 = new double[]{15.0, 0.0, 1.6};
|
---|
| 34 | double[] value7 = new double[]{ 6.0, 0.0, 1.4 };
|
---|
| 35 | double[] value8 = new double[]{15.0, 0.0, 1.6};
|
---|
| 36 |
|
---|
| 37 | Instances instances = new Instances("test", attributes, 0);
|
---|
| 38 | instances.setClassIndex(1);
|
---|
| 39 |
|
---|
| 40 | instances.add(new DenseInstance(1.0, value1));
|
---|
| 41 | instances.add(new DenseInstance(1.0, value2));
|
---|
| 42 | instances.add(new DenseInstance(1.0, value3));
|
---|
| 43 | instances.add(new DenseInstance(1.0, value4));
|
---|
| 44 | instances.add(new DenseInstance(1.0, value5));
|
---|
| 45 | instances.add(new DenseInstance(1.0, value6));
|
---|
| 46 | instances.add(new DenseInstance(1.0, value7));
|
---|
| 47 | instances.add(new DenseInstance(1.0, value8));
|
---|
| 48 |
|
---|
| 49 | BiasedWeights processor = new BiasedWeights();
|
---|
| 50 | processor.apply(new Instances(instances), instances);
|
---|
| 51 |
|
---|
| 52 | assertEquals(0.6666666d, instances.instance(0).weight(), 0.00001);
|
---|
| 53 | assertEquals(2.0d, instances.instance(1).weight(), 0.00001);
|
---|
| 54 | assertEquals(0.6666666d, instances.instance(2).weight(), 0.00001);
|
---|
| 55 | assertEquals(0.6666666d, instances.instance(3).weight(), 0.00001);
|
---|
| 56 | assertEquals(2.0d, instances.instance(4).weight(), 0.00001);
|
---|
| 57 | assertEquals(0.6666666d, instances.instance(5).weight(), 0.00001);
|
---|
| 58 | assertEquals(0.6666666d, instances.instance(6).weight(), 0.00001);
|
---|
| 59 | assertEquals(0.6666666d, instances.instance(7).weight(), 0.00001);
|
---|
| 60 | assertEquals(instances.numInstances(), instances.sumOfWeights(), 0.0001);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Test
|
---|
| 64 | public void testApply_2() {
|
---|
| 65 | ArrayList<Attribute> attributes = new ArrayList<Attribute>();
|
---|
| 66 | attributes.add(new Attribute("attr1"));
|
---|
| 67 | List<String> classAttVals = new ArrayList<String>();
|
---|
| 68 | classAttVals.add("0");
|
---|
| 69 | classAttVals.add("1");
|
---|
| 70 | attributes.add(new Attribute("bug", classAttVals));
|
---|
| 71 | attributes.add(new Attribute("attr2"));
|
---|
| 72 |
|
---|
| 73 | double[] value1 = new double[]{1.5, 0.0, 3.0};
|
---|
| 74 | double[] value2 = new double[]{1.4, 1.0, 6.0};
|
---|
| 75 | double[] value3 = new double[]{1.6, 0.0, 15.0};
|
---|
| 76 | double[] value4 = new double[]{ 3.0, 0.0, 1.5};
|
---|
| 77 | double[] value5 = new double[]{ 6.0, 1.0, 1.4 };
|
---|
| 78 | double[] value6 = new double[]{15.0, 0.0, 1.6};
|
---|
| 79 | double[] value7 = new double[]{ 6.0, 0.0, 1.4 };
|
---|
| 80 | double[] value8 = new double[]{15.0, 0.0, 1.6};
|
---|
| 81 |
|
---|
| 82 | Instances instances = new Instances("test", attributes, 0);
|
---|
| 83 | instances.setClassIndex(1);
|
---|
| 84 |
|
---|
| 85 | instances.add(new DenseInstance(1.0, value1));
|
---|
| 86 | instances.add(new DenseInstance(1.0, value2));
|
---|
| 87 | instances.add(new DenseInstance(1.0, value3));
|
---|
| 88 | instances.add(new DenseInstance(1.0, value4));
|
---|
| 89 | instances.add(new DenseInstance(1.0, value5));
|
---|
| 90 | instances.add(new DenseInstance(1.0, value6));
|
---|
| 91 | instances.add(new DenseInstance(1.0, value7));
|
---|
| 92 | instances.add(new DenseInstance(1.0, value8));
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | BiasedWeights processor = new BiasedWeights();
|
---|
| 96 | processor.setParameter("0.7");
|
---|
| 97 | processor.apply(new Instances(instances), instances);
|
---|
| 98 |
|
---|
| 99 | assertEquals(0.4, instances.instance(0).weight(), 0.00001);
|
---|
| 100 | assertEquals(2.8, instances.instance(1).weight(), 0.00001);
|
---|
| 101 | assertEquals(0.4, instances.instance(2).weight(), 0.00001);
|
---|
| 102 | assertEquals(0.4, instances.instance(3).weight(), 0.00001);
|
---|
| 103 | assertEquals(2.8, instances.instance(4).weight(), 0.00001);
|
---|
| 104 | assertEquals(0.4, instances.instance(5).weight(), 0.00001);
|
---|
| 105 | assertEquals(0.4, instances.instance(6).weight(), 0.00001);
|
---|
| 106 | assertEquals(0.4, instances.instance(7).weight(), 0.00001);
|
---|
| 107 | assertEquals(instances.numInstances(), instances.sumOfWeights(), 0.0001);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | }
|
---|