简体   繁体   中英

Using packages in PyWeka(Python Weka Wrapper)

I need some help with using installed packages in PyWeka. I am able to install packages, but I am unable to use them or find where they are installed. When I try to find the full classname, i get an exception(which occurs when there is no matching module)

Example:

import weka.core.classes as core
core.complete_classname("J48")

Output

  'weka.classifiers.trees.J48'

I am trying to install the DMNBtext package. Installation occurs but module cannot be found

import weka.core.classes as core
print(packages.is_installed("DMNBtext"))
core.complete_classname("DMNBtext")

Output

True
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-25-7ea05097d6f1> in <module>()
      1 import weka.core.classes as core
      2 print(packages.is_installed("DMNBtext"))
----> 3 core.complete_classname("DMNBtext")

/usr/local/lib/python3.6/dist-packages/weka/core/classes.py in complete_classname(classname)
   1725         return str(result[0])
   1726     elif len(result) == 0:
-> 1727         raise Exception("No classname matches found for: " + classname)
   1728     else:
   1729         matches = []

Exception: No classname matches found for: DMNBtext

Please note that is_installed gives True output, meaning the package is installed.

Any idea how I can resolve this ? Also,my jvm was started with packages=True, so that should not be a problem. Thanks in advance.

I just created a new virtual environment with python-weka-wrapper3 :

virtualenv -p /usr/bin/python3.6 pww3
./pww3/bin/pip install numpy matplotlib pygraphviz javabridge python-weka-wrapper3

And then ran the following script successfully (needs to be run twice, if the DMNBtext package is not yet installed):

import sys
import weka.core.jvm as jvm
import weka.core.packages as packages
from weka.core.classes import complete_classname

jvm.start(packages=True)

pkg = "DMNBtext"

# install package if necessary
if not packages.is_installed(pkg):
    print("Installing %s..." % pkg)
    packages.install_package(pkg)
    print("Installed %s, please re-run script!" % pkg)
    jvm.stop()
    sys.exit(0)

# testing classname completion
print(complete_classname(".J48"))
print(complete_classname(".DMNBtext"))

jvm.stop()

Once the DMNBtext package is installed, the script outputs this:

weka.classifiers.trees.J48
weka.classifiers.bayes.DMNBtext

I am going to assume you already have the pyWeka installed and running on Anaconda. The details of how to do this are here .

You should start activating the java virtual machine

import weka.core.jvm as jvm
import weka.core.classes as core
jvm.start(packages=True)    # needed for package manipulation
from weka.classifiers import Classifier # you are going to classify something

import weka.core.packages as packages # so you can install packages
packages.install_package('DNMNBtext') # if it is not already installed. 
                                      # You can also install it on Weka

And now you can issue:

core.complete_classname("DMNBtext")

to find the name of the class

'weka.classifiers.bayes.DMNBtext' 

Finally

dmnb = Classifier(classname="weka.classifiers.bayes.DMNBtext")
dmnb.options=[list of options]

Take care

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM