-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python_Weka_Wrapper_Feature_Selection.py
43 lines (36 loc) · 1.47 KB
/
Python_Weka_Wrapper_Feature_Selection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import sys
import weka.core.jvm as jvm
from weka.core.converters import Loader
from weka.filters import Filter
from weka.attribute_selection import ASSearch, ASEvaluation, AttributeSelection
def Feature_Selection(infile):
directory = os.getcwd() + '/'
csvpath = directory + infile
jvm.start(packages=True, max_heap_size="4g")
print "\n\n"
print "Loaded file: ", infile
csvloader = Loader(classname="weka.core.converters.CSVLoader")
csvdata = csvloader.load_file(csvpath)
remover = Filter(classname="weka.filters.unsupervised.attribute.Remove", options=["-R", " 1"])
remover.inputformat(csvdata)
filtered_data = remover.filter(csvdata)
filtered_data.class_is_last()
search = ASSearch(classname="weka.attributeSelection.BestFirst", options=["-D", "1", "-N", "5"])
evaluator = ASEvaluation(classname="weka.attributeSelection.CfsSubsetEval", options=["-P", "1", "-E", "1"])
attribs = AttributeSelection()
attribs.search(search)
attribs.evaluator(evaluator)
attribs.select_attributes(filtered_data)
print "Summary of Attribute Selection: "
print attribs.results_string
jvm.stop()
return
if len(sys.argv) != 2:
print """Performing Classification using Python Weka Wrapper:
http://pythonhosted.org/python-weka-wrapper/index.html#
Usage: Provide the csv filename as the argument.
The csv file should be present in the working directory."""
sys.exit()
else:
Feature_Selection(sys.argv[1])