0
votes

I want to save the classifier errors to arff file using weka's python3 wrapper. To accomplish this using the weka GUI, I first run any classifier, then right-click the item from the result list and click "Visualize classifier errors". This opens a window (refer image). Then to save the arff file I just click the save button. Since I need to do this for many files and for different classifiers, I can't do this manually.

Hence, I wrote the following code:

from weka.core.converters import Loader
from weka.classifiers import FilteredClassifier, Classifier
from weka.filters import Filter
from weka.classifiers import Evaluation
from weka.core.classes import Random

PATH = "file.arff"

loader = Loader(classname="weka.core.converters.ArffLoader")
data = loader.load_file(PATH)
data.class_is_last()

remove = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", "1"])
cls = Classifier(classname="weka.classifiers.trees.J48", options=["-C", "0.25", "-M", "2"])

fc = FilteredClassifier()
fc.filter = remove
fc.classifier = cls

evl = Evaluation(data)
evl.crossvalidate_model(classifier=fc, data=data, num_folds=10, rnd=Random(1))

But, how will I save the classifier errors using weka's python3 wrapper?

1

1 Answers

0
votes

I solved the problem by adding an attribute to the data model and modifying the value for each instance as per prediction.

...

data.no_class()
data.insert_attribute(
    att=Attribute.create_nominal(name='Predicted', labels=['True', 'False']),
    index=data.num_attributes
)

for index, pred in enumerate(evl.predictions):
    tmp = 'True' if pred.predicted else 'False'
    inst = data.get_instance(index)
    inst.set_string_value(data.num_attributes - 1, tmp)

with open("results.arff"), 'w') as fp:
    fp.write(str(data))

One can also use inst.set_value() if the variable if not string.