[2] | 1 | package de.ugoe.cs.cpdp.preprocessing;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.*;
|
---|
| 4 |
|
---|
| 5 | import java.util.ArrayList;
|
---|
| 6 | import java.util.LinkedList;
|
---|
| 7 |
|
---|
| 8 | import org.apache.commons.collections4.list.SetUniqueList;
|
---|
| 9 | import org.junit.Test;
|
---|
| 10 |
|
---|
| 11 | import de.ugoe.cs.cpdp.dataprocessing.AttributeRemoval;
|
---|
| 12 |
|
---|
| 13 | import weka.core.Attribute;
|
---|
| 14 | import weka.core.DenseInstance;
|
---|
| 15 | import weka.core.Instances;
|
---|
| 16 |
|
---|
| 17 | public class AttributeRemoverTest {
|
---|
| 18 |
|
---|
| 19 | @Test
|
---|
| 20 | public void testApply_1() {
|
---|
| 21 | ArrayList<Attribute> attributes = new ArrayList<Attribute>();
|
---|
| 22 | attributes.add(new Attribute("attr1"));
|
---|
| 23 | attributes.add(new Attribute("class"));
|
---|
| 24 | attributes.add(new Attribute("attr2"));
|
---|
| 25 | Instances instances = new Instances("test", attributes, 0);
|
---|
| 26 | instances.setClassIndex(1);
|
---|
| 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 |
|
---|
| 32 | instances.add(new DenseInstance(1.0, value1));
|
---|
| 33 | instances.add(new DenseInstance(1.0, value2));
|
---|
| 34 | instances.add(new DenseInstance(1.0, value3));
|
---|
| 35 |
|
---|
| 36 | AttributeRemoval processor = new AttributeRemoval();
|
---|
| 37 | processor.setParameter("attr2");
|
---|
| 38 | processor.apply(instances, SetUniqueList.setUniqueList(new LinkedList<Instances>()) );
|
---|
| 39 |
|
---|
| 40 | double[] expected1 = new double[]{1.5, 0.0};
|
---|
| 41 | double[] expected2 = new double[]{1.4, 1.0};
|
---|
| 42 | double[] expected3 = new double[]{1.6, 0.0};
|
---|
| 43 |
|
---|
| 44 | assertEquals(2, instances.numAttributes());
|
---|
| 45 | assertArrayEquals(expected1, instances.instance(0).toDoubleArray(), 0.0001);
|
---|
| 46 | assertArrayEquals(expected2, instances.instance(1).toDoubleArray(), 0.0001);
|
---|
| 47 | assertArrayEquals(expected3, instances.instance(2).toDoubleArray(), 0.0001);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | @Test
|
---|
| 51 | public void testApply_2() {
|
---|
| 52 | ArrayList<Attribute> attributes = new ArrayList<Attribute>();
|
---|
| 53 | attributes.add(new Attribute("attr1"));
|
---|
| 54 | attributes.add(new Attribute("class"));
|
---|
| 55 | attributes.add(new Attribute("attr2"));
|
---|
| 56 | Instances instances = new Instances("test", attributes, 0);
|
---|
| 57 | instances.setClassIndex(1);
|
---|
| 58 |
|
---|
| 59 | double[] value1 = new double[]{1.5, 0.0, 3.0};
|
---|
| 60 | double[] value2 = new double[]{1.4, 1.0, 6.0};
|
---|
| 61 | double[] value3 = new double[]{1.6, 0.0, 15.0};
|
---|
| 62 |
|
---|
| 63 | instances.add(new DenseInstance(1.0, value1));
|
---|
| 64 | instances.add(new DenseInstance(1.0, value2));
|
---|
| 65 | instances.add(new DenseInstance(1.0, value3));
|
---|
| 66 |
|
---|
| 67 | AttributeRemoval processor = new AttributeRemoval();
|
---|
| 68 | processor.setParameter("attr2 attr1");
|
---|
| 69 | processor.apply(instances, SetUniqueList.setUniqueList(new LinkedList<Instances>()) );
|
---|
| 70 |
|
---|
| 71 | double[] expected1 = new double[]{0.0};
|
---|
| 72 | double[] expected2 = new double[]{1.0};
|
---|
| 73 | double[] expected3 = new double[]{0.0};
|
---|
| 74 |
|
---|
| 75 | assertEquals(1, instances.numAttributes());
|
---|
| 76 | assertArrayEquals(expected1, instances.instance(0).toDoubleArray(), 0.0001);
|
---|
| 77 | assertArrayEquals(expected2, instances.instance(1).toDoubleArray(), 0.0001);
|
---|
| 78 | assertArrayEquals(expected3, instances.instance(2).toDoubleArray(), 0.0001);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | }
|
---|