hsiaomichiu
hsiaomichiu

Reputation: 602

python-weka-wrapper: ChiSquaredAttributeEval class not found

I am working with Weka in Python. I would like to use weka.attributeSelection.ChiSquaredAttributeEval for attribute selection. However, I repeatedly get this error:

Failed to get class weka/attributeSelection/ChiSquaredAttributeEval
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: weka/attributeSelection/ChiSquaredAttributeEval
Caused by: java.lang.ClassNotFoundException: weka.attributeSelection.ChiSquaredAttributeEval
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

I am able to use another attribute selection method though, weka.attributeSelection.CfsSubsetEval. Can anyone enlighten me what I should do now in order to get ChiSquaredAttributeEval working?

(I read somewhere that I should manually set the class_path of the jvm, as in jvm.start(class_path=mypath), but it didn't help.)

I am using Python 2.7.13, python-weka-wrapper 0.3.10, Java 1.7.0.

Upvotes: 0

Views: 499

Answers (1)

fracpete
fracpete

Reputation: 2608

The ChiSquaredAttributeEval attribute evaluation is not part of core Weka, but a separate package. Therefore you need to start up the JVM with packages included using jvm.start(packages=True) (from API documentation).

Here is a working example:

import sys
import weka.core.jvm as jvm
import weka.core.packages as pkg
from weka.core.converters import Loader
from weka.attribute_selection import ASSearch
from weka.attribute_selection import ASEvaluation
from weka.attribute_selection import AttributeSelection

# start JVM with packages
jvm.start(packages=True)

# package installed?
chisq_name = "chiSquaredAttributeEval"
chisq_installed = False
for p in pkg.installed_packages():
    if p.name == chisq_name:
        chisq_installed = True
if not chisq_installed:
    pkg.install_package(chisq_name)
    print("pkg %s installed, please restart" % chisq_name)
    jvm.stop()
    sys.exit(1)

# load a dataset
data_file = "/adjust/the/path/anneal.arff"
print("Loading dataset: " + data_file)
loader = Loader("weka.core.converters.ArffLoader")
anneal_data = loader.load_file(data_file)
anneal_data.class_is_last()

# perform attribute selection
search = ASSearch(classname="weka.attributeSelection.Ranker", options=["-N", "-1"])
evaluation = ASEvaluation(classname="weka.attributeSelection.ChiSquaredAttributeEval")
attsel = AttributeSelection()
attsel.search(search)
attsel.evaluator(evaluation)
attsel.select_attributes(anneal_data)
print("# attributes: " + str(attsel.number_attributes_selected))
print("attributes: " + str(attsel.selected_attributes))
print("result string:\n" + attsel.results_string)

jvm.stop()

Upvotes: 1

Related Questions