package de.ugoe.cs.cpdp.training;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections4.list.SetUniqueList;
import org.apache.commons.io.output.NullOutputStream;
import weka.classifiers.AbstractClassifier;
import weka.classifiers.Classifier;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
/**
* Programmatic WekaBaggingTraining
*
* first parameter is Trainer Name.
* second parameter is class name
*
* all subsequent parameters are configuration params (for example for trees)
*
* XML Configurations for Weka Classifiers:
*
* {@code
*
*
*
* }
*
*
*/
public class WekaBaggingTraining2 extends WekaBaseTraining2 implements ISetWiseTrainingStrategy {
private final TraindatasetBagging classifier = new TraindatasetBagging();
@Override
public Classifier getClassifier() {
return classifier;
}
@Override
public void apply(SetUniqueList traindataSet) {
PrintStream errStr = System.err;
System.setErr(new PrintStream(new NullOutputStream()));
try {
classifier.buildClassifier(traindataSet);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
System.setErr(errStr);
}
}
public class TraindatasetBagging extends AbstractClassifier {
private static final long serialVersionUID = 1L;
private List trainingData = null;
private List classifiers = null;
@Override
public double classifyInstance(Instance instance) {
if( classifiers==null ) {
return 0.0;
}
double classification = 0.0;
for( int i=0 ; i attributeNames = new HashSet<>();
for( int j=0; j=0.5) ? 1.0 : 0.0;
}
public void buildClassifier(SetUniqueList traindataSet) throws Exception {
classifiers = new LinkedList<>();
trainingData = new LinkedList<>();
for( Instances traindata : traindataSet ) {
Classifier classifier = setupClassifier();
classifier.buildClassifier(traindata);
classifiers.add(classifier);
trainingData.add(new Instances(traindata));
}
}
@Override
public void buildClassifier(Instances traindata) throws Exception {
classifiers = new LinkedList<>();
trainingData = new LinkedList<>();
final Classifier classifier = setupClassifier();
classifier.buildClassifier(traindata);
classifiers.add(classifier);
trainingData.add(new Instances(traindata));
}
}
}