python-weka-wrapper:找不到ChiSquaredAttributeEval类

时间:2017-02-13 08:53:17

标签: python weka

我正在使用Python中的Weka。我想使用weka.attributeSelection.ChiSquaredAttributeEval进行属性选择。但是,我反复得到这个错误:

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)

我可以使用另一种属性选择方法weka.attributeSelection.CfsSubsetEval。任何人都可以告诉我现在应该做些什么才能让ChiSquaredAttributeEval工作?

(我在某处读到我应该手动设置class_path的{​​{1}},就像在jvm中一样,但它没有帮助。)

我使用的是Python 2.7.13,python-weka-wrapper 0.3.10,Java 1.7.0。

1 个答案:

答案 0 :(得分:1)

ChiSquaredAttributeEval属性评估不是核心Weka的一部分,而是单独的package。因此,您需要使用jvm.start(packages=True)(来自API文档)包含的软件包启动JVM。

这是一个有效的例子:

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()