48 lines
1.7 KiB
Java
48 lines
1.7 KiB
Java
|
|
package edu.nju.ics.frontier.learning;
|
||
|
|
|
||
|
|
import edu.nju.ics.frontier.util.Assertion;
|
||
|
|
import weka.classifiers.Classifier;
|
||
|
|
import weka.classifiers.trees.RandomForest;
|
||
|
|
import weka.core.Instance;
|
||
|
|
import weka.core.Instances;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
public class RFModel extends Model {
|
||
|
|
public RFModel(String name) {
|
||
|
|
super(name);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Classifier fit(TimeDatabase trainDb) throws Exception {
|
||
|
|
Instances instances = trainDb.getLabeledInstances();
|
||
|
|
if (instances == null) {
|
||
|
|
throw new NullPointerException("No labeled data in the training set!");
|
||
|
|
}
|
||
|
|
RandomForest metaClassifier = new RandomForest();
|
||
|
|
metaClassifier.buildClassifier(instances);
|
||
|
|
return metaClassifier;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Map<String, List<int[]>> predict(Classifier classifier, TimeDatabase trainDb, TimeDatabase testDb, ModelPerformanceSampler sampler) throws Exception {
|
||
|
|
Map<String, List<int[]>> confusionMatrices = new HashMap<String, List<int[]>>();
|
||
|
|
testDb.getLabeledAndUnlabeledInstances();
|
||
|
|
for (TimeSeries ts : testDb.getTimeSeries()) {
|
||
|
|
for (TimePoint tp : ts.getTimePoints()) {
|
||
|
|
if (!tp.isEmpty()) {
|
||
|
|
Instance instance = tp.getInstance();
|
||
|
|
Assertion.assertNotNull(instance);
|
||
|
|
double[] probDist = classifier.distributionForInstance(instance);
|
||
|
|
tp.setFinalProbDist(probDist);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
confusionMatrices.put("final", sampler.collectTrueAndPredLabels("final", testDb));
|
||
|
|
return confusionMatrices;
|
||
|
|
}
|
||
|
|
}
|