-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPy_Weka_Subprocess.py
78 lines (67 loc) · 2.5 KB
/
Py_Weka_Subprocess.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import sys
import subprocess
def GetFilenames(directory):
for root, dirs, files in os.walk(directory):
CSV_files = []
for filename in files:
if filename.endswith(('.csv')):
CSV_files.append(filename)
return CSV_files
def Run_J48(directory, filelist):
for name in filelist:
fid = str(name)
fpth = str(name[0:-4]) + '_J48'
pth = directory + fid
WEKA = ["java", "-Xmx4G", "weka.classifiers.trees.J48",
"-t", pth, "-d", fpth + ".model"]
process = subprocess.Popen(WEKA, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
f = open(directory + fpth + '.txt', 'w')
f.write(stdout)
f.close()
return stdout, stderr
def Run_RandomTree(directory, filelist):
for name in filelist:
fid = str(name)
fpth = str(name[0:-4]) + '_J48graft'
pth = directory + fid
WEKA = ["java", "-Xmx4G", "weka.classifiers.trees.RandomTree",
"-t", pth, "-d", fpth + ".model"]
process = subprocess.Popen(WEKA, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
f = open(directory + fpth + '.txt', 'w')
f.write(stdout)
f.close()
return stdout, stderr
def Run_RandomForest(directory, filelist):
for name in filelist:
fid = str(name)
fpth = str(name[0:-4]) + '_RF'
pth = directory + fid
WEKA = ["java", "-Xmx4G", "weka.classifiers.trees.RandomForest",
"-t", pth, "-d", fpth + ".model"]
process = subprocess.Popen(WEKA, stdout=subprocess.PIPE)
stdout, stderr = process.communicate()
f = open(directory + fpth + '.txt', 'w')
f.write(stdout)
f.close()
return stdout, stderr
if __name__ == '__main__':
print """
This program uses the subprocess module to run Weka classifiers, namely
J48, RandomTree and RandomForest. The classification results and the
resulting models are saved to the disk. The program runs classifiers on
all the .csv files present in the current directory.
Important: Please export the weka classpath before usage.
TRYING TO PERFORM CLASSIFICATION NOW..."""
directory = os.getcwd() + '/'
CSVList = GetFilenames(directory)
Run_J48(directory, CSVList)
Run_RandomTree(directory, CSVList)
Run_RandomForest(directory, CSVList)
print "=" * 100
print "Performed Classification on: -"
for item in CSVList:
print item
sys.exit()