WEKA J48 Classifier Example Using Jython

Monday, 03 November 2008


"""
Jython-WEKA data mining example
Author: Pradeep Kishore Gowda
This Program demonstrates the use of Simple Classification
Algorithm (Using j48 decision Tree) on Iris Data set
"""
import java.io.FileReader as FR
import weka.core.Instances as Instances
import weka.classifiers.trees.J48 as J48
# Load Data File
file = FR('iris.arff')
data = Instances(file)
"""
The sample data reads like this
@relation iris
@attribute sepallength numeric
@attribute sepalwidth numeric
@attribute petallength numeric
@attribute petalwidth numeric
@attribute class {Iris-setosa,Iris-versicolor,Iris-virginica}
@data
5.1,3.5,1.4,0.2,Iris-setosa
"""
#set the class Index - the index of the dependent variable
data.setClassIndex(4)
#create the model
j48 = J48()
j48.buildClassifier(data)
#print out the built model
print j48
# you do not have to call the j48.toString(),
# My guess is Python's default __str__ method calls
#.toString Internally. ?
# let us classify an instance and verify the result
myinstance = data.instance(0)
klass = j48.classifyInstance(myinstance)
#0.0
#what does 0.0 mean?
print data.attribute(data.classIndex()).value(klass)
#'Iris-setosa'

Update: This thread on wekalist gives a similar example.