use of weka.core.Instances in project dkpro-tc by dkpro.
the class WekaDataWriter method writeClassifierFormat.
@Override
public void writeClassifierFormat(Collection<Instance> instances) throws AnalysisEngineProcessException {
try {
Instances masterInstance = initalConfiguration(instances);
for (Instance inst : instances) {
double[] featureValues = getFeatureValues(attributeStore, inst);
weka.core.Instance wekaInstance;
if (useSparse) {
wekaInstance = new SparseInstance(1.0, featureValues);
} else {
wekaInstance = new DenseInstance(1.0, featureValues);
}
wekaInstance.setDataset(masterInstance);
String outcome = inst.getOutcome();
if (isRegression) {
wekaInstance.setClassValue(Double.parseDouble(outcome));
} else {
wekaInstance.setClassValue(outcome);
}
Double instanceWeight = inst.getWeight();
if (applyWeighting) {
wekaInstance.setWeight(instanceWeight);
}
// preprocessingFilter.input(wekaInstance);
// saver.writeIncremental(preprocessingFilter.output());
saver.writeIncremental(wekaInstance);
}
} catch (Exception e) {
throw new AnalysisEngineProcessException(e);
}
}
use of weka.core.Instances in project dkpro-tc by dkpro.
the class WekaArffTest method main.
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File train = new File("src/main/resources/arff/manyInstances/train.arff.gz");
File test = new File("src/main/resources/arff/manyInstances/test.arff.gz");
Instances trainData = WekaUtils.getInstances(train, false);
Instances testData = WekaUtils.getInstances(test, false);
Classifier cl = new NaiveBayes();
// no problems until here
Evaluation eval = new Evaluation(trainData);
eval.evaluateModel(cl, testData);
}
use of weka.core.Instances in project dkpro-tc by dkpro.
the class WekaResultsTest method testWekaResultsRegression.
@Test
public void testWekaResultsRegression() throws Exception {
SMOreg cl = new SMOreg();
Instances trainData = WekaUtils.removeInstanceId(regressionTrainData, false);
Instances testData = WekaUtils.removeInstanceId(regressionTestData, false);
cl.buildClassifier(trainData);
Evaluation eval = WekaUtils.getEvaluationSinglelabel(cl, trainData, testData);
assertEquals(0.45, eval.correlationCoefficient(), 0.01);
}
use of weka.core.Instances in project dkpro-tc by dkpro.
the class WekaResultsTest method testWekaResultsMultiLabel.
@Test
public void testWekaResultsMultiLabel() throws Exception {
BR cl = new BR();
cl.setOptions(new String[] { "-W", J48.class.getName() });
Instances testData = WekaUtils.makeOutcomeClassesCompatible(multiLabelTrainData, multiLabelTestData, true);
Instances trainData = WekaUtils.removeInstanceId(multiLabelTrainData, true);
testData = WekaUtils.removeInstanceId(testData, true);
cl.buildClassifier(trainData);
Result eval = WekaUtils.getEvaluationMultilabel(cl, trainData, testData, "0.2");
assertEquals(16.0, eval.L, 0.01);
assertEquals(0.0, (Double) Result.getStats(eval, "1").get("Exact match"), 0.01);
}
use of weka.core.Instances in project dkpro-tc by dkpro.
the class WekaUtilTest method tcInstanceToMekaInstanceTest.
@Test
public void tcInstanceToMekaInstanceTest() throws Exception {
List<String> outcomeValues = Arrays.asList(new String[] { "outc_1", "outc_2", "outc_3" });
Instance i1 = new Instance();
i1.addFeature(new Feature("feature1", 2, FeatureType.NUMERIC));
i1.addFeature(new Feature("feature2", 2, FeatureType.NUMERIC));
i1.addFeature(new Feature("feature3_{{", "a", FeatureType.STRING));
Instance i2 = new Instance();
i2.addFeature(new Feature("feature1", 1, FeatureType.NUMERIC));
i2.addFeature(new Feature("feature4", "val_1", FeatureType.STRING));
i2.addFeature(new Feature("feature3_{{", "b", FeatureType.STRING));
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("outc_1", Arrays.asList(new String[] { "0", "1" })));
attributes.add(new Attribute("outc_2", Arrays.asList(new String[] { "0", "1" })));
attributes.add(new Attribute("outc_3", Arrays.asList(new String[] { "0", "1" })));
attributes.add(new Attribute("feature5"));
attributes.add(new Attribute("feature2"));
attributes.add(new Attribute("feature4", Arrays.asList(new String[] { "val_1", "val_2" })));
attributes.add(new Attribute("feature1"));
Instances trainingData = new Instances("test", attributes, 0);
weka.core.Instance wekaInstance1 = WekaUtils.tcInstanceToMekaInstance(i1, trainingData, outcomeValues);
weka.core.Instance wekaInstance2 = WekaUtils.tcInstanceToMekaInstance(i2, trainingData, outcomeValues);
assertEquals(true, wekaInstance1.equalHeaders(wekaInstance2));
assertEquals(7, wekaInstance1.numAttributes());
wekaInstance1.dataset().add(wekaInstance1);
wekaInstance2.dataset().add(wekaInstance2);
System.out.println(wekaInstance1.dataset() + "\n");
System.out.println(wekaInstance2.dataset() + "\n");
}
Aggregations