Search code examples
javapythonscikit-learnwrapperweka

sklearn-weka-plugin return JavaException


I am using the sklearn-weka-plugin and want to run the JRip classifier. When I try to run it with the python-weka-wrapper:

import weka.core.jvm as jvm
from weka.classifiers import Classifier

jvm.start()

jrip = Classifier("weka.classifiers.rules.JRip")

jvm.stop()

everything works fine, but if I try to do the same with the sklearn-weka-plugin:

import sklweka.jvm as jvm
from sklweka.classifiers import WekaEstimator

jvm.start()

jrip = WekaEstimator("weka.classifiers.rules.JRip")

jvm.stop()

I get the following error message:

Failed to get class weka.classifiers.rules.JRip
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: weka.classifiers.rules.JRip
Traceback (most recent call last):
  File "sklearn_weka_test.py", line 21, in <module>
    jrip = WekaEstimator("weka.classifiers.rules.JRip")
  File "/home/andreas/.local/lib/python3.8/site-packages/sklweka/classifiers.py", line 45, in __init__
    if not is_instance_of(_jobject, "weka.classifiers.Classifier"):
  File "/home/andreas/.local/lib/python3.8/site-packages/weka/core/classes.py", line 285, in is_instance_of
    if is_array(obj):
  File "/home/andreas/.local/lib/python3.8/site-packages/weka/core/classes.py", line 309, in is_array
    cls = javabridge.call(obj, "getClass", "()Ljava/lang/Class;")
  File "/home/andreas/.local/lib/python3.8/site-packages/javabridge/jutil.py", line 888, in call
    fn = make_call(o, method_name, sig)
  File "/home/andreas/.local/lib/python3.8/site-packages/javabridge/jutil.py", line 846, in make_call
    raise JavaException(jexception)
javabridge.jutil.JavaException: weka.classifiers.rules.JRip

Solution

  • In both cases, you should use classname= as all parameters of these classes are optional. The difference is that the first parameter of Classifier is classname and for WekaEstimator it is the JavaBridge object. That is why it works in the first case, but fails with an Exception in the second case.

    Your code should look like this:

    import weka.core.jvm as jvm
    from weka.classifiers import Classifier
    
    jvm.start()
    
    jrip = Classifier(classname="weka.classifiers.rules.JRip")
    
    jvm.stop()
    

    And:

    import sklweka.jvm as jvm
    from sklweka.classifiers import WekaEstimator
    
    jvm.start()
    
    jrip = WekaEstimator(classname="weka.classifiers.rules.JRip")
    
    jvm.stop()