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}
#set the class Index - the index of the dependent variable
#create the model
j48 = J48()
#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)
#what does 0.0 mean?
print data.attribute(data.classIndex()).value(klass)

Update: This thread on wekalist gives a similar example.