-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass_predict_updated.py
More file actions
203 lines (156 loc) · 5.11 KB
/
Copy pathclass_predict_updated.py
File metadata and controls
203 lines (156 loc) · 5.11 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import itertools
import numpy as np
import re
import csv
from sklearn.svm import SVC
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
DATA_FILE = 'data_svm_org_new_v2.csv'
TRAINING_FILE_HAS_HEADER = True
TESTING_FILE_HAS_HEADER = False
# METHOD INDICES
NOISY = 'unprocessed'
KLT = 'klt'
KLT_JABLOUN = 'klt_jabloun'
STSA = 'stsa'
LOGTSA = 'logstsa'
LOGSTA_NEST = 'logstsa_nest'
LOGSTA_SAP_Q = 'logstsa_sap_q'
WEUCLID = 'weuclid'
RDC = 'rdc'
RDC_NEST = 'rdc_nest'
MB = 'mb'
WAVTHRE = 'wavthre'
SCALART = 'scalart'
TSOUKALAS = 'tsoukalas'
# Samples Names
SAMPLE_NAMES = ['sp01', 'sp02', 'sp03', 'sp04', 'sp06', 'sp07', 'sp08', 'sp09', 'sp11', 'sp12', 'sp13', 'sp14', 'sp16', 'sp17', 'sp18', 'sp19']
CLASS_LABEL = {}
class_index = -1
# Function definition
def getIndex (s):
global class_index
if s not in CLASS_LABEL.keys():
class_index = class_index + 1
CLASS_LABEL[s] = class_index
return CLASS_LABEL[s]
def splitData (dataFile, trainFile, testFile, testSamples):
train = []
test = []
for line in csv.reader((open(dataFile))):
matchedTestSample = False
for t in testSamples:
if line[0].find(t) != -1:
matchedTestSample = True
test.append(line)
break
if matchedTestSample == False:
train.append(line)
csv.writer(open(trainFile, 'w', newline='')).writerows(train)
csv.writer(open(testFile, 'w', newline='')).writerows(test)
def getSample (str):
smpl = re.findall(r'\w+|\W+', str)[0]
return smpl.split('_')[0]
def getClass (str):
fileName = re.findall(r'\w+|\W+', str)[0]
return fileName.split('_')[1] + '_' + fileName.split('_')[2]
def getMethod (str):
mthd = re.findall(r'\w+|\W+', str)[0]
mthd = '_'.join(mthd.split('_')[3:])
if '' == mthd:
mthd = NOISY
return mthd
def process_data (file, has_header):
# Read training data and create training set
training_raw = {}
for line in csv.reader((open(file))):
if has_header:
has_header = False
continue
sample = getSample(line[0])
cls = getClass(line[0])
mthd = getMethod(line[0])
if cls not in training_raw.keys():
tmp_mthd = {}
tmp_smpl = {}
tmp_mthd[mthd] = line[1]
tmp_smpl[sample] = tmp_mthd
training_raw[cls] = tmp_smpl
elif sample not in training_raw[cls].keys():
tmp_mthd = {}
tmp_mthd[mthd] = line[1]
training_raw[cls][sample] = tmp_mthd
else:
training_raw[cls][sample][mthd] = line[1]
training_X = []
training_Y = []
for cls in training_raw.keys():
for sample in training_raw[cls].keys():
tmp = {}
tmp[NOISY] = training_raw[cls][sample][NOISY]
tmp[KLT] = training_raw[cls][sample][KLT]
tmp[KLT_JABLOUN] = training_raw[cls][sample][KLT_JABLOUN]
tmp[STSA] = training_raw[cls][sample][STSA]
tmp[LOGTSA] = training_raw[cls][sample][LOGTSA]
tmp[LOGSTA_NEST] = training_raw[cls][sample][LOGSTA_NEST]
tmp[LOGSTA_SAP_Q] = training_raw[cls][sample][LOGSTA_SAP_Q]
tmp[WEUCLID] = training_raw[cls][sample][WEUCLID]
tmp[RDC] = training_raw[cls][sample][RDC]
tmp[RDC_NEST] = training_raw[cls][sample][RDC_NEST]
tmp[MB] = training_raw[cls][sample][MB]
tmp[WAVTHRE] = training_raw[cls][sample][WAVTHRE]
tmp[SCALART] = training_raw[cls][sample][SCALART]
tmp[TSOUKALAS] = training_raw[cls][sample][TSOUKALAS]
training_X.append(list(tmp.values()))
training_Y.append(cls)
return training_X, training_Y
bestP = 0
bestPSamples = []
for ratio in range(1, 8):
for testSamples in itertools.combinations(SAMPLE_NAMES, ratio):
splitData (DATA_FILE, 'train' + str(ratio) + '.csv', 'test' + str(ratio) + '.csv', testSamples)
X, y = process_data ('train' + str(ratio) + '.csv', TRAINING_FILE_HAS_HEADER)
# train model
clf = SVC(gamma='scale')
clf.fit(X, y)
right_prediction = 0
wrong_prediction = 0
testing_X, testing_Y = process_data('test' + str(ratio) + '.csv', TESTING_FILE_HAS_HEADER)
results = clf.predict(testing_X)
confusion_matrix = np.zeros( ( len(set(testing_Y)), len(set(testing_Y)) ) )
for (result,label) in zip(results, testing_Y):
confusion_matrix[getIndex(result)][getIndex(label)] = confusion_matrix[getIndex(result)][getIndex(label)] + 1
if ( result == label) :
right_prediction = right_prediction + 1
else:
wrong_prediction = wrong_prediction + 1
ap = 0
ar = 0
for label in set(testing_Y):
tp = 0
fp = 0
tn = 0
fn = 0
for i in range(0, len(set(testing_Y))):
for j in range(0, len(set(testing_Y))):
if i == getIndex(label):
if j == getIndex(label):
tp = tp + confusion_matrix[i][j]
else:
fp = fp + confusion_matrix[i][j]
else:
if j == getIndex(label):
fn = fn + confusion_matrix[i][j]
else:
tn = tn + confusion_matrix[i][j]
p = tp/(tp+fp)
r = tp/(tp+fn)
ap = ap + p
ar = ar + r
if bestP <= ap/len(set(testing_Y)):
bestP = ap/len(set(testing_Y))
bestPSamples = testSamples
print('Best Precision: ' + str(bestP))
print('Best Precision Samples: ' + ' '.join(bestPSamples))