简体   繁体   中英

J48 Classifier - python-weka-wrapper — selecting Class attribute

I am trying to apply the j48 classifier on a dataset, but I am not understanding how to actually select the right attribute as the class. I found a method called class_is_last() which sets the last attribute as the class, but what if I want to select my attribute called say 'name' as the class?

jvm.start()
loader = Loader(classname="weka.core.converters.ArffLoader")
dataweka = loader.load_file("enumeratedremovedtest-arff.arff")
dataarff = arff.load(open('enumeratedremovedtest-arff.arff', 'rb'))

from weka.classifiers import Classifier
dataweka.class_is_last()
cls = Classifier(classname="weka.classifiers.trees.J48", options=["-C","0.25", "-B", "-M", "2"])
print dataweka.class_attribute
cls.build_classifier(dataweka)

print(cls)

jvm.stop()

All required modules have been imported, and this works as expected and uses the last attribute in the list as the class.

Your question is how to select class index in Weka. First off, Let's see what class index is in here . My suggestion is to use function setClassIndex(int)

Let's imagine that attribute "name" is in column 5 of your dataset, you can set the class as:

label = 5    
dataweka.class_index = label - 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM