有什么方法可以在 python 上使用 PYWEKA 上的 ADTrees?

Is there any way to use ADTrees on python with PYWEKA?

大家好,我有这个问题,因为我正在尝试使用 pyweka 在 python 上使用 ADTrees of weka,如下所示:

cls = Classifier(classname="weka.classifiers.trees.adtree", options=["-B", "10", "-E", "-3", "-S", "1"])
cls.build_classifier(data_modelos_1_2_csv)

但它遇到了这个错误:

Failed to instantiate weka.classifiers.trees.adtree: weka.classifiers.trees.adtree

Exception in thread "Thread-0" java.lang.ClassNotFoundException: weka.classifiers.trees.adtree
    java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    java.base/java.lang.Class.forName0(Native Method)
    java.base/java.lang.Class.forName(Class.java:398)
    weka.core.WekaPackageClassLoaderManager.forName(WekaPackageClassLoaderManager.java:198)
    weka.core.WekaPackageClassLoaderManager.forName(WekaPackageClassLoaderManager.java:178)
    weka.core.ResourceUtils.forName(ResourceUtils.java:80)
    weka.core.Utils.forName(Utils.java:1112)

    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at weka.core.WekaPackageClassLoaderManager.forName(WekaPackageClassLoaderManager.java:198)
    at weka.core.WekaPackageClassLoaderManager.forName(WekaPackageClassLoaderManager.java:178)
    at weka.core.ResourceUtils.forName(ResourceUtils.java:80)
    at weka.core.Utils.forName(Utils.java:1112)

这是追溯:

AssertionError                            Traceback (most recent call last)
/tmp/ipykernel_6468/746392926.py in <module>
      1 #from weka.classifiers import alternatingDecisionTrees
      2 
----> 3 cls = Classifier(classname="weka.classifiers.trees.adtree", options=["-B", "10", "-E", "-3", "-S", "1"])
      4 cls.build_classifier(data_modelos_1_2_csv)

/usr/local/lib/python3.8/dist-packages/weka/classifiers.py in __init__(self, classname, jobject, options)
     56         if jobject is None:
     57             jobject = Classifier.new_instance(classname)
---> 58         self.enforce_type(jobject, "weka.classifiers.Classifier")
     59         self.is_updateable = self.check_type(jobject, "weka.classifiers.UpdateableClassifier")
     60         self.is_drawable = self.check_type(jobject, "weka.core.Drawable")

/usr/local/lib/python3.8/dist-packages/weka/core/classes.py in enforce_type(cls, jobject, intf_or_class)
    867         :type intf_or_class: str
    868         """
--> 869         if not cls.check_type(jobject, intf_or_class):
    870             raise TypeError("Object does not implement or subclass " + intf_or_class + ": " + get_classname(jobject))
    871 

/usr/local/lib/python3.8/dist-packages/weka/core/classes.py in check_type(cls, jobject, intf_or_class)
    855         :rtype: bool
    856         """
--> 857         return is_instance_of(jobject, intf_or_class)
    858 
    859     @classmethod

/usr/local/lib/python3.8/dist-packages/weka/core/classes.py in is_instance_of(obj, class_or_intf_name)
    283     classname = get_classname(obj)
    284     # array? retrieve component type and check that
--> 285     if is_array(obj):
    286         jarray = JavaArray(jobject=obj)
    287         classname = jarray.component_type()

/usr/local/lib/python3.8/dist-packages/weka/core/classes.py in is_array(obj)
    307     :rtype: bool
    308     """
--> 309     cls = javabridge.call(obj, "getClass", "()Ljava/lang/Class;")
    310     return javabridge.call(cls, "isArray", "()Z")
    311 

~/.local/lib/python3.8/site-packages/javabridge/jutil.py in call(o, method_name, sig, *args)
    886     '''
    887     env = get_env()
--> 888     fn = make_call(o, method_name, sig)
    889     args_sig = split_sig(sig[1:sig.find(')')])
    890     ret_sig = sig[sig.find(')')+1:]

~/.local/lib/python3.8/site-packages/javabridge/jutil.py in make_call(o, method_name, sig)
    834 
    835     '''
--> 836     assert o is not None
    837     env = get_env()
    838     if isinstance(o, basestring):

AssertionError: 

所以我不知道是否存在在 Python.

上使用此 ADTrees 的方法

我尝试使用“weka.classifiers.trees.adtree”、“weka.classifiers.trees.ADTree”和导入

from weka.classifiers import alternatingDecisionTrees
from weka.classifiers import ADTree
from weka.classifiers import ADTrees
from weka.classifiers import adtree
from weka.classifiers import adtrees

不知道还能做什么。

python-weka-wrapper3,顾名思义,是 Weka classes 的 light-weight 包装器。然而,它主要只包含抽象的超级classes,而不是 all 构成 Weka 的数千个 classes。

weka.classifiers.Classifier class 是用于 class 化器的包装器,您需要指定实际 [=] 的 Java class 名称31=] 您要包装的标识符(在您的情况下 weka.classifiers.trees.ADTree)。而且,是的,就像 Python 一样,Java 也是区分大小写的。

此外,如果您需要来自包的 classes,那么您需要启动 JVM with package support(默认不支持包)。

下面是一个使用默认参数输出 ADTree 的 command-line 的例子。如有必要,它会先安装包。

import sys
import weka.core.jvm as jvm
from weka.core.packages import is_installed, install_package
from weka.classifiers import Classifier

jvm.start(packages=True)

pkgname = "alternatingDecisionTrees"
if not is_installed(pkgname):
    print("Package %s not installed, attempting installation..." % pkgname)
    if install_package(pkgname):
        print("Package %s installed, please rerun script!" % pkgname)
    else:
        print("Failed to install package %s!" % pkgname)
    jvm.stop()
    sys.exit(1)

cls = Classifier(classname="weka.classifiers.trees.ADTree", options=[])
print(cls.to_commandline())

jvm.stop()