Skip to content

Using weka via jepp

Jepp embeds CPython in Java. It is safe to use in a heavily threaded environment, it is quite fast and its stability is a main feature and goal.

--taken from the Jepp homepage

Prerequisites#

  • Java 6 (jepp makes use of the javax.script package)
  • Jepp 2.2 or higher
  • suggested fix for the missing sys.argv problem

Limitations#

Jepp doesn't seem to be able to import third-party libraries like scipy, numpy or wx (pure Python modules can be imported, though).

Accessing Weka classes within Jepp#

Java classes are imported in one's Python script as follows:

 from <package> import <class>
E.g., importing J48 looks like this:

 from weka.classifiers.trees import J48
In the following a little example script for loading a dataset, cross-validating J48 with it and outputting the results of the cross-validation in the console:

 # import classes
 from weka.core import Instances
 from weka.classifiers import Evaluation
 from weka.classifiers.trees import J48

 from java.io import BufferedReader
 from java.io import FileReader
 from java.util import Random

 # load data
 reader = BufferedReader(FileReader('/some/where/file.arff'))
 data   = Instances(reader)
 data.setClassIndex(data.numAttributes() - 1)
 reader.close()

 # train classifier
 j48  = J48()
 eval = Evaluation(data)
 rand = Random(1)
 eval.crossValidateModel(j48, data, 10, rand)

 # output summary
 print eval.toSummaryString()
The script can be started like this (you will have to adjust the paths for the jars and the Python script):

 java -classpath jep.jar:weka.jar some_script.py

See also#