| 1 | package de.ugoe.cs.cpdp.dataselection;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.ArrayList;
|
|---|
| 4 |
|
|---|
| 5 | import org.apache.commons.collections4.list.SetUniqueList;
|
|---|
| 6 |
|
|---|
| 7 | import weka.core.Attribute;
|
|---|
| 8 | import weka.core.DenseInstance;
|
|---|
| 9 | import weka.core.Instance;
|
|---|
| 10 | import weka.core.Instances;
|
|---|
| 11 | import weka.experiment.Stats;
|
|---|
| 12 | import weka.filters.Filter;
|
|---|
| 13 | import weka.filters.unsupervised.attribute.Normalize;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Abstract class that implements the foundation of setwise data selection strategies using distributional characteristics.
|
|---|
| 17 | * This class provides the means to transform the data sets into their characteristic vectors.
|
|---|
| 18 | * @author Steffen Herbold
|
|---|
| 19 | */
|
|---|
| 20 | public abstract class AbstractCharacteristicSelection implements
|
|---|
| 21 | ISetWiseDataselectionStrategy {
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * vector with the distributional characteristics
|
|---|
| 25 | */
|
|---|
| 26 | private String[] characteristics = new String[]{"mean","stddev"};
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Sets the distributional characteristics. The names of the characteristics are separated by blanks.
|
|---|
| 30 | */
|
|---|
| 31 | @Override
|
|---|
| 32 | public void setParameter(String parameters) {
|
|---|
| 33 | if( !"".equals(parameters) ) {
|
|---|
| 34 | characteristics = parameters.split(" ");
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Transforms the data into the distributional characteristics. The first instance is the test data, followed by the training data.
|
|---|
| 40 | * @param testdata test data
|
|---|
| 41 | * @param traindataSet training data sets
|
|---|
| 42 | * @return distributional characteristics of the data
|
|---|
| 43 | */
|
|---|
| 44 | protected Instances characteristicInstances(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
|---|
| 45 | // setup weka Instances for clustering
|
|---|
| 46 | final ArrayList<Attribute> atts = new ArrayList<Attribute>();
|
|---|
| 47 |
|
|---|
| 48 | final Attribute classAtt = testdata.classAttribute();
|
|---|
| 49 | for( int i=0 ; i<testdata.numAttributes() ; i++ ) {
|
|---|
| 50 | Attribute dataAtt = testdata.attribute(i);
|
|---|
| 51 | if( !dataAtt.equals(classAtt) ) {
|
|---|
| 52 | for( String characteristic : characteristics ) {
|
|---|
| 53 | atts.add(new Attribute(dataAtt.name() + "_" + characteristic));
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | final Instances data = new Instances("distributional_characteristics", atts, 0);
|
|---|
| 58 |
|
|---|
| 59 | // setup data for clustering
|
|---|
| 60 | double[] instanceValues = new double[atts.size()];
|
|---|
| 61 | for( int i=0 ; i<testdata.numAttributes() ; i++ ) {
|
|---|
| 62 | Attribute dataAtt = testdata.attribute(i);
|
|---|
| 63 | if( !dataAtt.equals(classAtt) ) {
|
|---|
| 64 | Stats stats = testdata.attributeStats(i).numericStats;
|
|---|
| 65 | for( int j=0; j<characteristics.length; j++ ) {
|
|---|
| 66 | if( "mean".equals(characteristics[j]) ) {
|
|---|
| 67 | instanceValues[i*characteristics.length+j] = stats.mean;
|
|---|
| 68 | } else if( "stddev".equals(characteristics[j])) {
|
|---|
| 69 | instanceValues[i*characteristics.length+j] = stats.stdDev;
|
|---|
| 70 | } else if( "var".equals(characteristics[j])) {
|
|---|
| 71 | instanceValues[i*characteristics.length+j] = testdata.variance(j);
|
|---|
| 72 | } else {
|
|---|
| 73 | throw new RuntimeException("Unkown distributional characteristic: " + characteristics[j]);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | data.add(new DenseInstance(1.0, instanceValues));
|
|---|
| 79 |
|
|---|
| 80 | for( Instances traindata : traindataSet ) {
|
|---|
| 81 | instanceValues = new double[atts.size()];
|
|---|
| 82 | for( int i=0 ; i<traindata.numAttributes() ; i++ ) {
|
|---|
| 83 | Attribute dataAtt = traindata.attribute(i);
|
|---|
| 84 | if( !dataAtt.equals(classAtt) ) {
|
|---|
| 85 | Stats stats = traindata.attributeStats(i).numericStats;
|
|---|
| 86 | for( int j=0; j<characteristics.length; j++ ) {
|
|---|
| 87 | if( "mean".equals(characteristics[j]) ) {
|
|---|
| 88 | instanceValues[i*characteristics.length+j] = stats.mean;
|
|---|
| 89 | } else if( "stddev".equals(characteristics[j])) {
|
|---|
| 90 | instanceValues[i*characteristics.length+j] = stats.stdDev;
|
|---|
| 91 | } else if( "var".equals(characteristics[j])) {
|
|---|
| 92 | instanceValues[i*characteristics.length+j] = testdata.variance(j);
|
|---|
| 93 | } else {
|
|---|
| 94 | throw new RuntimeException("Unkown distributional characteristic: " + characteristics[j]);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 | Instance instance = new DenseInstance(1.0, instanceValues);
|
|---|
| 100 |
|
|---|
| 101 | data.add(instance);
|
|---|
| 102 | }
|
|---|
| 103 | return data;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Returns the normalized distributional characteristics of the training data.
|
|---|
| 108 | * @param testdata test data
|
|---|
| 109 | * @param traindataSet training data sets
|
|---|
| 110 | * @return normalized distributional characteristics of the data
|
|---|
| 111 | */
|
|---|
| 112 | protected Instances normalizedCharacteristicInstances(Instances testdata, SetUniqueList<Instances> traindataSet) {
|
|---|
| 113 | Instances data = characteristicInstances(testdata, traindataSet);
|
|---|
| 114 | try {
|
|---|
| 115 | final Normalize normalizer = new Normalize();
|
|---|
| 116 | normalizer.setInputFormat(data);
|
|---|
| 117 | data = Filter.useFilter(data, normalizer);
|
|---|
| 118 | } catch (Exception e) {
|
|---|
| 119 | throw new RuntimeException("Unexpected exception during normalization of distributional characteristics.", e);
|
|---|
| 120 | }
|
|---|
| 121 | return data;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|