-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchMarkData.py
148 lines (115 loc) · 3.94 KB
/
BenchMarkData.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
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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from mod_settings.GenParam import GenSimParam, LoadPrm
from mod_load.FetchDataObj import FetchDataObj
from sklearn.ensemble import RandomForestClassifier as rfc
from sklearn.svm import SVC as svc
from sklearn.linear_model import Ridge, RidgeCV, LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# Load in param template
tprm = LoadPrm(param_file='')
# Gen final prm file
prm = GenSimParam(param_file=tprm) # Produce PAramater File
for dataset in ['c2DDS', 'MMDS']: # ['d2DDS', 'c2DDS', 'BankNote', 'MMDS']
print(" ### %s ###" % (dataset))
prm['DE']['training_data'] = dataset
# Make load object
lobj = FetchDataObj(prm)
trainX, trainY = lobj.fetch_data('train')
testX, testY = lobj.fetch_data('test')
#
#
print("\n** Random Forest Algorithm **")
rfc_object = rfc(n_estimators=100, random_state=0)
rfc_object.fit(trainX, trainY)
predicted_labels = rfc_object.predict(testX)
#print(classification_report(testY, predicted_labels))
#print(confusion_matrix(testY, predicted_labels))
print("accuracy:", accuracy_score(testY, predicted_labels), ", error:", 1-accuracy_score(testY, predicted_labels))
"""
fig = plt.figure()
Y = rfc_object.predict_proba(testX)
plt.scatter(testX[:,0], testX[:,1])
plt.title('Random Forest Algorithm (probability)')
plt.ylabel('a2')
plt.xlabel('a1')
plt.colorbar()
#"""
#
#
print("\n** Support Vector Machine **")
trainY2d = trainY.reshape(-1, 1)
testY2d = testY.reshape(-1, 1)
svc_object = svc(kernel='linear')
svc_object.fit(trainX, trainY2d)
predicted_labels = svc_object.predict(testX)
#print(classification_report(testY2d, predicted_labels))
#print(confusion_matrix(testY2d, predicted_labels))
print("SVM linear:", accuracy_score(testY2d, predicted_labels), ", error:", 1-accuracy_score(testY2d, predicted_labels))
svc_object = svc(kernel='poly')
svc_object.fit(trainX, trainY2d)
predicted_labels = svc_object.predict(testX)
#print(classification_report(testY2d, predicted_labels))
#print(confusion_matrix(testY2d, predicted_labels))
print("SVM poly:", accuracy_score(testY2d, predicted_labels), ", error:", 1-accuracy_score(testY2d, predicted_labels))
"""
fig = plt.figure()
Y = svc_object.decision_function(testX)
plt.scatter(testX[:,0], testX[:,1], c=Y)
plt.title('SVM')
plt.ylabel('a2')
plt.xlabel('a1')
plt.colorbar()
#"""
# .reshape(-1, 1)
#
print("\n** Ridge Regression **")
model = RidgeCV(normalize=False) # alphas=(0.01, 0.1, 1.0, 10.0),
model.fit(trainX, trainY)
Y = model.predict(testX)
class_out = []
for val in Y:
if val >= 1.5:
class_out.append(2)
else:
class_out.append(1)
#print(classification_report(testY2d, class_out))
#print(confusion_matrix(testY2d, class_out))
print("accuracy:", accuracy_score(testY, class_out), ", error:", 1-accuracy_score(testY, class_out))
"""
fig = plt.figure()
plt.scatter(testX[:,0], testX[:,1], c=Y)
plt.title('Ridge Regression')
plt.ylabel('a2')
plt.xlabel('a1')
plt.colorbar()
#"""
#
#
print("\n** Logistic Regression **")
model = LogisticRegression()
model.fit(trainX, trainY)
predicted_labels = model.predict(testX)
#print(classification_report(testY, predicted_labels))
#print(confusion_matrix(testY, predicted_labels))
print("accuracy:", accuracy_score(testY, predicted_labels), ", error:", 1-accuracy_score(testY, predicted_labels))
"""
fig = plt.figure()
Y = model.decision_function(testX)
plt.scatter(testX[:,0], testX[:,1], c=Y)
plt.title('Logistic Regression')
plt.ylabel('a2')
plt.xlabel('a1')
plt.colorbar()
#"""
# ################################
#
#
#
plt.show()
#
#
#
#
# fin