use of weka.core.SelectedTag in project dkpro-tc by dkpro.
the class WekaUtils method getPredictionInstancesSingleLabel.
/**
* Generates an instances object containing the predictions of a given single-label classifier
* for a given test set
*
* @param testData
* weka instances
* @param cl
* classifier
* @return weka instances
* @throws Exception
* in case of errors
*/
public static Instances getPredictionInstancesSingleLabel(Instances testData, Classifier cl) throws Exception {
StringBuffer classVals = new StringBuffer();
for (int i = 0; i < testData.classAttribute().numValues(); i++) {
if (classVals.length() > 0) {
classVals.append(",");
}
classVals.append(testData.classAttribute().value(i));
}
// get predictions
List<Double> labelPredictionList = new ArrayList<Double>();
for (int i = 0; i < testData.size(); i++) {
labelPredictionList.add(cl.classifyInstance(testData.instance(i)));
}
// add an attribute with the predicted values at the end off the attributes
Add filter = new Add();
filter.setAttributeName(WekaTestTask.PREDICTION_CLASS_LABEL_NAME);
if (classVals.length() > 0) {
filter.setAttributeType(new SelectedTag(Attribute.NOMINAL, Add.TAGS_TYPE));
filter.setNominalLabels(classVals.toString());
}
filter.setInputFormat(testData);
testData = Filter.useFilter(testData, filter);
// fill predicted values for each instance
for (int i = 0; i < labelPredictionList.size(); i++) {
testData.instance(i).setValue(testData.classIndex() + 1, labelPredictionList.get(i));
}
return testData;
}
use of weka.core.SelectedTag in project dkpro-tc by dkpro.
the class WekaUtils method addInstanceId.
/**
* Copies the instanceId attribute and its values from an existing data set, iff present. It
* will be indexed right before the class attribute
*
* @param newData
* data set without instanceId attribute
* @param oldData
* data set with or without instanceId attribute
* @param isMultilabel
* is multi label processing
* @return a data set with or without instanceId attribute
* @throws Exception
* an exception
*/
public static Instances addInstanceId(Instances newData, Instances oldData, boolean isMultilabel) throws Exception {
Instances filteredData;
if (oldData.attribute(Constants.ID_FEATURE_NAME) != null) {
int instanceIdOffset = oldData.attribute(Constants.ID_FEATURE_NAME).index();
Add add = new Add();
add.setAttributeName(Constants.ID_FEATURE_NAME);
// for single-label
if (isMultilabel) {
add.setAttributeIndex("last");
} else {
add.setAttributeIndex("first");
}
add.setAttributeType(new SelectedTag(Attribute.STRING, Add.TAGS_TYPE));
add.setInputFormat(newData);
filteredData = Filter.useFilter(newData, add);
int j = isMultilabel ? filteredData.numAttributes() - 1 : 0;
for (int i = 0; i < filteredData.numInstances(); i++) {
String outcomeId = oldData.instance(i).stringValue(instanceIdOffset);
filteredData.instance(i).setValue(j, outcomeId);
}
} else {
filteredData = new Instances(newData);
}
return filteredData;
}
Aggregations