Is there a way to directly make use of SMOTE to a xxx.csv in python-weka-wrapper3

Viewed 39

For example,I have a data-set,which has 18 instances(class value:True:False=12:6),and it has 11 attributes.Can just make use SMOTE in python like in weka? How can I make it by code?

enter image description here

1 Answers

Have you looked at the API examples and the example repository? These resources explain most if not all of the available functionality.

Here is an example of loading a CSV file and applying the SMOTE filter:

import weka.core.jvm as jvm
from weka.core.classes import from_commandline
from weka.core.packages import install_missing_package, installed_package
from weka.core.converters import load_any_file

jvm.start(packages=True)

# install SMOTE if necessary
installed = installed_package("SMOTE")
if not installed:
    success, restart = install_missing_package("SMOTE")
    if restart:
        print("Please rerun script")
        jvm.stop()
        import sys
        sys.exit(0)

# load data
data = load_any_file("/some/where/iris.csv", class_index="last")
# if the default parameters for loading the CSV file don't work, 
# you need to configure the CSVLoader yourself and set the 
# appropriate options (or even use a 3rd-party package):
# from weka.core.converters import Loader
# loader = Loader(classname="weka.core.converters.CSVLoader", options=[])
# data = loader.load_file("/some/where/iris.csv", class_index="last")
print(data.num_instances)

# apply SMOTE
# replace the command-line with the one that you can copy/paste from 
# the Weka Explorer via right-click menu
smote = from_commandline("weka.filters.supervised.instance.SMOTE -C 0 -K 5 -P 100.0 -S 1", classname="weka.filters.Filter")
smote.inputformat(data)
filtered = smote.filter(data)
print(filtered.num_instances)

jvm.stop()
Related