WEKA J48 Classifier Example Using Jython

"""
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

  1. 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

  1. you do not have to call the j48.toString(),
  2. My guess is Python’s default str method calls
  3. .toString Internally. ?
  4. 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.

See Also

JythonPython

© 2008 Pradeep Kishore Gowda (C-C by SA 2.5-IN) ◊ Made with python, YUI on a mac.